The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python FAQ: How can I create a stand-alone binary from a Python script on Windows, Mac OS or Linux?
By Guest on 19th December 2022 05:41:05 AM | Syntax: PYTHON | Views: 224



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. 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.
  2.  
  3. Here's how to use PyInstaller to create a stand-alone binary from a Python script:
  4.  
  5. First, make sure that you have PyInstaller installed on your machine. You can install it using pip:
  6.  
  7.  
  8. pip install pyinstaller
  9.  
  10.  
  11. Next, open a terminal or command prompt and navigate to the directory where your Python script is located.
  12.  
  13. Run the following command to create a stand-alone binary from your script:
  14.  
  15.  
  16. pyinstaller script.py
  17.  
  18. Replace "script.py" with the name of your Python script.
  19.  
  20. 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).
  21.  
  22. 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.
  23.  
  24. 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.
  25.  
  26. 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.
  27.  
  28. From the Python official documentation:
  29.  
  30. How can I create a stand-alone binary from a Python script?
  31. -----------------------------------------------------------
  32.  
  33. You don't need the ability to compile Python to C code if all you want
  34. is a stand-alone program that users can download and run without
  35. having to install the Python distribution first.  There are a number
  36. of tools that determine the set of modules required by a program and
  37. bind these modules together with a Python binary to produce a single
  38. executable.
  39.  
  40. One is to use the freeze tool, which is included in the Python source
  41. tree as "Tools/freeze". It converts Python byte code to C arrays; with
  42. a C compiler you can embed all your modules into a new program, which
  43. is then linked with the standard Python modules.
  44.  
  45. It works by scanning your source recursively for import statements (in
  46. both forms) and looking for the modules in the standard Python path as
  47. well as in the source directory (for built-in modules).  It then turns
  48. the bytecode for modules written in Python into C code (array
  49. initializers that can be turned into code objects using the marshal
  50. module) and creates a custom-made config file that only contains those
  51. built-in modules which are actually used in the program.  It then
  52. compiles the generated C code and links it with the rest of the Python
  53. interpreter to form a self-contained binary which acts exactly like
  54. your script.
  55.  
  56. The following packages can help with the creation of console and GUI
  57. executables:
  58.  
  59. * Nuitka (Cross-platform)
  60.  
  61. * PyInstaller (Cross-platform)
  62.  
  63. * PyOxidizer (Cross-platform)
  64.  
  65. * cx_Freeze (Cross-platform)
  66.  
  67. * py2app (macOS only)
  68.  
  69. * 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
  • Recent Pastes