这是一个关于使用pyinstaller打包python脚本文件(.py文件)的简明教程。
1.安装Pyinstaller。
-
在终端窗口运行命令pip install pyinstaller 来安装 pythonpyinstaller模块。
(MyPythonEnv) C:\Users\zhaosong>pip install pyinstaller Collecting pyinstaller Downloading pyinstaller-4.6-py3-none-win_amd64.whl (1.9 MB) |████████████████████████████████| 1.9 MB 39 kB/s Collecting pyinstaller-hooks-contrib>=2020.6 Downloading pyinstaller_hooks_contrib-2021.3-py2.py3-none-any.whl (200 kB) |████████████████████████████████| 200 kB 25 kB/s Collecting pefile>=2017.8.1 Downloading pefile-2021.9.3.tar.gz (72 kB) |████████████████████████████████| 72 kB 25 kB/s Collecting pywin32-ctypes>=0.2.0 Downloading pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB) Requirement already satisfied: setuptools in c:\users\zhaosong\anaconda3\envs\mypythonenv\lib\site-packages (from pyinstaller) (58.0.4) Collecting altgraph Downloading altgraph-0.17.2-py2.py3-none-any.whl (21 kB) Collecting future Downloading future-0.18.2.tar.gz (829 kB) |████████████████████████████████| 829 kB 22 kB/s Building wheels for collected packages: pefile, future Building wheel for pefile (setup.py) ... done Created wheel for pefile: filename=pefile-2021.9.3-py3-none-any.whl size=68844 sha256=42ddeb63b48399239f9b7ffc7f8148b389307ee76427c60d602c88b481e0a7d2 Stored in directory: c:\users\zhaosong\appdata\local\pip\cache\wheels\ee\a0\e1\ff2dafe5ae846f536acf0a4cc4a8b8d4ccb3044ab87f3ef174 Building wheel for future (setup.py) ... done Created wheel for future: filename=future-0.18.2-py3-none-any.whl size=491070 sha256=486c247d31a3aaf5f7256955f8d4a063ca3051479be6f3830cc9bbce1c12f918 Stored in directory: c:\users\zhaosong\appdata\local\pip\cache\wheels\8e\70\28\3d6ccd6e315f65f245da085482a2e1c7d14b90b30f239e2cf4 Successfully built pefile future Installing collected packages: future, pywin32-ctypes, pyinstaller-hooks-contrib, pefile, altgraph, pyinstaller Successfully installed altgraph-0.17.2 future-0.18.2 pefile-2021.9.3 pyinstaller-4.6 pyinstaller-hooks-contrib-2021.3 pywin32-ctypes-0.2.0 -
运行命令pip show pyinstaller来验证它是否已经安装成功。
(MyPythonEnv) C:\Users\zhaosong>pip show pyinstaller Name: pyinstaller Version: 4.6 Summary: PyInstaller bundles a Python application and all its dependencies into a single package. Home-page: http://www.pyinstaller.org/ Author: Hartmut Goebel, Giovanni Bajo, David Vierra, David Cortesi, Martin Zibricky Author-email: License: GPLv2-or-later with a special exception which allows to use PyInstaller to build and distribute non-free programs (including commercial ones) Location: c:\users\zhaosong\anaconda3\envs\mypythonenv\lib\site-packages Requires: pyinstaller-hooks-contrib, altgraph, pywin32-ctypes, pefile, setuptools Required-by: -
如果安装失败,你可以到www.lfd.uci.edu/ ~gohlke/py…下载编译好的WHL文件。
-
你可以在上面的下载页面上搜索关键字pyinstaller来找到PyInstaller-3.6-py2.py3-none-any.whl 文件。
-
下载它并运行下面的命令来安装它。
pip install PyInstaller‑3.6‑py2.py3‑none‑any.whl
2.一般打包。
-
要打包abc.py文件,你可以简单地在终端执行pyinstaller abc.py命令。注意:终端需要切换到保存abc.py文件的目录。
-
常用选项和描述。
1. -F: Only a single exe format file is generated after packaging. 2. -D: The default option is to create a directory containing exe files and a large number of dependent files. 3. -c: The default option is to use the console (a black window similar to dos window). 4. -w: Do not use console. 5. -p: Add a search path to find the corresponding library. 6. -i: Change the icon of the generated program.
3.特殊包装。
-
我们可以编辑spec文件来满足一些特殊的包装要求。那么,什么是spec文件呢?
-
它的作用是告诉pyinstaller如何打包你的**.py文件。当你在终端使用命令自动打包.py文件时,pyinstaller会先自动创建一个spec**文件。
-
一般来说,我们不需要自己编辑spec文件,但以下情况除外。1)需要打包资源文件,2)在可执行文件中添加运行时选项,或者包含一些pyinstaller不知道的运行时库。
-
要生成abc.py的规格文件,只需在终端执行以下命令。命令选项与pyinstaller 相同。
pyi-makespec abc.py -
下面是一个简单的 spec 文件的例子。
# -*- mode: python -*- block_cipher = None a = Analysis(['abc.py'], pathex=['C:\\Users\\test'], binaries=[], datas=[], hiddenimports=[], hookspath=[], runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE(pyz, a.scripts, exclude_binaries=True, name='abc', debug=False, strip=False, upx=True, console=True ) coll = COLLECT( exe, a.binaries, a.zipfiles, a.datas, strip=False, upx=True, name='abc') -
分析。用于定义 Python 源文件,包括搜索路径、源文件名等。
1) scripts:Source files defined in the Analysis. 2) pure:Python module. 3) binaries:Dynamic library. 4) datas:Data files, including pictures, fonts, etc. 5) zipfiles:Dependent files in zip format, they are generally library files in egg format. -
PYZ。压缩和打包 Python 文件,包括程序运行所需的所有依赖。输入一般为Analysis.pure。
-
EXE:根据上述两个项目打包并生成EXE 文件。EXE子任务包括Analysis的所有五个输出项,以及程序运行所需的一些配置文件和动态库。
-
配置文件和动态库是以TOC格式(名称、路径、类型码)配置的,下面是一个例子。
exe = EXE(pyz, a.scripts, exclude_binaries=True, name='abc', debug=False, strip=False, upx=True, console=True ) -
类型码包括以下数值。
```
1. EXTENSION:python extension library.
2. PYSOURCE:python script.
3. PYMODULE;
4. PYZ;
5. PKG;
6. BINARY:Dynamic library.
7. DATA:Data files.
8. OPTION
```
11. COLLECT:它用于建立最终的生成目录。你可以复制其他子任务产生的结果,并把它们复制到指定的目录中,形成最终的包装结果。这个配置项不是必须的项目,你可以通过不配置它来使用默认的COLLECT 值。 12. 编辑完abc.spec文件后,在终端上执行以下命令,生成所需的app(.exe)文件。
```
pyinstaller abc.spec
Command options include:
–upx-dir,
–distpath,
–noconfirm,
–ascii
```
The postHow To Package Python Scripts Using Pyinstallerappeared first on.