一、下載源碼包
# 切換到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