丽爷笑一个 發表於 2019-5-28 15:53:00

Python打包方法——Pyinstaller CentOS下踩坑记录

<p>各种环境各种坑,一步一搜索,终于解决了CentOS下使用的问题,记录一下</p>
<p>安装,参考https://www.cnblogs.com/gopythoner/p/6337543.html</p>
<p>windows10, 很容易,直接pip搞定</p>
<div class="cnblogs_code">
<pre><span>pip install pywin32
pip install PyInstaller</span></pre>
</div>
<p>试一下。</p>
<p>创建一个python文件</p>
<div class="cnblogs_Highlighter">
<pre class="brush:python;gutter:true;">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()
</pre>
</div>
<p>&nbsp;</p>
<p>打开cmd,转到文件路径</p>
<div class="cnblogs_code">
<pre>pyinstaller -F app.py</pre>
</div>
<p>虽然用了flask和numpy,但是并没有出错,不需要像文章中说的那样复制package目录到文件夹,直接就打包好了。</p>
<p>dist目录下生成了app.exe,居然有212MB。打开,没问题。复制到同事电脑,没有python环境也可以执行。很好。</p>
<p>&nbsp;</p>
<p>服务器是linux,先到虚拟机的CentOS上试一下吧</p>
<p>安装还是很简单</p>
<div class="cnblogs_code">
<pre>pip install pyinstaller</pre>
</div>
<p>执行以下试试,报错:pyinstaller: command not found</p>
<p>安装没问题啊</p>
<p>查到这篇文章https://blog.csdn.net/qq_35614920/article/details/77404323</p>
<p>要不试试源文件安装,官网地址</p>
<p>解压缩</p>
<div class="cnblogs_code">
<pre>tar -xzv PyInstaller-3.4.tar.gz</pre>
</div>
<p>解压完成后进入文件夹执行</p>
<div class="cnblogs_code">
<pre>python setup.py install</pre>
</div>
<p>再打包试一下,还是报错:pyinstaller: command not found</p>
<p>&nbsp;</p>
<p>继续搜:https://superuser.com/questions/1310800/pyinstaller-command-not-found</p>
<p>这是因为pyinstaller没有被放到/usr/bin目录下,需要从python目录下复制过去</p>
<div class="cnblogs_code">
<pre>cp /usr/local/python36/bin/pyinstaller /usr/bin/pyinstaller</pre>
</div>
<p>&nbsp;</p>
<p>再来,居然有新的错误:</p>
<div class="cnblogs_code">
<pre>OSError: Python library not found: libpython3.6m.so.1.0, libpython3.6mu.so.1.0, libpython3.6.so.1.0<span>
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/<span>Ubuntu, you would need to install Python development packages
* apt-get install python3-<span>dev
* apt-get install python-<span>dev
* If you're building Python by yourself, please rebuild your Python with `--enable-shared` (or, `--enable-framework` on Darwin)</span></span></span></span></pre>
</div>
<p>&nbsp;</p>
<p>这个兄弟看起来尝试了不少方案&nbsp;pyinstaller编译python脚本为单文件可执行文件遇到的问题</p>
<p>安装python3-dev,python-dev。这是对ubuntu系统的,CentOS不需要</p>
<p>文章里提到stackoverflow的一个方案</p>
<p>https://stackoverflow.com/questions/43067039/pyinstaller-error-oserror-python-library-not-found-libpython3-4mu-so-1-0-lib</p>
<p>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.)<br>2nd. sudo cp /folder/to/your/libpython3.4m.so.1.0 /usr/lib</p>
<p>照着样子找一下,居然找不到</p>
<div class="cnblogs_code">
<pre>find / -name libpython3.6mu.so.1.0</pre>
</div>
<p>&nbsp;</p>
<p>继续找为什么系统没有这个文件。https://www.cnblogs.com/Tommy-Yu/p/6144512.html</p>
<p>python是自己装的,在安装之前指定设置的时候,需要指定--enable-shared参数</p>
<p>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.&nbsp;</p>
<p>好吧,重新编译python,顺便打开ssl,避免以后掉坑</p>
<div class="cnblogs_code">
<pre>./configure --prefix=/usr/local/python36 --enable-<span>shared&nbsp;--with-ssl
make
make install</span></pre>
</div>
<p>回头仔细看看之前的报错信息,里面已经说过了:</p>
<p>If you're building Python by yourself, please rebuild your Python with `--enable-shared` (or, `--enable-framework` on Darwin)</p>
<p>&nbsp;</p>
<p>python重新装好了,再试一下打包</p>
<div class="cnblogs_code">
<pre>pyinstaller -F app.py</pre>
</div>
<p>居然还是报找不到so,http://www.cnblogs.com/trasin/p/6212124.html</p>
<p>查看动态库情况</p>
<div class="cnblogs_code">
<pre>ldd /usr/local/python36/bin/python3</pre>
</div>
<p>把需要的.so复制到lib目录</p>
<div class="cnblogs_code">
<pre>cp libpython3.6m.so.1.0 /usr/lib64/</pre>
</div>
<p>&nbsp;</p>
<p>再次打包,这下可以了。这种环境问题真是够烦的</p>
<div class="cnblogs_code">
<pre>pyinstaller -F app.py</pre>
</div>
<p>&nbsp;</p>
<p>pyinstaller打包报错for real_module_name, six_moduleAttributeError: 'str' object has no attribute 'items'</p>
<p>https://stackoverflow.com/questions/35613300/pyinstaller-compile-to-exe<br>更新setuptools</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">pip install -U --pre setuptools
</pre>
</div>
<p>  </p>
<p><br>其他文章说的一些解决办法,当然,对我的问题没有效果</p>
<p>http://www.pianshen.com/article/423794883/</p>
<p>在/etc/ld.so.conf中添加</p>
<p>/usr/local/lib64<br>/usr/local/lib</p>
<p>然后ldconfig刷新</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/jerryzh/p/10937905.html
頁: [1]
查看完整版本: Python打包方法——Pyinstaller CentOS下踩坑记录