


How to install and configure the Python pyinstaller library
Brief
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.
Usage
1. Installation
pyinstaller is a third-party library, so it needs to be installed in advance when using it
pip install pyinstaller
2.Configuration spec file
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
