原创文|Space9
Python文件打包成可安装、无需Python依赖的高效可执行exe程序
工具及环境
- PyInstaller
- Inno Setup
- Windows和Python
PyInstaller打包Python应用程序为独立的可执行文件
安装PyInstaller
pypi 镜像使用帮助https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
PyInstaller打包python程序
pyinstaller -D -w -i "resources\logo.ico" AudioConverter.py Amusic.py --add-data "resources\7z.dll;." --add-data "resources\7z.exe;." --add-data "resources\aria2c.exe;." --add-data "resources\logo.ico;."
参数解释:
-D 创建一个包含可执行文件的单文件夹捆绑包(不推荐-F单文件形式,原因:单文件每次启动前需要解压操作,会拖慢启动速度)
-w 不显示控制台窗口,GUI可视化程序需要此项
-i 指定可执行文件的图标
-n 指定应用程序包的名称(默认值:第一个脚本的名称)
–add-data <SRC;DEST or SRC:DEST> 添加资源文件(Windows使用;分号,大多数Unix使用:冒号,注意后面还有一个.点)
其他参数可查阅官方文档https://pyinstaller.readthedocs.io/en/stable/
生成文件在dist目录下
兼容性问题
出现运行异常兼容性问题,请尝试PyInstaller开发版本
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz
高速访问GitHub
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple https://git.yumenaka.net/github.com/pyinstaller/pyinstaller/archive/develop.tar.gz