1 安裝依賴包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel gcc
2 下載解壓Python3.7
# 1 首先切換到桌面
cd /home
# 2 下載
wget http://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
# 3 解壓
tar -xvf Python-3.7.1.tar.xz
# 4 切換目錄
cd Python-3.7.1
3 執行配置檔案,編譯,安裝
./configure --prefix=/usr/local/python37
make
make install
4 建立軟連線
mv /usr/bin/python /usr/bin/python_bak
ln -s /usr/local/python37/bin/python3.7 /usr/bin/python
ln -s /usr/local/python37/bin/pip3.7 /usr/bin/pip
5 測試
# python
Python 3.7.1 (default, Jan 14 2019, 10:30:01)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world!")
hello world!
>>> exit()
# pip --version
pip 10.0.1 from /usr/local/python37/lib/python3.7/site-packages/pip (python 3.7)
6 產生問題
安裝完之後可能導致yum不能用,報錯為:
# yum -y install redis
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
原因是我們把預設的python命令改成了python3(原來是python2),而yum命令依賴於python2執行
7 解決方案
我們需要修改yum命令使用的python版本,原來使用的是預設的python(即python2),現在預設的python變成了python3,我們把它改成python2即可。
# 開啟檔案
vi /usr/bin/yum
# 修改第一行#!/usr/bin/python 改為#!/usr/bin/python2.7
# 還需要修改第二個檔案
vi /usr/libexec/urlgrabber-ext-down
# 修改第一行#!/usr/bin/python 改為#!/usr/bin/python2.7