默认情况下,Linux会自带安装Python,可以运行python --version命令查看
1.查看当前python版本
# 查看Linux自带的Python2.7版本位置信息
[dyf@iZuf6jf0fjkrh41n5rm60oZ ~]$ which python
/usr/bin/python
# 查看Linux默认安装的Python位置
[dyf@iZuf6jf0fjkrh41n5rm60oZ ~]$ whereis python
python: /usr/bin/python2.7-config /usr/bin/python2.7 /usr/bin/python /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
# 看到/usr/bin/python和/usr/bin/python2都是软链接,/usr/bin/python指向/usr/bin/python2,而/usr/bin/python2最终又指向/usr/bin/python2.7。所以运行python/python2/python2.7
[dyf@iZuf6jf0fjkrh41n5rm60oZ ~]$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root 7 Feb 27 11:18 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 Feb 27 11:18 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7144 Nov 17 06:23 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 Nov 17 06:23 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 Feb 27 11:18 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 32 Mar 16 11:18 /usr/bin/python3 -> /usr/local/python3/bin/python3.8
lrwxrwxrwx 1 root root 14 Feb 27 11:18 /usr/bin/python-config -> python2-config
2.安装依赖
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
3.安装wget
yum install wget
4.下载源码包
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
5.解压安装
# 解压压缩包
tar -zxvf Python-3.8.1.tgz
# 进入文件夹
cd Python-3.8.1
# 配置安装位置
./configure prefix=/usr/local/python3
# 安装
make && make install
# 查看是否安装成功
[dyf@iZuf6jf0fjkrh41n5rm60oZ local]# cd /usr/local/
[dyf@iZuf6jf0fjkrh41n5rm60oZ local]# ls
aegis bin etc games include lib lib64 libexec python3 sbin share src
6.添加软连接
#添加python3的软链接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
#添加 pip3 的软链接
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
7.测试是否安装成功
[dyf@iZuf6jf0fjkrh41n5rm60oZ ~]$ python3
Python 3.8.1 (default, Mar 16 2021, 11:17:21)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
想要把Python3链接到python的话,就得修改一下yum的配置
vi /usr/bin/yum
把 #! /usr/bin/python 修改为 #! /usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down
把 #! /usr/bin/python 修改为 #! /usr/bin/python2