windows版本
1、需要安装pyinstaller,通过以下命令:
通过pip install pyinstaller
其中pyinstaller对python3.6不兼容问题,但是在打包时会出现问题。解决办法是升级python为3.7版本,python3.7支持pyinstaller
2、安装好了pyinstaller后,通过命令pyinstaller -F xx1.py xx2.py。生成和第一个py文件相同名的exe文件,其中如果出现依赖的py文件,需要添加py文件到到第一个文件之后。
Linux版本
1、同样,通过如下命令安装pyinstaller
pip install pyinstaller
2、在通过pyinstaller -F xx.py编译程序的时候,出现如下错误:
OSError: Python library not found: libpython3.7.so.1.0, libpython3.7m.so.1.0, libpython3.7mu.so.1.0
This would mean your Python installation doesn't come with proper library files.
This usually happens by missing development package, or unsuitable build parameters of Python installation.
* On Debian/Ubuntu, you would need to install Python development packages
* apt-get install python3-dev
* apt-get install python-dev
* If you're building Python by yourself, please rebuild your Python with `--enable-shared` (or, `--enable-framework` on Darwin)
提示的解决错误的方法是安装python3-dev(这是在Debian/Ubuntu),而Centos下是通过yum install python3-devel安装。其实最终解决该问题可以通过如下命令:
sudo cp /....../libpython3.7.so.1.0 /usr/lib
pyinstaller打包中遇到的问题:
遇到了no module named typedefs
这个问题解决的时候需要在刚才的命令后面加
--hidden-import sklearn.neighbors.typedefs
pyinstaller打包报错: RecursionError: maximum recursion depth exceeded
1)pyinstaller -F xxx.py
这一步会报下述错误,会产生一个xxx.spec文件
File "d:\programdata\anaconda3\lib\ast.py", line 253, in visit
return visitor(node)
File "d:\programdata\anaconda3\lib\ast.py", line 261, in generic_visit
self.visit(item)
File "d:\programdata\anaconda3\lib\ast.py", line 253, in visit
return visitor(node)
File "d:\programdata\anaconda3\lib\ast.py", line 263, in generic_visit
self.visit(value)
File "d:\programdata\anaconda3\lib\ast.py", line 253, in visit
return visitor(node)
RecursionError: maximum recursion depth exceeded
2)在xxx.spec文件中增加两行(添加在原文件第二行):
import sys
sys.setrecursionlimit(5000)
3)pyinstaller xxx.spec
pyinstaller打包遇到的问题:ImportError: The ‘packaging’ package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.
解决办法:
pip uninstall setuptools
pip install setuptools==19.2
pyinstaller打包遇到问题:_tkinter.TclError: no display name and no $DISPLAY environment variable
解决办法:
import matplotlib
matplotlib.use('Agg')