I have trying to compile by Python scripts using Nuitka. I have six scripts, out of which three are modules and data scripts. Three need to be compiled as an executable. How do I compile all three recursively in Nuitka.
I saw the Nuitka user manual and it said something about Multidist
, and I didn't understand how to do this. I tried something like <command> --main=updater.py --main=installer.py
, but it didn't work.
If you have multiple programs, that each should be executable, in the past you had to compile multiple times, and deploy all of these. With standalone mode, this, of course, meant that you were fairly wasteful, as sharing the folders could be done, but wasn’t really supported by Nuitka.
Enter Multidist. There is an option --main that replaces or adds to the positional argument given. And it can be given multiple times. When given multiple times, Nuitka will create a binary that contains the code of all the programs given, but sharing modules used in them. They therefore do not have to be distributed multiple times.
Let’s call the basename of the main path, and entry point. The names of these must, of course, be different. Then the created binary can execute either entry point, and will react to what sys.argv[0] appears to it. So if executed in the right way (with something like subprocess or OS API you can control this name), or by renaming or copying the binary, or symlinking to it, you can then achieve the miracle.
Python version: 3.11.6
Nuitka version: 1.9.2
OS: Windows
Arch: x86_64
WindowsRelease: 10 (Actually 11)
Nuitka-Scons:INFO: For Python version 3.11 MSVC 14.3 or later is required, not 14.2 which is too old.
Version C compiler: ~\AppData\Local\Nuitka\Nuitka\Cache\downloads\gcc\x86_64\13.2.0-16.0.6-11.0.1-msvcrt-r1\mingw64\bin\gcc.exe (gcc 13.2.0).
Any help is appreciated.
Consider this example:
# test1.py
print("hi")
# test2.py
print("hello")
Compiling via python -m nuitka --main=test1.py --main=test2.py --standalone
produces a single binary: ./test1.dist/test1.bin
Executing test1.bin
runs the test1.py
entry point:
hi
Execute the other entry point (test2.py
) by renaming the executable to match name of the other entry point (rename test1.bin
-> test2.bin
):
hello
You can also use Python to run a particular entry point.
# Output is 'hi'
subprocess.run("test1", executable="./test1.dist/test1.bin")
# Output is 'hello'
subprocess.run("test2", executable="./test1.dist/test1.bin")
More generally, Nuitka picks the entry point depending on the value of sys.argv[0]
. This is what is meant by this part of the docs:
Then the created binary can execute either entry point, and will react to what sys.argv[0] appears to it. So if executed in the right way (with something like subprocess or OS API you can control this name), or by renaming or copying the binary, or symlinking to it, you can then achieve the miracle.