- Secret Recipes of the Python Ninja
- Cody Jackson
- 780字
- 2021-06-25 22:14:44
How to do it...
- Write your Python program.
- To create a Windows .exe file, create a setup.py file to tell the libraries what you want to do. This is mainly importing the setup() function from the Distutils library, importing py2exe, and then calling setup and telling it what type of application it is making, for example, a console, and what the main Python file is. py2exe_setup.py, following, is an example from the documentation of a setup.py file:
from distutils.core import setup import py2exe setup(console=['hello.py'])
- Run the setup script by calling python setup.py py2exe. This creates two directories: build/ and dist/. The dist/ directory is where the new files are placed, while build/ is used for temporary files during the creation process.
- Test the application by moving to the dist/ directory and running the .exe file located there.
- To make a macOS .app file, create the setup.py file. Any icons or data files required for the application need to be included during this step.
- Clean up the build/ and dist/ directories to ensure there are no files that may be accidentally included.
- Use Alias mode to build the application in-place, that is, not ready for distribution. This allows you to test the program before bundling for delivery.
- Test the application and verify it works correctly in alias mode.
- Clean up the build/ and dist/ directories again.
- Run python setup.py py2app to create the distributable .app file.
- For cross-platform files, the easiest way to use cx_Freeze is to use the cxfreeze script:
cxfreeze <program>.py --target-dir=<directory>
Other options are available for this command, such as compressing the bytecode, setting an initialization script, or even excluding modules.
If more functionality is required, a distutils setup script can be created. The command cxfreeze-quickstart can be used to generate a simple setup script; the cx_Freeze documentation provides an example setup.py file (cxfreeze_setup.py):
import sys from cx_Freeze import setup, Executable # Dependencies are automatically detected, but it might need fine tuning. build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]} # GUI applications require a different base on Windows (the default is for # console application). base = None if sys.platform == "win32": base = "Win32GUI" setup( name = "guifoo", version = "0.1", description = "My GUI application!", options = {"build_exe": build_exe_options}, executables = [Executable("guifoo.py", base=base)])
To run the setup script, run the command: python setup.py build. This will create the directory build/, which contains the subdirectory exe.xxx, where xxx is the platform-specific executable binary indicator:
-
- For developers who need even more control, or are looking at creating C scripts for extending or embedding Python, manually working with the classes and modules within the cx_Freeze program is possible.
- If using PyInstaller, its use is like most other Python programs, and is a simple command:
pyinstaller <program>.py
This generates the binary bundle in the dist/ subdirectory. Naturally, there many other options available when running this command:
-
- Optionally, UPX (https://upx.github.io/) can be used to compress the executable files and libraries. When used, UPX compresses the files and wraps them in a self-decompressing file. When executed, the UPX wrapper decompresses the enclosed files and the resulting binary is executed normally.
- To create multiple Python environments for a single operating system, it is recommended you to create virtual Python environments for each Python version to be generated. Then, install PyInstaller in each environment and build the binary within each environment.
- Like cx_Freeze, to create binaries for different operating systems, the other OSes must be available and PyInstaller used on each one.
- Create your Python file; save it with the extension .pyx. For example, helloworld.pyx.
- When working with Cython, create a setup.py file that looks similar to cython_setup.py from http://docs.cython.org/en/latest/src/tutorial/cython_tutorial.html#the-basics-of-cython:
from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("helloworld.pyx") )
- Create the Cython file by running the following:
$ python setup.py build_ext --inplace
- This creates a file in the local directory: helloworld.so on *nix and helloworld.pyd on Windows.
- To use the binary, simply import it into Python as normal.
- If your Python program doesn't require additional C libraries or a special build configuration, you can use the pyximport library. The install() function from this library allows loading .pyx files directly when imported, rather than having to rerun setup.py every time the code changes.
- To compile a program using Nuitka with all modules embedded, use the following command:
nuitka --recurse-all <program>.py
- To compile a single module, use the following command:
nuitka --module <module>.py
- To compile an entire package and embed all modules, the previous commands are combined into a similar format:
nuitka --module <package> --recurse-directory=<package>
- To make a truly cross-platform binary, use the option --standalone, copy the <program>.dist directory to the destination system, and then run the .exe file inside that directory.
推薦閱讀
- Java逍遙游記
- 演進式架構(原書第2版)
- The DevOps 2.3 Toolkit
- JavaScript從入門到精通(微視頻精編版)
- Instant Testing with CasperJS
- 深入淺出Java虛擬機:JVM原理與實戰
- 信息可視化的藝術:信息可視化在英國
- Mastering Python Scripting for System Administrators
- GeoServer Beginner's Guide(Second Edition)
- 數據結構習題解析與實驗指導
- 移動界面(Web/App)Photoshop UI設計十全大補
- 寫給程序員的Python教程
- Python入門很輕松(微課超值版)
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- 交互設計師成長手冊:從零開始學交互