The pyinstaller module is mainly used to package python code into an exe program for direct use, so that it can be run on other computers even if there is no python environment.
pyinstaller is a third-party library, so it needs to be installed in advance when using it
pip install pyinstaller
1. Configure the generated exe program folder
(1) If you are not familiar with the spec configuration content, you can run the following command in the terminal to generate a fixed template
pyinstaller --name myapp main.py # myapp为生成的spec文件名称,main.py为打包的文件
After running, we can see several files and can delete the dist and build files directly. I see that when I open the myapp.spec file, I can see the configuration information.
# -*- mode: python ; coding: utf-8 -*- block_cipher = None a = Analysis( ['main.py','hello.py'], #注:要打包的模块,按照先后顺序运行 pathex=['C:/Users/pythonProject'], #注:要打包的Python源代码的路径列表。 binaries=[], datas=[], #注:要打包的非Python资源(例如图像、配置文件等)列表。 hiddenimports=[''requests], #注:必需的隐藏导入列表,用于告诉PyInstaller找到其他未明确指定的依赖项。 hookspath=[], #注:一个路径列表,其中包含指定要自定义的钩子模块的目录。 hooksconfig={}, #注: runtime_hooks=[], #注:用于在应用程序运行时运行的Python代码文件列表。 excludes=[], #注:不包括在生成的可执行文件中的模块列表 win_no_prefer_redirects=False, #注: win_private_assemblies=False, cipher=block_cipher, #注:用于加密Python字节码的密码。 noarchive=False, ) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) #注:a.pure: 一个布尔值,指示是否生成纯Python字节码。如果为True,则PyInstaller将不包括任何二进制文件或库。
a.zipped_data: A tuple containing all Python scripts and resources in OneFile mode.
exe = EXE( pyz, #注:一个PYZ实例,其中包含要打包的所有Python脚本和资源。 a.scripts, #注:应用程序的主Python脚本列表。 [], exclude_binaries=True, name='myapp', #注:生成exe可执行文件的名称 debug=False, #注:一个布尔值,指示是否生成调试版本的可执行文件 bootloader_ignore_signals=False, #注:一个布尔值,指示是否忽略启动加载器的信号。 strip=False, #注:一个布尔值,指示是否对可执行文件进行符号剥离。 upx=True, #注:一个布尔值,指示是否使用UPX压缩可执行文件 console=True, #注:是否开启dos窗口 disable_windowed_traceback=False, argv_emulation=False, target_arch=None, codesign_identity=None, entitlements_file=None, icon:'图标.ico' #注:用户生成exe文件的封面,后缀必须是ico格式,转换地址:https://convertio.co/zh/download/88c5806204642c8a1c10e65b1bef9b5886f6d8/ ) coll = COLLECT( exe, #注:你的Python脚本生成的可执行文件路径(通常是与spec文件同名的文件) a.binaries, #注:二进制对象列表,表示其他相关二进制文件的位置以及将它们复制到输出目录的相对路径。例如,如果您的应用程序需要音频或图像文件,则可以使用此参数将其包含在可执行文件中。 a.zipfiles, #注:压缩文件列表,表示应该从zip文件中提取哪些文件并将它们放入输出目录。这对于打包一些必需的库或数据文件非常有用。 a.datas, #注:数据文件列表,这些文件不应被压缩,但应该被复制到输出目录中。例如,这可能包括配置文件、模板文件或其他类型的文本文件。 strip=False, #注:是否从可执行文件和库中去除调试信息。默认情况下为True,这将减小文件大小,但会使得调试更加困难。 upx=True, #注:是否使用UPX来压缩可执行文件和库。默认情况下为False,因为UPX可能会导致某些文件无法正常工作。 upx_exclude=[], #注: name='myapp', #注:打包文件夹名称。 )
(3) Usually we only need to configure the module that needs to be packaged. pathex generates the name of the exe file and the name of the directory where the exe exists. After configuration, enter the command directly in the terminal for packaging.
pyinstaller myapp.spec
(4) After the packaging is completed, you can look at the dist file in the directory where the project is located. The directory next to the dist file is the packaging folder, and below the folder is the program. It should be noted that since we package the entire folder, when sharing it with others, you need to send the entire folder to ensure correct operation.
(5) Configure the modules to be packaged. If there is an import connection between the modules, you can directly package the module that is finally run (main). pyinstaller will by default The imports (.py third-party libraries) involved in main are all packaged together. If the two py modules have no relationship with each other, multiple modules can be packaged at this time.
2. Configure to generate an independent exe file
(1) The steps are the same as above, first generate the template
pyinstaller -F main.py
(2) The template is about The style is as follows, and the field meanings are the same as above.
# -*- mode: python ; coding: utf-8 -*- block_cipher = None a = Analysis( ['main.py'], pathex=[], binaries=[], datas=[], hiddenimports=[], hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher, noarchive=False, ) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE( pyz, a.scripts, a.binaries, a.zipfiles, a.datas, [], name='hello', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, upx_exclude=[], runtime_tmpdir=None, console=True, disable_windowed_traceback=False, argv_emulation=False, target_arch=None, codesign_identity=None, entitlements_file=None, )
(3) After the configuration is completed, the terminal runs the command packaging. After the packaging is completed, you will see that there is only one exe file in the dist directory.
pyinstaller myapp.spec
In addition to configuring the spec file, you can also directly package it through the pyinstaller command. I won’t introduce it here.
The above is the detailed content of How to install and configure the Python pyinstaller library. For more information, please follow other related articles on the PHP Chinese website!