1. 流程
1.1. 创建虚拟环境,并且激活
# 使用 venv 创建
python -m venv [ProjectName]
# 激活虚拟环境
cd [ProjectName]
\Scripts\active
1.2. 安装模块
# 安装环境
pip install -r requirements.txt
1.3. 打包
# 简单打包 不带命令行窗口
pyinstaller --clean -F -w -i [path] [py-path]
# 带命令行窗口
pyinstaller --clean -F -i [path] [py-path]
2. pyinstaller 常用参数说明
参数 | 作用 | 原文说明 |
-F, --onefile | 产生单个的可执行文件 | Create a one-file bundled executable. |
-D, --onedir | 产生一个目录(包含多个文件)作为可执行程序 | Create a one-folder bundle containing an executable (default) |
-a, --ascii | 不包含 Unicode 字符集支持 | Do not include unicode encoding support (default: included if available) |
-d [选项], --debug [选项] | 产生 debug 版本的可执行文件 | -d {all,imports,bootloader,noarchive}, --debug {all,imports,bootloader,noarchive} Provide assistance with debugging a frozen application. This argument may be provided multiple times to select several of the following options. - all: All three of the following options. - imports: specify the -v option to the underlying Python interpreter, causing it to print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded. See https://docs.python.org/3/using/cmdline.html#id4. - bootloader: tell the bootloader to issue progress messages while initializing and starting the bundled app. Used to diagnose problems with missing imports. - noarchive: instead of storing all frozen Python source files as an archive inside the resulting executable, store them as files in the resulting output directory. |
-w, --windowed, --noconsole | 指定程序运行时不显示命令行窗口(仅对 Windows 有效) | Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS this also triggers building a Mac OS .app bundle. On Windows this option is automatically set if the first script is a '.pyw' file. This option is ignored on *NIX systems. |
-c, --nowindowed, --console | 指定使用命令行窗口运行程序(仅对 Windows 有效) | Open a console window for standard i/o (default). On Windows this option has no effect if the first script is a '.pyw' file. |
-o [DIR], --out [DIR] | 指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件 | |
-p [DIR], --paths=[DIR] | 设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径 (模块未导入时,可以将模块路径加入) | A path to search for imports (like using PYTHONPATH). Multiple paths are allowed, separated by ``';'``, or use this option multiple times. Equivalent to supplying the ``pathex`` argument in the spec file. |
-n [NAME], --name [NAME] | 指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字 | Name to assign to the bundled app and spec file (default: first script's basename) |
-i [ico path], --icon [ico path] | 添加可执行文件图标 路径 | <FILE.ico or FILE.exe,ID or FILE.icns or "NONE">, --icon <FILE.ico or FILE.exe,ID or FILE.icns or "NONE"> |
--clean | 在构建前清除缓存 和 删除临时文件 | Clean PyInstaller cache and remove temporary files before building. |
3. 注意
3.1 关于闪退的情况如何调试
在程序的文件夹,启动 cmd
,在 cmd
种启动 exe
文件,就能看到文件的报错了
3.2 打包失败或有问题时,尝试其他版本的 pyinstaller
例如
pip install pyinstaller==4.1 # 目前最稳定的版本
3.3 导入隐藏包
--hidden-import=pandas._libs.tslibs.timedeltas
3.4 使用 pyinstaller 打包多进程程序时,需要使用 freeze_support() 来进行兼容
pyinstaller 来打包的应用中,如果需要使用到多进程,multiprocess,尽量使用 multiprocessing 库,而非更高级的 future,因为无法被冻结
import multiprocessing
def main():
pool = multiprocessing.Pool(processes=4)
pool.map(target=~, arg=~)
pool.close()
pool.join()
if __name__ == '__main__':
multiprocessing.freeze_support() # 这个要放在主程序运行入口前
main()
