各種環境各種坑,一步一搜索,終於解決了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刷新