Ubuntu下切换python版本的方法

本文介绍如何在Linux系统中为特定用户或整个系统修改默认Python版本。包括通过修改~/.bashrc文件为用户设置Python别名,以及利用update-alternatives命令进行系统级Python版本的更改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你可以按照以下方法使用 ls 命令来查看你的系统中都有那些 Python 的二进制文件可供使用。

$ ls /usr/bin/python*
/usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.4 /usr/bin/python3.4m /usr/bin/python3m

执行如下命令查看默认的 Python 版本信息:

$ python --version
Python 2.7.8

1、基于用户修改 Python 版本:

想要为某个特定用户修改 Python 版本,只需要在其 home 目录下创建一个 alias(别名) 即可。打开该用户的 ~/.bashrc文件,添加新的别名信息来修改默认使用的 Python 版本。

alias python='/usr/bin/python3.4'

一旦完成以上操作,重新登录或者重新加载 .bashrc 文件,使操作生效。

$ . ~/.bashrc

检查当前的 Python 版本。

$ python --version
Python 3.4.2

2、 在系统级修改 Python 版本

我们可以使用 update-alternatives 来为整个系统更改 Python 版本。以 root 身份登录,首先罗列出所有可用的 python 替代版本信息:

# update-alternatives --list python
update-alternatives: error: no alternatives for python

如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被 update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将 python2.7 和 python3.4 放入其中。

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

--install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中。这个例子中,我们为 /usr/bin/python3.4 设置的优先级为2,所以update-alternatives 命令会自动将它设置为默认 Python 版本。

# python --version
Python 3.4.2

接下来,我们再次列出可用的 Python 替代版本。

# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4

现在开始,我们就可以使用下方的命令随时在列出的 Python 替代版本中任意切换了。

# update-alternatives --config python

# python --version
Python 2.7.8

3、移除替代版本

一旦我们的系统中不再存在某个 Python 的替代版本时,我们可以将其从 update-alternatives 列表中删除掉。例如,我们可以将列表中的 python2.7 版本移除掉。

# update-alternatives --remove python /usr/bin/python2.7

update-alternatives: removing manually selected alternative - switching python to auto mode
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

方法2、移除软连接

rm -rf /data/logs

ln -s /temp/logs /data/logs

解决软连接ln报错-bash: /usr/local/bin/mysql: Too many levels of symbolic links

### 在 Ubuntu 上手动切换 Python 版本的具体操作 在 Ubuntu 系统中,可以通过多种方式来实现不同 Python 版本之间的切换。以下是具体的操作方法: #### 方法一:使用 `update-alternatives` 命令 `update-alternatives` 是一种管理多个命令版本的工具,在 Ubuntu 中可以用来设置系统的默认 Python 解释器。 1. **查看当前已安装的 Python 版本** 使用以下命令列出系统中存在的所有 Python 可执行文件: ```bash ls /usr/bin/python* ``` 2. **配置可选的 Python 版本** 将不同的 Python 版本添加到 `update-alternatives` 的列表中。假设系统中有 Python 2.7 和 Python 3.6,则运行如下命令: ```bash sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2 ``` 这里的数字 `1` 和 `2` 表示优先级,数值越高越容易被选作默认解释器[^3]。 3. **选择默认的 Python 版本** 输入以下命令以交互式的方式选择所需的 Python 默认版本: ```bash sudo update-alternatives --config python ``` 然后按照提示输入对应编号即可完成切换。 #### 方法二:修改环境变量 PATH 通过调整 `$PATH` 环境变量中的路径顺序也可以改变默认使用的 Python 版本。 1. 编辑用户的 shell 配置文件(如 `.bashrc` 或者 `.zshrc`),并加入指定目录下的特定 Python 版本位置至最前头。例如如果想让 `/opt/python3.6/bin/` 成为主导则添加下面这行代码: ```bash export PATH=/opt/python3.6/bin/:$PATH ``` 2. 让更改生效: ```bash source ~/.bashrc ``` 3. 测试新设定是否成功应用: ```bash which python && python --version ``` 以上两种方法都可以有效地帮助你在同一台机器上的不同项目间灵活切换所需的不同版本Python 环境[^1][^2]。 ```python import sys print(sys.version) ``` 上述脚本可用于验证当前正在使用的 Python 版本号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值