各种环境各种坑,一步一搜索,终于解决了CentOS下使用的问题,记录一下
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
安装没问题啊
要不试试源文件安装,官网地址
解压缩
tar -xzv PyInstaller-3.4.tar.gz
解压完成后进入文件夹执行
python setup.py install
再打包试一下,还是报错: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.0This 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,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
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
查看动态库情况
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
其他文章说的一些解决办法,当然,对我的问题没有效果
在/etc/ld.so.conf中添加
/usr/local/lib64
/usr/local/lib
然后ldconfig刷新