Centos6下编译安装Python3.5.3

in 互联网技术 with 0 comment  访问: 702 次

Centos6下编译安装Python3.5.3

安装编译依赖

# yum -y install gcc zlib zlib-devel openssl openssl-devel

解释这几个依赖包的作用:

1、gcc这个就不多做解释了,就是编译器

2、zlib依赖是因为在安装Python3.5的编译过程中会依赖于系统的zlib,如果不安装编译的时候不加--with-zlib-dir参数,编译过程中自动安装pip3就会失败,手动模拟情况示例如下:

[root@Labhost1 src]# python3 get-pip.py 
Traceback (most recent call last):
  File "get-pip.py", line 20061, in <module>
    main()
  File "get-pip.py", line 194, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip
zipimport.ZipImportError: can't decompress data; zlib not available

3、openssl依赖是因为在安装Python3.5的编译过程中会依赖于ssl模块,如果没有的话编译过程中自动安装pip3就会失败,手动模拟情况示例如下:

[root@Labhost1 src]# python3 get-pip.py 
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting pip
  Could not fetch URL https://pypi.python.org/simple/pip/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
  Could not find a version that satisfies the requirement pip (from versions: )
No matching distribution found for pip

下载包编译安装

# wget --no-check-certificate https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tgz
# tar zxf Python-3.5.3.tgz
# cd Python-3.5.3
# ./configure --prefix=/usr/local/ --with-zlib-dir --enable-optimizations

执行完成上面操作之后,然后修改Python-3.5.3目录下Modules/Setup文件:

# vim Modules/Setup

把Setup文件207 ~ 210四行内容前端的注释去掉,把:

#SSL=/usr/local/ssl
#_ssl _ssl.c \
#       -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#       -L$(SSL)/lib -lssl -lcrypto

改为:

SSL=/usr/local/ssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto

改完之后,然后接着编译:

# make
# make install

MakeResult.png

验证结果

VerfResult.png
之前以为到这就可以了,其实没有,当我利用pip3命令安装mysql模块会报如下错误:

# pip3 install pymysql
/usr/local/bin/python3.5: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

这个是因为找不到共享库libpython3.5m.so.1.0, 详情如下:

# ldd /usr/local/bin/python3.5
    linux-vdso.so.1 =>  (0x00007ffd0bfbf000)
    libpython3.5m.so.1.0 => not found
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fece1538000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007fece1334000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007fece1131000)
    librt.so.1 => /lib64/librt.so.1 (0x00007fece0f28000)
    libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007fece0cbc000)
    libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007fece08d7000)
    libm.so.6 => /lib64/libm.so.6 (0x00007fece0652000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fece02be000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fece175f000)
    libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fece007a000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fecdfd92000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fecdfb8e000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fecdf962000)
    libz.so.1 => /lib64/libz.so.1 (0x00007fecdf74b000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fecdf540000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fecdf33d000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fecdf122000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fecdef03000)

如上可以看到libpython3.5m.so.1.0 => not found,那如何解决呢,解决过程如下:

# echo "/usr/local/lib" > /etc/ld.so.conf.d/python3.5.conf
# ldconfig

然后再尝试安装包就不会报错了,安装测试结果如下:

# pip3.5 install pymysql
Collecting pymysql
  Downloading PyMySQL-0.7.11-py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 9.8kB/s 
Installing collected packages: pymysql
Successfully installed pymysql-0.7.11

总结

为什么要写这篇文章,其实没有什么,就是记录自己安装实践过程中的历程,以后再有这种需求,直接照着博客文章,复制粘贴就好了。

那有人问了,Python不是有多版本管理的一些工具包吗,比如Pyenv、pythonbrew等工具。有这些便利的工具为什么不用?

确实这些工具还是相当不错的,我在做一些实验的使用本地开发用过pyenv,还是非常好用的,但是有时候一些准生产环境和线上,我们软件的版本是有严格的版本控制,不会允许多版本共存,除非有特殊情况,比如Centos6.*的yum命令就是基于Python2.6的,这个就不会动了。

所以作为一个运维我一直信奉:没有最好的,只有最适合自我的,只有适合自我场景、业务的任何情况都是合理的,其他的你给我说的天花乱坠,那我只能当你在扯淡。

WeZan
Responses