Skip to main content

Python 安装

Linux 安装 Python 3

Python 2 已经退出了历史舞台,所以我们的教程都是 Python 3 版本的。

如果你在使用 Linux 系统,我可以默认为你拥有一定的系统管理经验,安装 Python 对于你来说应该是很容易的事。

事实上,在 Linux 上安装 Python 3 是最简单的,直接使用包管理器安装即可。

# ubuntu 系列,系统默认会安装 Python3
sudo apt install python3

# centos 系列,系统默认会安装 Python,但是 Python3 需要手动安装
sudo yum install python3

CentOS 7 系列安装 Python 3.6

虽然CentOS 已经停止维护了,但是还是有很多的系统是 CentOS。CentOS 7 直接安装 epel-release 包,它里面包含了 Python3.6 ,看下面示例。

yum源,yum仓库源,CentOS 停止维护了以后,官方的地址已经不能访问了,下面是测试可以用的阿里yum源仓库。

[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

安装Python

# 安装 yum 仓库
$ sudo yum install epel-release

# 安装 Python3
$ sudo yum install python3


$ pip3 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

$ python3 --version
Python 3.6.8

$ python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(100)
100

CentOS 7 系列安装 Python 3.8

如果想安装更高版本的,需要用到 centos-release-scl

# 安装 yum 仓库
sudo yum install centos-release-scl

# 安装 Python3.8
# 安装 rh-python38-python-devel 有些Python包会依赖该库
sudo yum install rh-python38 rh-python38-python-devel

# 创建 python3.8 命令软连接
sudo ln -s /opt/rh/rh-python38/root/usr/bin/python3.8 /usr/local/bin/python3.8


[work@node1 ~]$ python3.8 --version
Python 3.8.13
[work@node1 ~]$ python3.8 
Python 3.8.13 (default, Aug 16 2022, 12:16:29) 
[GCC 9.3.1 20200408 (Red Hat 9.3.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(100)
100