Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How can I make a standalone executable from Python code?
I know that I can make Python code run starting from the script, but this still requires the user to have Python installed (and will still rely on configuring the system so that it can find Python from the script, which can go wrong in a few different ways).
I want to be able to distribute my program to others who might not have Python installed, or even understand what Python is. I don't want to guide users through installing Python, or make an installer that does it for them (that seems like a lot of work, and ideally my program shouldn't need an installer at all). What other options do I have?
1 answer
Normally, Python is compiled, but it compiles to bytecode, not to your computer's native instructions. This bytecode is cross-platform, but it can't be used by the CPU directly - it can't be turned into a native executable.
So fundamentally there are two ways to fix the problem: by using a custom compilation process, or by setting up a program that can use the bytecode (the same way that the Python interpreter, itself, does) and then making it use that bytecode specifically.
Bundling an interpreter
This approach is probably the most common. Typically it's done using tools like cx_Freeze, PyInstaller, py2exe (Windows only) or py2app (Mac only). These tools basically all do the same thing, so the choice is mostly down to personal preference.
Understanding this approach
Basically, these tools work by setting up a folder that contains your code (possibly pre-compiled into .pyc
files), the necessary dependencies for your code (including the Python code for needed standard library modules), a "Python library" file (a .dll
or .so
file that contains functions that another native executable can use to interpret Python), and a small "wrapper" native executable that works by reading your code and using the library to interpret it (including doing any necessary import
s). From there you can simply zip up and distribute the folder, and tell users to unzip it and run the wrapper executable.
This sort of tool may also offer (as PyInstaller does for example) a "one-file" option. It works something like a self-extracting archive which then automatically starts the interpreter after extracting into a temporary folder. That is: you get a wrapper program that has an embedded archive, and the program's logic is to unpack the archive into a temporary folder, read the starting bytecode file out of that folder, and start the interpreter (also using the library from that folder) with that bytecode.
Of course, it's also possible to use standard installer-creator tools to make an "installer" for the zipped folder (which could also, e.g., add the unpacked folder to the system PATH
, so that the wrapper executable can be started from the command line by name). cx_Freeze suggests that approach.
Keep in mind that even though some bundling tools are cross-platform, the bundles they produce still aren't. They need to contain a Python interpretation library, and that library is platform-specific (since it contains native compiled code).
Native compilation options
Another way to solve the problem is to just find a way to turn Python code directly into native code, instead of bytecode for the Python interpreter. The most commonly cited options for this are Nuitka and Cython.
Nuitka directly makes an executable from your code, and its main interface is a Python library (that you can run like python -m nuitka
, the same way that you would with Pip). It requires a compiler up front: for newer Python the compiler needs to support C11, and for older Python it needs to support C++03. Nuitka also offers some commercial options (paid advanced features).
Cython translates your code into C, so that it can then be compiled and built like a traditional C program. You're on your own for compiling the C code - so you can customize the build process. The Cython project also defines extensions to the Python language, also called Cython: that is, you can write in a superset of Python that's specially designed for interfacing between that code and C. The Cython compiler is mostly implemented in Python (and Cython), but the main interface is more complex (but flexible) than with Nuitka.
References:
- Create a single executable from a Python project on Stack Overflow
- How can I make a Python script standalone executable to run without any dependency? on Stack Overflow
- The documentation for the projects cited herein, as linked herein
1 comment thread