Pyinstaller 安装
PyInstaller 是一个跨平台的,用来将 Python 程序打包成一个独立可执行文件的程序。 它的主要优点是PyInstaller可与2.3以来的任何版本的Python兼容,并且使用透明压缩,从而构建了更小的可执行文件,它是完全多平台的,并使用操作系统支持来加载动态库,从而确保了完全的兼容性。
常用的安装方法为:
pip install pyinstaller # 安装最新版本
pip install pyinstaller==2.0 # 安装固定版本
当然,这也是一个开源软件,可以直接下载其开发版:
pip install https://github.com/pyinstaller/pyinstaller/archive/v4.2.zip
如果在安装过程中失败,出现以下错误:“Preparing wheel metadata ... error
”,则可以采用以下解决办法:
- 先安装 wheel:
pip install wheel -i https://pypi.tuna.tsinghua.edu.cn/simple
- 再安装Pyinstaller:
pip install Pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
************************************************************ 小插曲 ************************************************************
pip 的 -i
参数用来临时指定源,常用的几个国内源为:
阿里云 https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) https://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学:https://pypi.mirrors.ustc.edu.cn/simple/
如需永久修改,则可以采用:
Linux:
修改 ~/.pip/pip.conf
(没有就创建一个), 内容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
Windows:
直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,在pip 目录下新建文件pip.ini,内容如下:
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
************************************************************ end ************************************************************
Pyinstaller 使用
语法:pyinstaller 选项 Python源文件
【不管这个 Python 应用是单文件的应用,还是多文件的应用,只要在使用 pyinstaller 命令时编译作为程序入口的 Python 程序即可。】
在cmd下使用命令:pyinstaller -h
即可查看Pyinstaller的所有选项,下面我简单介绍一下常用的几个:
-h,–help | 查看该模块的帮助信息 |
---|---|
-F,–onefile | 产生单个的可执行文件 |
-D,–onedir | 产生一个目录(包含多个文件)作为可执行程序 |
-d,–debug | 产生 debug 版本的可执行文件 |
-w,–windowed,–noconsole | 指定程序运行时不显示命令行窗口(仅对 Windows 有效) |
-c,–nowindowed,–console | 指定使用命令行窗口运行程序(仅对 Windows 有效) |
-o DIR,–out=DIR | 指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件 |
-n NAME,–name=NAME | 指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字 |
-p DIR, –path=DIR | 设置导入路径(和使用PYTHONPATH效果相似).可以用路径分割符(Windows使用分号,Linux使用冒号)分割,指定多个目录.也可以使用多个-p参数来设置多个导入路径 |
如果在使用Pyinstaller将PyQt5程序打包成 exe 文件的过程中出现了:
“ModuleNotFoundError: No module named 'PyQt5.sip'
” 错误,主要是因为 pyqt5与sip版本不匹配造成的。原因找到了,那么解决办法也就有了:
- 方法1:
# 安装旧版本 pyqt5
pip install pyqt5=5.10.1
- 方法2:
# 直接在源码中导入sip,然后重新打包
from PyQt5 import sip
- 方法3:
# 更新 sip 版本 —— 可以去官网根据更新日期的相近程度下载对应版本
pip install sip