使用pyinstaller将python文件打包成exe程序,打包步骤如下:
一、安装pyinstaller
(1)win+R输入cmd,打开命令窗口
(2)安装pyinstaller,安装指令:pip install pyinstaller
二、打包
1,切换到打包程序目录
例:需要打包程序目录为:D:\automation\autotest_tool\interface_param_change_tool
切换指令:cd D:\automation\autotest_tool\interface_param_change_tool
2,打包文件,pyinstaller -F xxx.py(xxx.py,打包的文件)
例,打包文件为:runer.py,
-D 参数是生成所有链接库的文件夹, -w参数是隐藏命令行窗口(如pygame)
3,到打包文件目录查看打包好的程序,并执行
(1)打包成功后项目中新增dist文件
(2)进入dist文件,点击运行打包好的exe程序
如果引用了图象等资源文件夹, 要拷贝到发布目录下.
注1: 执行pyinstaller出错:
pyinstall : 无法将“pyinstall”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ pyinstall -F .\alien_invasion.py
+ ~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (pyinstall:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
最后有个警告说
WARNING: The scripts pyi-archive_viewer.exe, pyi-bindepend.exe, pyi-grab_version.exe, pyi-makespec.exe, pyi-set_version.exe and pyinstaller.exe are installed in ‘C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pyinstaller-4.0 pyinstaller-hooks-contrib-2020.10 pywin32-ctypes-0.2.0
就是没有设置该执行目录到系统环境参数, 可以进到该目录执行pyinstaller
举例,执行:
C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts\pyinstaller -F .\alien_invasion.py
成功.
注2: 管理员权限运行Windows PowerShell
from:https://www.cnblogs.com/mini-monkey/p/11195309.html
注3:报错 win32ctypes.pywin32.pywintypes.error:(1920,“ LoadLibraryExW”,“系统无法访问该档案。”)
https://github.com/pyinstaller/pyinstaller/issues/4507
解决方法1:
# 第一个地方
# 根据错误信息提示找到"win32api.py"文件
# 修改大约40行
def LoadLibraryEx(fileName, handle, flags):
# 添加这一行,改为你的python路径
fileName = r"C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\python.exe"
if not handle == 0:
raise ValueError("handle != 0 not supported")
with _pywin32error():
return _dll._LoadLibraryEx(fileName, 0, flags)
def EnumResourceTypes(hModule):
# 第二个地方
# 修改完第一个错误运行会报第二个同样的错误
# 根据错误提示修改"pefile.py"文件
# 修改大约1795行
def __parse__(self, fname, data, fast_load):
"""Parse a Portable Executable file.
Loads a PE file, parsing all its structures and making them available
through the instance's attributes.
"""
# 添加这一行,同样改为你的python路径
fname = r"C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\python.exe"
if fname is not None:
stat = os.stat(fname)
if stat.st_size == 0:
raise PEFormatError('The file is empty')
fd = None
解决方法2:在模拟环境下执行
http://www.dreamflier.net/wordpress/?p=83
解决方法3:一般是因为直接用了windows 应用市场安装的python, 先在市场中卸载,然后在官网https://www.python.org/downloads/下载安装.