Linux pyinstaller 打包问题 enable-shared

线上服务器的系统基本都是centos 6.x,python 版本是 2.7.5。仅仅是为字符的问题处理上,应该使用python3.x,而不是python2。所以用 pyinstaller 打包 python 3.x 运行的脚本,而不用在线上安装依赖的环境。

wget 下载源码,configure ;make ;make install 三部曲安装。

wget https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tgz
tar -zxvf Python-3.4.0.tgz
cd Python-3.4.0

./configure
make
make install

安装完之后,包管理器 pip3 也有了。如果没有,下载pip的源码使用 python3 setup.py install 来安装。使用 pip3 或者源码来安装 pyinstaller.

使用 pyinstaller 将所有依赖打包到一个文件当中(如果提示 pyinstaller 命令不存在的,需要处理一下环境变量):

pyinstaller --console --onefile script.py

结果报错:

* 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)

重新编译安装,按提示带上参数来生成makefile,然后再make;make install安装。

./configure --enable-shared

编译安装完毕,运行 python3,提示 error while loading shared libraries: 

运行时链接动态库失败,编译时安装该动态库在 (find /usr -name 'libpython3.6m.so.1.0' 搜一下)

/usr/local/lib

目录下。该路径不在连链接器默认的动态库搜索范围里,可以 ldconfig -v | grep python 看不到该动态库。

若该库不在链接器的搜索范围内,执行 ldd $(which python3) 可查看到该库指向为 not found.

        linux-vdso.so.1 =>  (0x00007ffd609e2000)
        libpython3.4m.so.1.0 => not found
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f6e934cf000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f6e932ca000)
        libutil.so.1 => /lib64/libutil.so.1 (0x00007f6e930c7000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f6e92e43000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f6e92aae000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f6e93ba2000)

ldconfig 命令用来管理链接器的动态库搜索路径,默认的是 /lib 和 /usr/lib 已经 /etc/ld.so.conf 配置文件中包含的目录,将 /usr/local/lib 添加到 /etc/ld.so.conf 当中,执行

ldconfig

刷新一下缓存。

此时再运行 python3,使用 pyinstaller 打包也都正常。

另外,使用 ldconfig 来修改系统的动态库的搜索路径,还有一个临时的替代方法就是修改预定义变量:LD_LIBRARY_PATH,然后 export 使得子进程当中能够使用。

Guess you like

Origin blog.csdn.net/zhouguoqionghai/article/details/103102724