Redhat 8 上如何安装 Python
RedHat Enterprise Linux 8
上 Python 环境默认是不预装的, 因此, 用户需要根据自己需要选择安装 Python 3
还是 Python 2
.官方源中默认对 Python 3.6
有全面的支持. 接下来我们看看如何安装需要的 Python
版本.
注: 大多情况下, yum 包管理器是需要依赖 Python
和 一些基础包, 早期的发行版是将系统依赖的 Python
和 用户应用软件 公用 Python 环境, 这样就容易造成系统和用户环境对 Python
环境的冲突. 从 RedHat Enterprise Linux 8
将系统 Python 环境独立出来作为 Platform-Python
, 即系统平台 Python
环境, 用户将不能使用该 Python
环境, 仅有系统功能使用, 有官方统一维护. 当然, 用户可以编写一些系统使用的 Python
程序有 Platform-Python
使用.
## 安装 Python 3
RedHat Enterprise Linux 8
开始启用新的包管理工具 dnf
, 我们使用 dnf
来安装 python3
dnf install python3
因为官方支持的 3.6 版本, 因此随同 python3
的安装, 也将同时将 pip
和 setuptools
等依赖将一并安装好.
安装 Python 2
如果你希望系统中同时存在 python3
和 python2
执行环境, 可以按下述命令, 安装 python2
dnf install python2
管理 Python 环境
安装完 Python
环境后, 我们希望默认 /usr/bin/python
指向我们指定版本 Python
. RedHat Enterprise Linux 8
为避免系统默认到底该使用 Python2
还是 Python3
的争论, /usr/bin/python
默认未被创建. 因此在执行时需要由用户确认.
- 运行 python2 程序
python2 linuxcoming.py
- 运行 python 3 程序
python3 linuxcoming.py
如果确需指定用户环境下使用统一的 Python
版本, 可以使用 alternatives --config
备选方案来配置默认 Python
版本, 最终将对应版本指向 /usr/bin/python
.
alternatives --set python /usr/bin/python2
或者
alternatives --set python /usr/bin/python3