如何打包Python为exe程序
要将Python脚本打包为exe程序,可以使用PyInstaller或cx_Freeze等第三方库。
使用PyInstaller:
- 安装PyInstaller库
pip install pyinstaller
- 在命令行窗口中切换到Python脚本所在的目录
cd /path/to/your/script
- 运行PyInstaller命令生成exe文件
pyinstaller your_script.py
- 在dist目录下找到生成的exe文件
使用cx_Freeze:
- 安装cx_Freeze库
pip install cx_Freeze
- 在Python脚本中添加setup.py文件
from cx_Freeze import setup, Executable
setup(name='your_script',
version='0.1',
description='Description',
executables=[Executable('your_script.py')])
- 运行setup.py文件生成exe文件
python setup.py build
- 在build目录下找到生成的exe文件
注意事项:
- 打包时需要保证Python环境和依赖库的版本与开发环境一致;
- 如果Python脚本使用了GUI库如Tkinter,需要在打包时指定GUI模块;
- 打包时可以添加其他选项如图标、版本信息等。
原文地址: https://www.cveoy.top/t/topic/l06 著作权归作者所有。请勿转载和采集!