其实很简单,首先安装pyinstaller包:
# 打开cmd
pip install pyinstaller
安装后,通过cmd
pyinstaller -F D:\xxx\xx.py
还有别的参数,具体移步pyinstaller参数介绍以及总结,这边仅简单演示一下
打包成功,会显示.exe文件的位置,找到便可直接运行
附打包成功的截图:
D:\xxx>pyinstaller -F xx.py
43 INFO: PyInstaller: 4.0
43 INFO: Python: 3.7.7
43 INFO: Platform: Windows-10-10.0.18362-SP0
44 INFO: wrote D:\xxx\xx.spec
48 INFO: UPX is not available.
61 INFO: Extending PYTHONPATH with paths
['D:\\xxx', 'D:\\xxx']
84 INFO: checking Analysis
84 INFO: Building Analysis because Analysis-00.toc is non existent
84 INFO: Initializing module dependency graph...
88 INFO: Caching module graph hooks...
104 INFO: Analyzing base_library.zip ...
1632 INFO: Processing pre-find module path hook distutils from 'c:\\program files\\python37\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
1633 INFO: distutils: retargeting to non-venv dir 'c:\\program files\\python37\\lib'
2599 INFO: Caching module dependency graph...
2798 INFO: running Analysis Analysis-00.toc
2800 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:\program files\python37\python.exe
2863 INFO: Analyzing D:\xxx\xx.py
2865 INFO: Processing module hooks...
2865 INFO: Loading module hook 'hook-distutils.py' from 'c:\\program files\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
2866 INFO: Loading module hook 'hook-encodings.py' from 'c:\\program files\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
2958 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\\program files\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
2959 INFO: Loading module hook 'hook-xml.py' from 'c:\\program files\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
3149 INFO: Looking for ctypes DLLs
3149 INFO: Analyzing run-time hooks ...
3155 INFO: Looking for dynamic libraries
3290 INFO: Looking for eggs
3291 INFO: Using Python library c:\program files\python37\python37.dll
3291 INFO: Found binding redirects:
[]
3293 INFO: Warnings written to D:\xxx\build\xx\warn-xx.txt
3320 INFO: Graph cross-reference written to D:\xxx\build\xx\xref-xx.html
3337 INFO: checking PYZ
3337 INFO: Building PYZ because PYZ-00.toc is non existent
3338 INFO: Building PYZ (ZlibArchive) D:\xxx\build\xx\PYZ-00.pyz
3734 INFO: Building PYZ (ZlibArchive) D:\xxx\build\xx\PYZ-00.pyz completed successfully.
3739 INFO: checking PKG
3739 INFO: Building PKG because PKG-00.toc is non existent
3740 INFO: Building PKG (CArchive) PKG-00.pkg
5014 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
5015 INFO: Bootloader c:\program files\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
5015 INFO: checking EXE
5016 INFO: Building EXE because EXE-00.toc is non existent
5016 INFO: Building EXE from EXE-00.toc
5017 INFO: Appending archive to EXE D:\xxx\dist\xx.exe
5021 INFO: Building EXE from EXE-00.toc completed successfully.
其中,-F 表示打包成单独的 .exe 文件,这时生成的 .exe 文件会比较大,而且运行速度回较慢。仅仅一个 helloworld 程序,生成的文件就 5MB 大。
另外,使用 -i 还可以指定可执行文件的图标;
-w 表示去掉控制台窗口,这在 GUI 界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!
PyInstaller 会对脚本进行解析,并做出如下动作:
1、在脚本目录生成 helloworld.spec 文件;
2、创建一个 build 目录;
3、写入一些日志文件和中间流程文件到 build 目录;
4、创建 dist 目录;
5、生成可执行文件到 dist 目录;
后又看到了另一种方法:使用 py2exe 打包 Python 程序