Python程序打包全攻略

使用PyInstaller打包Python程序

安装PyInstaller:

bash 复制代码
pip install pyinstaller

打包单个文件:

bash 复制代码
pyinstaller --onefile your_script.py

打包带有图标的程序:

bash 复制代码
pyinstaller --onefile --icon=your_icon.ico your_script.py

使用cx_Freeze打包Python程序

安装cx_Freeze:

bash 复制代码
pip install cx_Freeze

创建setup.py文件:

python 复制代码
from cx_Freeze import setup, Executable

setup(
    name="YourApp",
    version="0.1",
    description="Your Application Description",
    executables=[Executable("your_script.py")]
)

执行打包命令:

bash 复制代码
python setup.py build

使用Py2exe打包Python程序(仅Windows)

安装py2exe:

bash 复制代码
pip install py2exe

创建setup.py文件:

python 复制代码
from distutils.core import setup
import py2exe

setup(console=['your_script.py'])

执行打包命令:

bash 复制代码
python setup.py py2exe

打包注意事项

确保所有依赖项都已正确安装,可以使用:

bash 复制代码
pip freeze > requirements.txt

对于图形界面程序,可能需要添加--noconsole选项:

bash 复制代码
pyinstaller --noconsole --onefile your_script.py

处理数据文件需要额外配置,在PyInstaller中使用--add-data选项:

bash 复制代码
pyinstaller --add-data="data/*;data" your_script.py

打包结果位置

PyInstaller生成的打包文件位于dist目录下 cx_Freeze生成的打包文件位于build目录下 py2exe生成的打包文件位于dist目录下

相关推荐
小羊没烦恼!2 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
森叶3 天前
用远程调试端口自动化 Chrome 应用商店发布:做法、三个真坑,以及你必须知道的风险
自动化
懂软件的胡子个哥3 天前
微信机器人为什么需要“回复候选”而不是所有 AI 内容直接发送
运维·微信·自动化·wechatapi·个人微信号二次开发
jingli93 天前
多平台多店铺客服怎么统一接待?千牛/拼多多/抖音/京东/闲鱼/微信/快手切窗口太累,一个后台集中接管的七步落地方案(2026 实操版)
人工智能·自动化·用户运营
坚定信念,勇往无前3 天前
Playwright 的本质
自动化·网络爬虫
Am-Chestnuts3 天前
DeepSeek表格复制到Word变竖线错列?用DS随心转对比几种整理方法
人工智能·word·ds随心转