本文共 1977 字,大约阅读时间需要 6 分钟。
安装Pyinstaller是将Python脚本打包为可执行文件的利器。以下是安装和使用方法,解决常见问题也包括在内。
which python
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
注意: Replace https://pypi.tuna.tsinghua.edu.cn/simple
with your preferred镜像地址。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
在项目根目录下打开终端,执行如下命令:
pyinstaller -F -w "your_script.py"
-F
:打包为单一文件。-w
:隐藏终端窗口,生成无界面程序。对于图标,添加--icon
选项:
pyinstaller -F -w "your_script.py" --icon "path/to/icon.ico"
放在项目根目录下创建pyinstaller_config.txt
,修改配置:
echo '# -*- coding: utf-8 -*-' > pyinstaller_config.txtecho '__author__ = "your_name"' >> pyinstaller_config.txtecho ' Oy Ran: $(date +%Y%m%d)' >> pyinstaller_config.txtecho ' script_name = "your_script.py"' >> pyinstaller_config.txtecho ' icon_path = "path/to/icon.ico"' >> pyinstaller_config.txtecho ' upto압缩= True' >> pyinstaller_config.txt # 是否启用UPX压缩
添加过滤器,可以使用runpy_to_unpack
,更灵活地管理打包文件:
# -*- coding: utf-8 -*-"""打包自动化脚本"""import runpy_to_unpack#编辑此处添加打包参数r = '你的程序路径'target_path = 'dist'#运行打包脚本runpy_to_unpack.run(r, target_path=target_path, associational_files=[r"your_icon.ico"])
错误信息:
UnicodeDecodeError: 'utf-8' codec can't decode byte...
解决方法:
错误信息:
ModuleNotFoundError: No module named 'PyQt5'
解决方法:
pyinstaller -F -w "your_script.py" -p "绝对路径"
PATH
包含Python解释型的Python版本。requirements.txt
记录所需库,打包时可选择继承或重新安装。希望这些方法能帮助您顺利打包并分发Python程序!如有其他问题欢迎讨论。
转载地址:http://lrvgz.baihongyu.com/