下载安装python 3.x

1
2
3
# python --version
Python 2.7.5
# wget -c https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz

检查编译环境

检查是否将gcc,gcc-c++等是否安装,并升级到最新版本。若没有安装可执行安装。

1
# yum install make gcc gcc-c++ zlib* libffi-devel

配置、编译、安装

配置

1
2
3
# tar zxvf Python-3.6.9.tgz 
# cd Python-3.6.9
# ./configure --prefix=/usr/local/python3

编译:

1
# make

安装:

1
# make install 

看到类似如下信息:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
......
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _lzma              
_sqlite3              _ssl                  _tkinter           
readline                                                       
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

......(很多)
Looking in links: /tmp/tmpt2edpoh3
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.2

安装下列软件包:

1
2
3
4
# yum install readline-devel
# yum install tk-devel tcl-devel  
# yum install openssl-devel   
# yum install sqlite-devel 

重新编译安装

1
2
# ./configure --prefix=/usr/local/python3
#make & make install

创建软连接

1
2
3
4
5
6
# ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
# ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
# pip3 -V
pip 18.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
# python3 -V
Python 3.6.9

设置 3.x 为默认版本

1
2
3
4
5
# ls -al /usr/bin|grep python
-rwxr-xr-x   1 root root      11232 Aug 13 05:18 abrt-action-analyze-python
lrwxrwxrwx   1 root root          7 Dec  9 10:12 python -> python2
lrwxrwxrwx   1 root root          9 Dec  9 10:12 python2 -> python2.7
-rwxr-xr-x   1 root root       7216 Aug  7 08:52 python2.7

删除旧的python软连接,将python连接到python3:

1
2
3
4
# rm /usr/bin/python
# ln -s /usr/local/bin/python3 /usr/bin/python
# python -V
Python 3.6.9

pip软连接

1
2
3
4
# ln -s /usr/local/python/bin/pip3 /usr/bin/pip3
# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
# pip -V
pip 18.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)

配置 yum(重要)

将 #!/usr/bin/python 改为 #!/usr/bin/python2.7,保存退出 。

1
# vi /usr/bin/yum
1
# vi /usr/libexec/urlgrabber-ext-down