centos、rocky linux 切换python默认版本的最简单步骤(随手记)

CentOS 8 修改方式

centos8默认安装的python版本是python3.6,当需要更高版本的python3.9或3.11时,我们经常搜索到源码安装然后编写软连接。

[root@manager Python-3.9.0]# python3 --version
Python 3.6.8
[root@manager Python-3.9.0]# pip3 --version
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

不过其实可以不用这样,最简单的步骤如下:

  1. yum安装需要的python版本(如3.9)
# 假如需要3.9
yum install -y python39
  1. alternatives命令指定默认python
alternatives --config python3

在这里插入图片描述
切换完成。

Rocky 9 修改方式

最近发现,使用上述方法sudo alternatives --config python3 ,结果没有回显。搜索了一下解决了。

1. 确认已安装 Python 3.11

首先,确保你的系统上已经安装了 Python 3.11。你可以通过以下命令来检查:

python3.11 --version

如果没有安装 Python 3.11,可以使用以下命令安装(针对 Rocky Linux 9):

sudo dnf install python3.11

2. 使用 alternatives 配置默认 Python 版本

在 Linux 系统中,alternatives 命令可以帮助管理多个版本的程序。你可以通过它来设置默认的 Python 版本。按照以下步骤操作:

添加 Python 版本到 alternatives 系统中

首先,确保将 Python 3.9 和 Python 3.11 都添加到 alternatives 系统中(如果它们尚未添加)。

sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2

这两个命令将 Python 3.9 和 Python 3.11 注册到 alternatives 中,并为它们指定优先级(1 和 2)。数字越小,优先级越高。

配置默认 Python 版本

接下来,你可以使用以下命令来选择默认的 Python 版本:

sudo alternatives --config python3

运行该命令后,系统会显示一个选择列表,类似于:

There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
  1           /usr/bin/python3.9   1         auto mode
  2           /usr/bin/python3.11  2         manual mode

Press <enter> to keep the current choice[*], or type selection number:

你可以输入对应的数字(比如 2)来选择 Python 3.11 作为默认版本。

3. 验证更改

完成上述操作后,验证默认 Python 版本是否已经更新:

python3 --version

如果显示 Python 3.11.x,那么就表示成功切换了默认 Python 版本。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值