Python打包方法——Pyinstaller CentOS下踩坑记录
各种环境各种坑,一步一搜索,终于解决了CentOS下使用的问题,记录一下
安装,参考https://www.cnblogs.com/gopythoner/p/6337543.html
windows10, 很容易,直接pip搞定
pip install pywin32
pip install PyInstaller
试一下。
创建一个python文件
from flask import Flask from flask import request app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def home(): return 'test' if __name__ == '__main__': app.run()
打开cmd,转到文件路径
pyinstaller -F app.py
虽然用了flask和numpy,但是并没有出错,不需要像文章中说的那样复制package目录到文件夹,直接就打包好了。
dist目录下生成了app.exe,居然有212MB。打开,没问题。复制到同事电脑,没有python环境也可以执行。很好。
服务器是linux,先到虚拟机的CentOS上试一下吧
安装还是很简单
pip install pyinstaller
执行以下试试,报错:pyinstaller: command not found
安装没问题啊
查到这篇文章https://blog.csdn.net/qq_35614920/article/details/77404323
要不试试源文件安装,官网地址
解压缩
tar -xzv PyInstaller-3.4.tar.gz
解压完成后进入文件夹执行
python setup.py install
再打包试一下,还是报错:pyinstaller: command not found
继续搜:https://superuser.com/questions/1310800/pyinstaller-command-not-found
这是因为pyinstaller没有被放到/usr/bin目录下,需要从python目录下复制过去
cp /usr/local/python36/bin/pyinstaller /usr/bin/pyinstaller
再来,居然有新的错误:
OSError: Python library not found: libpython3.6m.so.1.0, libpython3.6mu.so.1.0, libpython3.6.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)
这个兄弟看起来尝试了不少方案 pyinstaller编译python脚本为单文件可执行文件遇到的问题
安装python3-dev,python-dev。这是对ubuntu系统的,CentOS不需要
文章里提到stackoverflow的一个方案
1st. check your system if it has libpython3.4m.so.1.0. If yes, go to step 2nd. If no, download it(I'm using anaconda python, so I have it in anaconda folder.)
2nd. sudo cp /folder/to/your/libpython3.4m.so.1.0 /usr/lib
照着样子找一下,居然找不到
find / -name libpython3.6mu.so.1.0
继续找为什么系统没有这个文件。https://www.cnblogs.com/Tommy-Yu/p/6144512.html
python是自己装的,在安装之前指定设置的时候,需要指定--enable-shared参数
When running configure, you should be supplying the --enable-shared option to ensure that shared libraries are built for Python. By not doing this you are preventing any application which wants to use Python as an embedded environment from working.
好吧,重新编译python,顺便打开ssl,避免以后掉坑
./configure --prefix=/usr/local/python36 --enable-shared --with-ssl
make
make install
回头仔细看看之前的报错信息,里面已经说过了:
If you're building Python by yourself, please rebuild your Python with `--enable-shared` (or, `--enable-framework` on Darwin)
python重新装好了,再试一下打包
pyinstaller -F app.py
居然还是报找不到so,http://www.cnblogs.com/trasin/p/6212124.html
查看动态库情况
ldd /usr/local/python36/bin/python3
把需要的.so复制到lib目录
cp libpython3.6m.so.1.0 /usr/lib64/
再次打包,这下可以了。这种环境问题真是够烦的
pyinstaller -F app.py
pyinstaller打包报错for real_module_name, six_moduleAttributeError: 'str' object has no attribute 'items'
https://stackoverflow.com/questions/35613300/pyinstaller-compile-to-exe
更新setuptools
pip install -U --pre setuptools
其他文章说的一些解决办法,当然,对我的问题没有效果
http://www.pianshen.com/article/423794883/
在/etc/ld.so.conf中添加
/usr/local/lib64
/usr/local/lib
然后ldconfig刷新