作为一名Python开发者,我们经常需要将写好的脚本分享给他人使用。但对方可能没有安装Python环境,这时候就需要将.py文件打包成.exe可执行文件。常见的应用场景包括:
# 安装
pip install pyinstaller
# 打包命令(基础版)
pyinstaller -F -w code-summar-tool.py
# 高级版(添加图标)
pyinstaller -F -w -i app.ico code-summar-tool.py参数说明:
创建setup.py文件:
from cx_Freeze import setup, Executable
setup(
name="代码管理工具",
version="1.0",
description="Python代码管理工具",
executables=[Executable("code-summar-tool.py", base="Win32GUI")]
)执行打包:
python setup.py build# 安装
pip install nuitka
# 打包命令
nuitka --standalone --onefile --windows-disable-console code-summar-tool.py创建setup.py:
from distutils.core import setup
import py2exe
setup(windows=["code-summar-tool.py"])执行打包:
python setup.py py2exe# 安装
pip install auto-py-to-exe
# 启动图形界面
auto-py-to-exeQ1:打包后文件太大怎么办? 使用UPX压缩:
pyinstaller -F -w --upx-dir=upx路径 code-summar-tool.py排除不必要的包:
# 在PyInstaller中
--exclude-module=不需要的模块Q2:如何添加程序图标?
pyinstaller -i app.ico -F code-summar-tool.pyQ3:杀毒软件误报怎么办?
pyinstaller --key=yourpassword -F code-summar-tool.py你在打包Python程序时遇到过什么问题?欢迎在评论区留言讨论!