Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

50%
+0 −0
Q&A How can I make a standalone executable from Python code?

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...

posted 5mo ago by Karl Knechtel‭  ·  edited 5mo ago by Karl Knechtel‭

Answer
#3: Post edited by user avatar Karl Knechtel‭ · 2024-06-14T11:22:58Z (5 months ago)
  • 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](https://marcelotduarte.github.io/cx_Freeze/), [PyInstaller](https://pyinstaller.org/en/stable/), [py2exe](https://www.py2exe.org/) <span class="notice is-warning">(Windows only)</span> or [py2app](https://github.com/ronaldoussoren/py2app) <span class="notice is-warning">(Mac only)</span>.
  • 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](https://pyinstaller.org/en/stable/operating-mode.html#bundling-to-one-file) 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](https://cx-freeze.readthedocs.io/en/stable/overview.html).
  • Keep in mind that **even though the bundling tool may be cross-platform, the bundles it produces are not**. 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](https://nuitka.net/) and [Cython](https://cython.org/).
  • 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](https://software.codidact.com/posts/291675)). It [requires](https://nuitka.net/user-documentation/user-manual.html) 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)](https://cython.readthedocs.io/en/latest/src/quickstart/build.html) than with Nuitka.
  • ----
  • References:
  • * [Create a single executable from a Python project](https://stackoverflow.com/questions/12059509) on Stack Overflow
  • * [How can I make a Python script standalone executable to run without any dependency?](https://stackoverflow.com/questions/5458048) on Stack Overflow
  • * The documentation for the projects cited herein, as linked herein
  • 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](https://marcelotduarte.github.io/cx_Freeze/), [PyInstaller](https://pyinstaller.org/en/stable/), [py2exe](https://www.py2exe.org/) (**Windows only**) or [py2app](https://github.com/ronaldoussoren/py2app) (**Mac only**). These tools basically all do the same thing, so the choice is mostly down to personal preference.
  • <details><summary>Understanding this approach</summary>
  • 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](https://pyinstaller.org/en/stable/operating-mode.html#bundling-to-one-file) 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](https://cx-freeze.readthedocs.io/en/stable/overview.html).
  • </details>
  • <section class="notice is-warning">
  • 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).
  • </section>
  • ## 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](https://nuitka.net/) and [Cython](https://cython.org/).
  • 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](https://software.codidact.com/posts/291675)). It [requires](https://nuitka.net/user-documentation/user-manual.html) 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)](https://cython.readthedocs.io/en/latest/src/quickstart/build.html) than with Nuitka.
  • ----
  • References:
  • * [Create a single executable from a Python project](https://stackoverflow.com/questions/12059509) on Stack Overflow
  • * [How can I make a Python script standalone executable to run without any dependency?](https://stackoverflow.com/questions/5458048) on Stack Overflow
  • * The documentation for the projects cited herein, as linked herein
#2: Post edited by user avatar Karl Knechtel‭ · 2024-06-14T11:17:56Z (5 months ago)
  • 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](https://marcelotduarte.github.io/cx_Freeze/), [PyInstaller](https://pyinstaller.org/en/stable/), [py2exe](https://www.py2exe.org/) <span class="notice is-warning">(Windows only)</span> or [py2app](https://github.com/ronaldoussoren/py2app) <span class="notice is-warning">(Mac only)</span>.
  • 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](https://pyinstaller.org/en/stable/operating-mode.html#bundling-to-one-file) 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](https://cx-freeze.readthedocs.io/en/stable/overview.html).
  • Keep in mind that **even though the bundling tool may be cross-platform, the bundles it produces are not**. 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](https://nuitka.net/) and [Cython](https://cython.org/).
  • 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](https://software.codidact.com/posts/291675)). It [requires](https://nuitka.net/user-documentation/user-manual.html) 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)](https://cython.readthedocs.io/en/latest/src/quickstart/build.html) than with Nuitka.
  • 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](https://marcelotduarte.github.io/cx_Freeze/), [PyInstaller](https://pyinstaller.org/en/stable/), [py2exe](https://www.py2exe.org/) <span class="notice is-warning">(Windows only)</span> or [py2app](https://github.com/ronaldoussoren/py2app) <span class="notice is-warning">(Mac only)</span>.
  • 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](https://pyinstaller.org/en/stable/operating-mode.html#bundling-to-one-file) 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](https://cx-freeze.readthedocs.io/en/stable/overview.html).
  • Keep in mind that **even though the bundling tool may be cross-platform, the bundles it produces are not**. 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](https://nuitka.net/) and [Cython](https://cython.org/).
  • 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](https://software.codidact.com/posts/291675)). It [requires](https://nuitka.net/user-documentation/user-manual.html) 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)](https://cython.readthedocs.io/en/latest/src/quickstart/build.html) than with Nuitka.
  • ----
  • References:
  • * [Create a single executable from a Python project](https://stackoverflow.com/questions/12059509) on Stack Overflow
  • * [How can I make a Python script standalone executable to run without any dependency?](https://stackoverflow.com/questions/5458048) on Stack Overflow
  • * The documentation for the projects cited herein, as linked herein
#1: Initial revision by user avatar Karl Knechtel‭ · 2024-06-14T11:15:24Z (5 months ago)
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](https://marcelotduarte.github.io/cx_Freeze/), [PyInstaller](https://pyinstaller.org/en/stable/), [py2exe](https://www.py2exe.org/) <span class="notice is-warning">(Windows only)</span> or [py2app](https://github.com/ronaldoussoren/py2app) <span class="notice is-warning">(Mac only)</span>.

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](https://pyinstaller.org/en/stable/operating-mode.html#bundling-to-one-file) 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](https://cx-freeze.readthedocs.io/en/stable/overview.html).

Keep in mind that **even though the bundling tool may be cross-platform, the bundles it produces are not**. 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](https://nuitka.net/) and [Cython](https://cython.org/).

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](https://software.codidact.com/posts/291675)). It [requires](https://nuitka.net/user-documentation/user-manual.html) 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)](https://cython.readthedocs.io/en/latest/src/quickstart/build.html) than with Nuitka.