centos6.6安裝pyinstaller的過程及errror:shared libraries解決

1.本機centos6.6安裝python3的方式:
python3.6.5的源碼編譯安裝

# 準備編譯環境
yum groupinstall 'Development Tools'
yum install zlib-devel bzip2-devel  openssl-devel ncurses-devel
# 創建安裝目錄
mkdir /usr/local/python3
# 解壓
tar -xvJf Python-3.6.5.tar.xz
# 編譯安裝
cd Python-3.6.5
./configure --prefix=/usr/local/python3 --enable-optimizations
make
make install
# 是以優化的方式安裝python3
--enable-optimizations

2.pyinstaller包的安裝及異常處理

# 安裝pyinstaller包
pip install pyinstaller

pyinstaller打包的demo示例:

# 創建目錄
mkdir demo
# 進入目錄
cd demo
# 創建demo
vi demo.py
# 寫入內容,保存退出
name = input("請輸入您的姓名:")
print(name)

打包

# 在demo目錄下執行 打包爲一個可執行文件
pyinstaller -F demo.py

參數的詳情可以參考官方文檔,地址:https://pyinstaller.readthedocs.io/en/stable/usage.html
pyinstaller打包的過程中異常的代碼:

35 INFO: PyInstaller: 3.5
35 INFO: Python: 3.6.5
36 INFO: Platform: Linux-4.4.0-142-generic-x86_64-with-centos-6.10-Final
37 INFO: wrote /root/test.spec
39 INFO: UPX is not available.
40 INFO: Extending PYTHONPATH with paths
['/root', '/root']
40 INFO: checking Analysis
40 INFO: Building Analysis because Analysis-00.toc is non existent
40 INFO: Initializing module dependency graph...
41 INFO: Initializing module graph hooks...
43 INFO: Analyzing base_library.zip ...
2846 INFO: running Analysis Analysis-00.toc
2868 INFO: Caching module hooks...
2872 INFO: Analyzing /root/test.py
2875 INFO: Loading module hooks...
2875 INFO: Loading module hook "hook-pydoc.py"...
2876 INFO: Loading module hook "hook-encodings.py"...
2976 INFO: Loading module hook "hook-xml.py"...
3191 INFO: Looking for ctypes DLLs
3191 INFO: Analyzing run-time hooks ...
3197 INFO: Looking for dynamic libraries
3577 INFO: Looking for eggs
3577 INFO: Python library not in binary dependencies. Doing additional searching...
Traceback (most recent call last):
  File "/usr/local/python3/bin/pyinstaller", line 8, in <module>
    sys.exit(run())
  File "/usr/local/python3/lib/python3.6/site-packages/PyInstaller/__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "/usr/local/python3/lib/python3.6/site-packages/PyInstaller/__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "/usr/local/python3/lib/python3.6/site-packages/PyInstaller/building/build_main.py", line 844, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "/usr/local/python3/lib/python3.6/site-packages/PyInstaller/building/build_main.py", line 791, in build
    exec(code, spec_namespace)
  File "/root/test.spec", line 17, in <module>
    noarchive=False)
  File "/usr/local/python3/lib/python3.6/site-packages/PyInstaller/building/build_main.py", line 243, in __init__
    self.__postinit__()
  File "/usr/local/python3/lib/python3.6/site-packages/PyInstaller/building/datastruct.py", line 158, in __postinit__
    self.assemble()
  File "/usr/local/python3/lib/python3.6/site-packages/PyInstaller/building/build_main.py", line 575, in assemble
    self._check_python_library(self.binaries)
  File "/usr/local/python3/lib/python3.6/site-packages/PyInstaller/building/build_main.py", line 681, in _check_python_library
    raise IOError(msg)
OSError: Python library not found: libpython3.6mu.so.1.0, libpython3.6m.so.1.0, libpython3.6.so.1.0, libpython3.6m.so
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)

異常問題:從異常代碼中可以看出OSError: Python library not found,異常主要是自己安裝Python過程中沒有設置Python的庫共享,pyinstaller打包過程依賴缺失。

解決方案:如下截圖所示,需要重新對Python安裝包進行編譯安裝
在這裏插入圖片描述
Python重新配置,編譯安裝

# 進入Python-3.6.5的目錄 重新設置配置
./configure --prefix=/usr/local/python3 --enable-shared
# 編譯安裝
make
make install

在python3.6.5的目錄下可以看到生成的庫文件:
在這裏插入圖片描述
測試Python是否安裝成功:

# 執行命令 進入交互環境
python3

可能會出現以下異常,需要複製共享庫到相應的目錄

python3: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory

解決方案

# 查看python3動態鏈接庫
ldd /usr/bin/python3
# 沒有libpython3.6m.so.1.0則,進入Python-3.6.5 目錄進行復制文件
cp libpython3.6m.so.1.0 /usr/local/lib64/
cp libpython3.6m.so.1.0 /usr/lib
cp libpython3.6m.so.1.0 /usr/lib64/

異常解決之後重新打包,如下圖可以看出,成功加載python的庫,exe文件也打包成功:
在這裏插入圖片描述
生成的新文件,進入dist目錄,可以看到demo的可執行文件:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章