一、下载源码包
切换到root目录
[root@localhost ~] cd /root/
安装wget
[root@localhost ~] yum -y install wget
使用wget下载到目录
[root@localhost ~] wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
解压
[root@localhost ~] tar xvf Python-3.7.0.tar.xz
二、安装依赖
[root@localhost ~] yum install gcc openssl-devel bzip2-devel expat-devel gdbm-devel sqlite-devel libffi-devel
三、编译安装
切换到解压后的目录Python-3.7.0
[root@localhost ~] cd Python-3.7.0
编译
[root@localhost Python-3.7.0] ./configure --prefix=/usr/local/python3.7 --enable-shared CFLAGS=-fPIC
生成安装文件,进行安装
[root@localhost Python-3.7.0] make && make install
四、配置环境
备份python软连接,pip如果不存在就不用备份
[root@localhost Python-3.7.0] mv -i /usr/bin/python /usr/bin/python.bak
[root@localhost Python-3.7.0] mv -i /usr/bin/pip /usr/bin/pip.bak
创建python3的连接
[root@localhost Python-3.7.0] ln -sv /usr/local/python3.7/bin/python3 /usr/bin/python
[root@localhost Python-3.7.0] ln -sv /usr/local/python3.7/bin/pip3 /usr/bin/pip
配置动态库
[root@localhost Python-3.7.0] vim /etc/ld.so.conf.d/python.conf
写入内容
/usr/local/python3.7/lib
启用配置
[root@localhost Python-3.7.0] ldconfig
五、解决yum和防火墙问题
修改下面几个文件内容的第一行的python为python2.7
[root@localhost Python-3.7.0] vim /usr/libexec/urlgrabber-ext-down
[root@localhost Python-3.7.0] vim /usr/bin/yum
[root@localhost Python-3.7.0] vim /usr/bin/firewall-cmd
[root@localhost Python-3.7.0] vim /usr/bin/firewall-offline-cmd
[root@localhost Python-3.7.0] vim /usr/sbin/firewalld
验证yum
[root@localhost Python-3.7.0] yum list
验证firewall
[root@localhost Python-3.7.0] systemctl status firewalld.service
六、修改pip源为阿里云
创建配置文件pip.conf
[root@localhost Python-3.7.0] cd /root/ && mkdir .pip && cd .pip && touch pip.conf
把下列内容写入到pip.conf文件
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
更新pip到最新版
[root@localhost Python-3.7.0] pip install --upgrade pip
七、校验、删除
命令行输入python,即可查看到版本号,如果还是2.7之类的就是没成功
[root@localhost Python-3.7.0] python
Python 3.7.0 (default, Jan 21 2020, 09:22:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
python命令下,按住Ctrl+D退出
[root@localhost Python-3.7.0] cd /root/
删除解压文件和安装包,如果需要也可以不删除
[root@localhost ~] rm -rf Python-3.7.0 Python-3.7.0.tar.xz