- There are a few different ways to create a stand-alone binary from a Python script. One way is to use a tool called PyInstaller. PyInstaller is a free and open-source tool that converts Python scripts into standalone executables, by bundling the Python interpreter, libraries, and your script into a single package.
- Here's how to use PyInstaller to create a stand-alone binary from a Python script:
- First, make sure that you have PyInstaller installed on your machine. You can install it using pip:
- pip install pyinstaller
- Next, open a terminal or command prompt and navigate to the directory where your Python script is located.
- Run the following command to create a stand-alone binary from your script:
- pyinstaller script.py
- Replace "script.py" with the name of your Python script.
- PyInstaller will create a stand-alone binary in a subdirectory called "dist". The binary will be located in a subdirectory that corresponds to your operating system and architecture (e.g., "dist/windows/64bit" for a 64-bit Windows system).
- You can also specify additional options when running PyInstaller to customize the behavior of the stand-alone binary. For example, you can use the --onefile option to create a single executable file instead of a directory of files. You can find a full list of options in the PyInstaller documentation.
- Another option for creating a stand-alone binary from a Python script is to use a tool called Py2exe. Py2exe is a free and open-source tool that converts Python scripts into stand-alone executables for Windows. It works in a similar way to PyInstaller, but is specifically designed for use on Windows systems.
- Finally, you can also use a tool called Nuitka to create a stand-alone binary from a Python script. Nuitka is a free and open-source tool that converts Python scripts into stand-alone executables for a variety of platforms, including Windows, Linux, and macOS. It works by compiling the Python code into C++, which is then linked to create a stand-alone executable.
- From the Python official documentation:
- How can I create a stand-alone binary from a Python script?
- -----------------------------------------------------------
- You don't need the ability to compile Python to C code if all you want
- is a stand-alone program that users can download and run without
- having to install the Python distribution first. There are a number
- of tools that determine the set of modules required by a program and
- bind these modules together with a Python binary to produce a single
- executable.
- One is to use the freeze tool, which is included in the Python source
- tree as "Tools/freeze". It converts Python byte code to C arrays; with
- a C compiler you can embed all your modules into a new program, which
- is then linked with the standard Python modules.
- It works by scanning your source recursively for import statements (in
- both forms) and looking for the modules in the standard Python path as
- well as in the source directory (for built-in modules). It then turns
- the bytecode for modules written in Python into C code (array
- initializers that can be turned into code objects using the marshal
- module) and creates a custom-made config file that only contains those
- built-in modules which are actually used in the program. It then
- compiles the generated C code and links it with the rest of the Python
- interpreter to form a self-contained binary which acts exactly like
- your script.
- The following packages can help with the creation of console and GUI
- executables:
- * Nuitka (Cross-platform)
- * PyInstaller (Cross-platform)
- * PyOxidizer (Cross-platform)
- * cx_Freeze (Cross-platform)
- * py2app (macOS only)
- * py2exe (Windows only)
Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.
Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license