使用pyinstaller 打包python脚本为exe程序
- 简介
- 环境
- 实操演示
- pyinstaller使用注意的点
使用pyinstaller 实现python脚本打包成exe程序
1. 简介
将python程序打包成exe可执行程序的办法很多,如pyinstaller、py2exe、cx_Freeze等,这里介绍pyinstaller方法
2. 环境
-
window 10 64bit
-
python 3.7.4
-
selenium 3.3.0
-
ChromePortable72
-
chromedriver
3. pyinstall 参数介绍
常用的主要是-F、-p、-i、-w这几个参数。
-
-a:不包含编码.在支持Unicode的python版本上默认包含所有的编码
-
-c:使用控制台子系统执行(默认)(只对Windows有效)
-
-d:产生debug版本的可执行文件
-
-i :指定打包程序使用的图标(icon)文件
-
-F:打包成可执行程序
-
-h:查看帮助
-
-p:添加使用的第三方库路径
-
-v:查看 PyInstaller 版本
-
-w:取消控制台显示(默认是显示控制台的)
4. 实操演示
pyinstaller提供了两种把.py文件打包成.exe文件的方式:
-
第一种,把由.py文件打包而成的.exe文件及相关文件放在一个目录中。
-
代码打印一个helloworld
-
print("#####开始#####") print("hello world!") print("#####结束#####")
-
成功的结果会看到
-
打包前demo结构
-
打包语法:pyinstaller 应用程序
D:\myproject\demo\example2 $ pyinstaller hello.py 66 INFO: PyInstaller: 3.6 67 INFO: Python: 3.7.3 68 INFO: Platform: Windows-10-10.0.18362-SP0 69 INFO: wrote D:\myproject\demo\example2\hello.spec 117 INFO: UPX is not available. 120 INFO: Extending PYTHONPATH with paths ['D:\\myproject\\demo\\example2', 'D:\\myproject\\demo\\example2'] 120 INFO: checking Analysis 121 INFO: Building Analysis because Analysis-00.toc is non existent 121 INFO: Initializing module dependency graph... 124 INFO: Caching module graph hooks... 133 INFO: Analyzing base_library.zip ... 3502 INFO: Caching module dependency graph... 3678 INFO: running Analysis Analysis-00.toc 3681 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by d:\python37\python.exe 4058 INFO: Analyzing D:\myproject\demo\example2\hello.py 4059 INFO: Processing module hooks... 4060 INFO: Loading module hook "hook-encodings.py"... 4206 INFO: Loading module hook "hook-pydoc.py"... 4207 INFO: Loading module hook "hook-xml.py"... 4440 INFO: Looking for ctypes DLLs 4440 INFO: Analyzing run-time hooks ... 4447 INFO: Looking for dynamic libraries 4583 INFO: Looking for eggs 4583 INFO: Using Python library d:\python37\python37.dll 4583 INFO: Found binding redirects: [] 4586 INFO: Warnings written to D:\myproject\demo\example2\build\hello\warn-hello.txt 4623 INFO: Graph cross-reference written to D:\myproject\demo\example2\build\hello\xref-hello.html 4631 INFO: checking PYZ 4631 INFO: Building PYZ because PYZ-00.toc is non existent 4632 INFO: Building PYZ (ZlibArchive) D:\myproject\demo\example2\build\hello\PYZ-00.pyz 5195 INFO: Building PYZ (ZlibArchive) D:\myproject\demo\example2\build\hello\PYZ-00.pyz completed successfully. 5203 INFO: checking PKG 5204 INFO: Building PKG because PKG-00.toc is non existent 5204 INFO: Building PKG (CArchive) PKG-00.pkg 5220 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 5222 INFO: Bootloader d:\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe 5222 INFO: checking EXE 5222 INFO: Building EXE because EXE-00.toc is non existent 5223 INFO: Building EXE from EXE-00.toc 5224 INFO: Appending archive to EXE D:\myproject\demo\example2\build\hello\hello.exe 5227 INFO: Building EXE from EXE-00.toc completed successfully. 5230 INFO: checking COLLECT 5231 INFO: Building COLLECT because COLLECT-00.toc is non existent 5232 INFO: Building COLLECT COLLECT-00.toc 8582 INFO: Building COLLECT COLLECT-00.toc completed successfully.
-
打包后的目录结构,生成两个目录,一个文件
1. __pycache__ 是python编译产生的文件,可不用关心 2. build 是编译过程中的中间产物 3. dist是最终可执行程序目录 4. spec文件是类似缓存,如果你第二次打包,则需要先把spec删掉,否则第二次打包会受影响
-
执行我们打包好的hello.exe,直接拖到cmd下回车即可
到这里第一种打包方式就介绍完了,比较简单。
-
-
第二种,把谷歌浏览器和第三方库selenium和我们的python程序打包进一个独立的.exe格式的可执行文件。
-
要打包的demo的目录结构
-
打包
$ pyinstaller -F -p selenium -p ChromePortable72\ChromePortable.exe demo.py -i favicon.ico
D:\myproject\demo\example $ pyinstaller -F -p selenium -p ChromePortable72\ChromePortable.exe demo.py -i favicon.ico 56 INFO: PyInstaller: 3.6 57 INFO: Python: 3.7.3 58 INFO: Platform: Windows-10-10.0.18362-SP0 59 INFO: wrote D:\myproject\demo\example\demo.spec 111 INFO: UPX is not available. 112 INFO: Extending PYTHONPATH with paths ['D:\\myproject\\demo\\example', 'D:\\myproject\\demo\\example\\selenium', 'D:\\myproject\\demo\\example\\ChromePortable72\\ChromePortable.exe', 'D:\\myproject\\demo\\example'] 114 INFO: checking Analysis 114 INFO: Building Analysis because Analysis-00.toc is non existent 115 INFO: Initializing module dependency graph... 118 INFO: Caching module graph hooks... 126 INFO: Analyzing base_library.zip ... 3355 INFO: Caching module dependency graph... 3477 INFO: running Analysis Analysis-00.toc 3479 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by d:\python37\python.exe 3876 INFO: Analyzing D:\myproject\demo\example\demo.py 4847 INFO: Processing module hooks... 4848 INFO: Loading module hook "hook-encodings.py"... 5008 INFO: Loading module hook "hook-pydoc.py"... 5009 INFO: Loading module hook "hook-selenium.py"... 5018 INFO: Loading module hook "hook-xml.dom.domreg.py"... 5019 INFO: Loading module hook "hook-xml.py"... 5032 INFO: Looking for ctypes DLLs 5041 INFO: Analyzing run-time hooks ... 5048 INFO: Looking for dynamic libraries 5226 INFO: Looking for eggs 5227 INFO: Using Python library d:\python37\python37.dll 5227 INFO: Found binding redirects: [] 5230 INFO: Warnings written to D:\myproject\demo\example\build\demo\warn-demo.txt 5274 INFO: Graph cross-reference written to D:\myproject\demo\example\build\demo\xref-demo.html 5284 INFO: checking PYZ 5285 INFO: Building PYZ because PYZ-00.toc is non existent 5285 INFO: Building PYZ (ZlibArchive) D:\myproject\demo\example\build\demo\PYZ-00.pyz 6054 INFO: Building PYZ (ZlibArchive) D:\myproject\demo\example\build\demo\PYZ-00.pyz completed successfully. 6065 INFO: checking PKG 6065 INFO: Building PKG because PKG-00.toc is non existent 6065 INFO: Building PKG (CArchive) PKG-00.pkg 7997 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 8000 INFO: Bootloader d:\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe 8001 INFO: checking EXE 8001 INFO: Building EXE because EXE-00.toc is non existent 8001 INFO: Building EXE from EXE-00.toc 8003 INFO: Copying icons from ['favicon.ico'] 8004 INFO: Writing RT_GROUP_ICON 0 resource with 20 bytes 8005 INFO: Writing RT_ICON 1 resource with 67624 bytes 8010 INFO: Updating manifest in D:\myproject\demo\example\build\demo\run.exe.hbxskavy 8011 INFO: Updating resource type 24 name 1 language 0 8015 INFO: Appending archive to EXE D:\myproject\demo\example\dist\demo.exe 8024 INFO: Building EXE from EXE-00.toc completed successfully.
-
打包后demo目录结构
-
看下exe程序小图标是不是自己指定的kpc
-
运行exe
到这里,第二种打包方法也成功了。
-
5. pyinstaller使用注意的点
- pyinstaller也只能在当前操作系统中运行,比如你用mac只能打包出mac上的可执行脚本,要是你想打包出windwos电脑上的可执行程序,你就要用windows执行打包命令。
- 如果你的脚本文件中包含其他脚本,比如hello.py包含自定义脚本(world.py)或是系统脚本(sys.py):则需要在打包的时候加上其他脚本的路径。
通过-p指定第三方包的路径,一条路径对应一个-p
eg:pyinstaller -F -p C:\SystemLib\site-packages -p C:\MyLib Hello.py
- 执行一次打包命令通常会生成两个目录一个附件,分别是build、dist、和xx.spec。build是编译过程中的中间产物,dist是最终可执行程序目录,spec文件是类似缓存,如果你第二次打包,则需要先把spec删掉,否则第二次打包会受影响。