一、安装Pyinstaller
命令窗口执行:
pip install pyinstaller
如果下载太慢,可以通过镜像安装
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
其他可供替换的国内安装源
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/
阿里云 https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) https://pypi.douban.com/simple/
二、使用pyinstaller封装脚本中遇到的问题及方案
使用时发现简单脚本可正常执行,但是 出现 "import pandas"时就会出各种错误
(一)执行 pyinstaller -F xxxxx.py
其中: -F 代表封装成一个执行文件,适合代码只写在一个.py文件中,其他具体的参数含义可以参考这位大佬的文章:https://www.jb51.net/article/206825.htm
xxxxx.py 代表需要封装的python脚本
执行上述命令后,虽然报错,无法生成对应的exe文件,但是在.py文件同目录下会有一个.spec文件
(二)接下来介绍执行过程中遇到的各种坑(都是血泪)
1. UPX is not available
看其他大佬分享,说是由于 pyinstaller 打包使用的是 UPX,打包时没找到这个应用才报错
解决方案:
(1)官网下载UPX :https://upx.github.io
(2) 下载符合电脑系统的版本,例如我是【windows64位操作系统】
(3)下载后,解压,复制upx.exe到pyinstaller的安装目录下
(5)重新执行 pyinstaller -F xxxxx.py 不会再报 【UPX is not available】
2. AttributeError: module 'io' has no attribute 'OpenWrapper'
Current thread 0x00004f5c (most recent call first):pre-safe-import-module hook failed, needs fixing.
升级了xlwriter 后就解决了
pip install XlsxWriter --upgrade --ignore-installed XlsxWriter
3. RuntimeError: No metadata path found for distribution 'greenlet'.
pip install --ignore-installed greenlet
如果出现warning,提示某些文件已经存在,要从对应的地址中删除产生warning的文件夹,重新执行一次,确保 “greenlet”安装成功,例如这些文件:
参考文档: [pyinstaller] “RuntimeError: No metadata path found for distribution ‘greenlet‘
4. moduleNotFoundError: No module named 'pandas. libs.tslibs.base
(1)打开第一次执行生成的spec文件
(2)在a=Analysis()下的 hiddenimports模块加入 'pandas. libs.tslibs.base'
(3)命令窗口执行 pyinstaller -F xxxxx.spec --onefile
注意:改完spec文件后,以后执行打包语句都要使用.spec文件而不是原来的.py文件,否则会生成新的.spec文件覆盖更改过的spec文件(一句话,就是你白改了)
5. ModuleNotFoundError: No module named 'numpy.random.common
这是numpy的版本问题,执行一下安装 1.16.2的版本就可以解决
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U numpy==1.16.2
6. Unable to find “D:\xxxxx\Lib\site-packages\setuptools-27.2.0-py3.6.egg\EGG-INFO“
我看了一下:我是有.egg这个文件的,但是点了之后没有反应,网上查了一下才知道这是个压缩包,需要解压,进入该文件所在地址,在命令窗口执行
unzip setuptools-27.2.0-py3.6-zip.egg -d setuptools-27.2.0-py3.6.egg
注意点:
-d 后边是解压后的文件夹名称,前边是原始的egg压缩包,解压后的文件夹不能与之重名,所以我更改了原始的egg压缩包名称,加上了'-zip'
如果在命令窗口执行"unzip"的时候喜提报错 unzip”不是内部或外部命令,也不是可运行的程序或批处理文件,则去UnZip for Windows下载“Binaries”对应压缩包,并将解压后的unzip.exe文件放到C:\Windows\System32下
迈过了重重困难后
在 disk文件夹中遇到可以找到该程序
将该程序单独发给其他人,即使没有安装python也可以正常执行,返回结果~~~