Adding Data Files in Nuitka (Python Files, Images, etc)

Nuitka is a Python-to-C compiler that converts Python code into executable binaries. While Nuitka efficiently compiles Python scripts, incorporating data files such as images, audio, video, and additional Python files requires can be a little tricky. This guide outlines the steps to include various data files in a Nuitka standalone executable.


Basic Setup for Nuitka

Before delving into the inclusion of data files, ensure you have Nuitka installed. You can install Nuitka via pip:

pip install nuitka

For a more detailed starter guide on Nuitka and its various options, you can follow this link to our main Nuitka Tutorial. If you already familiar with Nuitka, then proceed ahead in this tutorial to learn how to add data files in our Nuitka EXE.


Compiling a Python Script with Nuitka

Assuming you have a Python script named main.py, the basic command to compile it into a standalone executable using Nuitka is:

python -m nuitka --standalone main.py

This command creates a standalone directory containing the executable and all necessary dependencies. However, if your program relies on external data files (images, audio, video, other Python files), you need to explicitly include these files.


Adding Data Files in Nuitka

1. Adding Individual Files

To include individual files such as images, use the --include-data-files option. The syntax is:

--include-data-files=<source-location>=<target-location>

For example, to include an image file favicon.ico, the command is:

--include-data-files=./favicon.ico=favicon.ico

This command tells Nuitka to include the favicon.ico file from the current directory and place it in the same location within the output directory.


2. Adding Directories

To include entire directories, use the --include-data-dir option. The syntax is the same as earlier:

--include-data-dir=<source-location>=<target-location>

For instance, to include an entire directory named Images, the command is:

--include-data-dir=./Images=Images

This will copy the Images directory from the current location to the output directory, preserving its structure.


Example: Adding Data Files of Multiple Types

Suppose you have a project with the following files and directories:

  • main.py: The main Python script.
  • favicon.ico: An icon file.
  • Images/: A directory containing images.
  • audio.mp3: An audio file.
  • videos/: A directory containing video files.
  • utils.py: An additional Python file used in the project.

To compile this project with all necessary data files included, the command would be:

python -m nuitka --standalone --include-data-files=./favicon.ico=favicon.ico --include-data-files=./audio.mp3=audio.mp3 --include-data-dir=./Images=Images --include-data-dir=./videos=videos --include-data-files=./utils.py=utils.py --disable-console main.py

Pretty confusing isn’t it?

Having to write out this command over and over again each time you want to re-build your executable is a massive pain. Instead, I recommend you use Nuitka Configuration Files, which allow you write out all your dependencies/datafiles/imports in a single file, which can then be executed as many times as you like.


Final Word

By using the --include-data-files and --include-data-dir options, you can include all necessary data files in your Nuitka-compiled executable. This ensures that your standalone application has all the resources it needs to run successfully, whether they are images, audio, video files, or additional Python scripts. Combining these options with plugins and other configurations allows for the creation of robust and self-sufficient executables.

Happy coding!

This marks the end of the Adding Data Files in Nuitka Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content are more than welcome.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments