Pyinstaller打包exe完整教程

原创文|Space9

Python文件打包成可安装、无需Python依赖的高效可执行exe程序

工具及环境

  1. PyInstaller
  2. Inno Setup
  3. 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 添加资源文件(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

程序64位和32位的问题

  1. 32位程序在64位和32位操作系统下都可以运行,64位程序只能在64位操作系统下运行
  2. 使用32位Python打包32位程序,使用64位Python打包64位程序

Inno Setup制作Windows程序安装包

安装Inno Setup

稳定版下载地址:https://jrsoftware.org/download.php/is.exe
测试版下载地址:https://jrsoftware.org/download.php/is-beta.exe
下载后桌面双击,即可开始安装

安装界面语言支持中文

简体中文语言包下载地址:https://raw.github.com/jrsoftware/issrc/main/Files/Languages/Unofficial/ChineseSimplified.isl
高速访问GitHub:https://cdn.jsdelivr.net/gh/jrsoftware/issrc@main/Files/Languages/Unofficial/ChineseSimplified.isl
其他语言包:https://jrsoftware.org/files/istrans/
下载语言包之后,将语言包文件放入Inno Setup安装目录里的Languages目录中

使用向导生成打包脚本

使用向导生成打包安装程序的脚本,并保存
参考脚本
setup.iss

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "嗖音HOME"
#define MyAppVersion "3.2.0"
#define MyAppPublisher "独居者"
#define MyAppExeName "AudioConverter.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{9DB7D8F0-D497-4AD8-A331-D3E74FD2C0DC}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
; Remove the following line to run in administrative install mode (install for all users.)
PrivilegesRequired=lowest
OutputDir=.\outfile
OutputBaseFilename=嗖音HOME
SetupIconFile=.\resources\logo.ico
UninstallDisplayIcon=.\resources\logo.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "chinesesimplified"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone

[Files]
Source: ".\dist\AudioConverter\AudioConverter.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: ".\dist\AudioConverter\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

[UninstallDelete]
Type: files; Name: "{app}\ffmpeg.exe"
Type: filesandordirs; Name: "{app}\WAV"
Type: files; Name: "{app}\cookie.json"

参数解释:
UninstallDisplayIcon=.\resources\logo.ico 设置卸载程序的图标
Flags: checkablealone 设置创建桌面快捷方式默认勾选状态

卸载删除文件或文件夹
[UninstallDelete]
Type: files; Name: “{app}\ffmpeg.exe”
Type: filesandordirs; Name: “{app}\WAV”

自动化构建脚本

实现的可行性

  1. Inno Setup安装目录的文件可以拷贝到任意地方使用cmd命令行形式进行调用
  2. 按照如图所示目录结构修改相关的脚本路径(绝对路径修改为相对路径
    Pyinstaller打包exe完整教程_第1张图片
  3. 之后就可以直接双击build.bat进行一键打包工作,安装包就会保存在outfile目录

实现步骤描述

  1. 安装pyinstaller和其他第三方依赖包
  2. 执行pyinstaller进行打包
  3. 使用Inno Setup制作安装包

具体实现脚本

build.bat

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

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;."

cd Inno Setup 6
iscc "..\setup.iss"

扩展

使用Github管理代码,并通过CI / CD工具轻松实现软件构建的自动化

示例中所用源码

您可以Star和Fork我的项目
https://github.com/space9bug/AudioConverter

本文仅为个人学习使用,不得用于任何商业用途,否则后果自负!如侵犯到您的权益,请及时通知我,我会及时处理。

Pyinstaller打包exe完整教程_第2张图片

你可能感兴趣的:(Pyinstaller打包exe完整教程)