用PyInstaller把python代码打包成可执行exe文件
当我们完成一个小游戏的编写后,迫不及待想要分享给其他人去玩,然而由于你的朋友的电脑可能没有安装python,那么你的游戏python脚本将不能运行,这时候我们需要将脚本打包成exe文件,降低脚本对环境的依赖性,同时运行更加迅速。这里我们采用PyInstaller来实现python脚本的exe打包。
PyInstaller的下载安装
python自身并不携带PyInstaller包,这时候需要自行下载安装PyInstaller。安装方法可以用pip,即pip install pyinstaller
。
PyInstaller打包
进入到要打包文件所在的目录,对目标文件进行操作,如pyinstaller -F guss_number.py
其中-F是产生单个的可执行文件。
PyInstaller 支持的常用选项如下,参考http://c.biancheng.net/view/2690.html
-h,–help
查看该模块的帮助信息
-F,-onefile
产生单个的可执行文件
-D,–onedir
产生一个目录(包含多个文件)作为可执行程序
-a,–ascii
不包含 Unicode 字符集支持
-d,–debug
产生 debug 版本的可执行文件
-w,–windowed,–noconsolc
指定程序运行时不显示命令行窗口(仅对 Windows 有效)
-c,–nowindowed,–console
指定使用命令行窗口运行程序(仅对 Windows 有效)
-o DIR,–out=DIR
指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件
-p DIR,–path=DIR
设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径
-n NAME,–name=NAME
指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字
PyInstaller例子
我们把前面所写的猜数字谜游戏打包成exe文件,打包信息如下:
D:\AITFPy\hello\guess_number>pyinstaller -F guss_number.py
244 INFO: PyInstaller: 3.5
245 INFO: Python: 3.5.6
247 INFO: Platform: Windows-10-10.0.18362-SP0
256 INFO: wrote D:\AITFPy\hello\guess_number\guss_number.spec
263 INFO: UPX is not available.
270 INFO: Extending PYTHONPATH with paths
['D:\\AITFPy\\hello\\guess_number', 'D:\\AITFPy\\hello\\guess_number']
271 INFO: checking Analysis
272 INFO: Building Analysis because Analysis-00.toc is non existent
272 INFO: Initializing module dependency graph...
285 INFO: Initializing module graph hooks...
288 INFO: Analyzing base_library.zip ...
6926 INFO: running Analysis Analysis-00.toc
6942 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by d:\users\dell\anaconda3\python.exe
7523 INFO: Caching module hooks...
7527 INFO: Analyzing D:\AITFPy\hello\guess_number\guss_number.py
7539 INFO: Loading module hooks...
7539 INFO: Loading module hook "hook-encodings.py"...
8091 INFO: Loading module hook "hook-pydoc.py"...
8093 INFO: Loading module hook "hook-xml.py"...
8545 INFO: Looking for ctypes DLLs
8545 INFO: Analyzing run-time hooks ...
8550 INFO: Looking for dynamic libraries
8761 INFO: Looking for eggs
8761 INFO: Using Python library d:\users\dell\anaconda3\python35.dll
8764 INFO: Found binding redirects:
[]
8772 INFO: Warnings written to D:\AITFPy\hello\guess_number\build\guss_number\warn-guss_number.txt
8801 INFO: Graph cross-reference written to D:\AITFPy\hello\guess_number\build\guss_number\xref-guss_number.html
8825 INFO: checking PYZ
8825 INFO: Building PYZ because PYZ-00.toc is non existent
8826 INFO: Building PYZ (ZlibArchive) D:\AITFPy\hello\guess_number\build\guss_number\PYZ-00.pyz
9145 INFO: Building PYZ (ZlibArchive) D:\AITFPy\hello\guess_number\build\guss_number\PYZ-00.pyz completed successfully.
9167 INFO: checking PKG
9168 INFO: Building PKG because PKG-00.toc is non existent
9169 INFO: Building PKG (CArchive) PKG-00.pkg
10631 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
10640 INFO: Bootloader d:\users\dell\anaconda3\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
10641 INFO: checking EXE
10641 INFO: Building EXE because EXE-00.toc is non existent
10641 INFO: Building EXE from EXE-00.toc
10642 INFO: Appending archive to EXE D:\AITFPy\hello\guess_number\dist\guss_number.exe
10959 INFO: Building EXE from EXE-00.toc completed successfully.