Python升级

第1章 Python升级

1.1 Python 2.6升级到2.7

1.1.1 升级到2.7

1.1.1.1 安装依赖库

[root@cobbler ~]# yum install -y sqlite-devel zlib-devel openssl-devel

1.1.1.2 解压Python2.7

[root@cobbler ~]# cd /server/tools/
[root@cobbler tools]# wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
[root@cobbler tools]# unxz Python-2.7.14.tar.xz
[root@cobbler tools]# tar xf Python-2.7.14.tar

1.1.1.3 配置编译安装Python2.7

[root@cobbler tools]# cd Python-2.7.14
[root@cobbler Python-2.7.14]# ./configure --enable-shared --enable-loadable-sqlite-extensions --with-zlib
[root@cobbler Python-2.7.14]# vim ./Modules/Setup
467 zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz  # 去掉注释
[root@cobbler Python-2.7.14]# make && make install

1.1.1.4 更新系统Python版本

[root@cobbler ~]# mv /usr/bin/python /usr/bin/python2.6.6
[root@cobbler ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python

1.1.1.5 更改yum源使用的Python版本

[root@cobbler ~]# vim /usr/bin/yum
#!/usr/bin/python
改为
#!/usr/bin/python2.6.6

1.1.1.6 查看Python版本

[root@cobbler ~]# python -V

 

1.1.2 Python2.7安装pip

1.1.2.1 安装pip

[root@cobbler ~]# wget https://bootstrap.pypa.io/get-pip.py
[root@cobbler ~]# python get-pip.py

1.1.2.2 创建软链接

[root@cobbler ~]# ln -s /usr/local/bin/pip2.7 /usr/bin/pip

1.1.3 Python2.7安装django

[root@cobbler ~]# pip install django==1.6.9     # 后面数字为django版本信息

1.2 Python 2.6升级到3.x

1.2.1 升级到3.x

1.2.1.1 安装依赖包

[root@m01 ~]# yum install -y gcc libbz2 readline-devel sqlite-devel zlib-devel zlib-devel openssl-devel libffi-devel

1.2.1.2 下载python安装包

[root@m01 ~]# wget -P /server/tools https://www.python.org/ftp/python/3.7.0/Python-3.7.0a1.tgz

1.2.1.3 配置编译安装python3.x

[root@m01 ~]# cd /server/tools/
[root@m01 tools]# tar xf Python-3.7.0a1.tgz
[root@m01 tools]# cd Python-3.7.0a1
[root@m01 Python-3.7.0a1]# ./configure --prefix=/usr/local/python-3.7.0a1 --enable-optimizations
[root@m01 Python-3.7.0a1]# make && make install

1.2.1.4 设置python系统环境

[root@m01 ~]# ln -s /usr/local/python-3.7.0a1/ /usr/local/python
[root@m01 ~]# ln -fs /usr/local/python/bin/python3.7 /usr/bin/python

1.2.2 设置pip环境变量

# Python3.x默认已安装pip
[root@m01 ~]# ln -s /usr/local/python/bin/pip3.7 /usr/bin/pip
[root@m01 ~]# export PATH=/usr/local/python/bin:$PATH

1.2.3 Python3.x安装Django

[root@m01 ~]# pip install django

1.3 Python安装时的问题

1.3.1 提示找不到libpython2.7.so.1.0

【问题现象】:

[root@cobbler ~]# python -V
python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
[root@cobbler ~]# ldd /usr/bin/python
    linux-vdso.so.1 =>  (0x00007ffed2c9d000)
    libpython2.7.so.1.0 => not found        # 找不到libpython2.7.so.1.0文件
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003003e00000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003003600000)
    libutil.so.1 => /lib64/libutil.so.1 (0x0000003005600000)
    libz.so.1 => /lib64/libz.so.1 (0x0000003004600000)
    libm.so.6 => /lib64/libm.so.6 (0x0000003004a00000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003003a00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003003200000)

【原因分析】:

Python软件找不到libpython2.7.so.1.0文件。

【解决方法】:

查找libpython2.7.so.1.0文件位置:

[root@cobbler ~]# whereis libpython2.7.so.1.0
libpython2.7.so.1: /usr/local/lib/libpython2.7.so.1.0
添加libpython2.7.so.1.0文件到Python软件中:
[root@cobbler ld.so.conf.d]# cd /etc/ld.so.conf.d/
[root@cobbler ld.so.conf.d]# vim python.conf
/usr/local/lib
[root@cobbler ~]# ldconfig
[root@cobbler ~]# ldd /usr/bin/python
    linux-vdso.so.1 =>  (0x00007ffeccfab000)
    libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0 (0x00007fd718e43000)  # 已经识别了
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003003e00000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003003600000)
    libutil.so.1 => /lib64/libutil.so.1 (0x0000003005600000)
    libz.so.1 => /lib64/libz.so.1 (0x0000003004600000)
    libm.so.6 => /lib64/libm.so.6 (0x0000003004a00000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003003a00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003003200000)
温馨提示:本文最后更新于2022-12-20 20:57:55,已超过431天没有更新。某些文章具有时效性,若文章内容或图片资源有错误或已失效,请联系站长。谢谢!
转载请注明本文链接:https://blog.leonshadow.cn/763482/237.html
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享