使用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目录下