CentOS7系统目前默认python环境版本号为2.7.5,在实际的开发、测试环境中我们可能需要2.7.13的版本,但是Centos操作系统自带软件可能依赖python2.7.5版本,故原版本不能删除,我们只能python基础版本与高版本并存,以下为具体步骤:
配置编译环境
1 |
$ yum install gcc* openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel python-devel zlib zlib-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel |
下载python
1 |
$ wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz |
解压tgz包
1 |
$ tar -zxvf Python-3.6.5.tgz |
配置安装环境
SSL支持
修改 ./setup.py: (默认的openssl路径不改也可以)
1 2 3 4 5 6 |
# Detect SSL support for the socket module (via _ssl) search_for_ssl_incs_in = [ '/usr/local/openssl/include', # 修改为新目录 '/usr/local/openssl/include/openssl', # 新增 '/usr/contrib/ssl/include/' ] |
修改 ./Modules/Setup.dist:
1 2 3 4 5 6 7 8 9 |
# Socket module helper for socket(2) _socket socketmodule.c # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/openssl # 这里改为我们指定的目录 _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto |
安装位置
1 |
$ ./configure --prefix=/usr/local/python |
安装
1 2 |
$ make $ make altinstall #不要使用make install,否则会覆盖系统自带python |
安装后的配置
重命名旧版本
1 |
$ mv /usr/bin/python /usr/bin/python2.7.5 |
访问旧版本
1 |
$ python2.7.5 |
连接新版本
1 |
$ ln -s /usr/local/bin/python/python2.7 /usr/bin/python # 增加连接 |
查看当前版本
1 2 3 |
$ python -V output: Python 2.7.13 |
修改yum配置
1 2 3 4 5 |
$ vim /usr/bin/yum 首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7.5 $ vim /usr/libexec/urlgrabber-ext-down 首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7.5 |
安装pip
python3自带pip,不需要安装。
1 2 3 |
$ wget https://bootstrap.pypa.io/get-pip.py $ python get-pip.py $ ln -s /usr/local/bin/pip2.7 /usr/bin/pip #建立软连接 |
添加环境变量
1 2 3 |
$ vim ~/.bash_profile $ export PATH=/usr/local/python/bin:$PATH $ source ~/.bash_profile |