如何在Ubuntu 20.04 LTS中将默认的Python 3设置为Python 3.9而不是Python 3.8?

23

我已经在Ubuntu 20.04 LTS中安装了Python 3.9。现在系统上有Python 3.8和Python 3.9两个版本。

# which python
# which python3
/usr/bin/python3
# which python3.8
/usr/bin/python3.8
# which python3.9
/usr/bin/python3.9
# ls -alith /usr/bin/python3
12583916 lrwxrwxrwx 1 root root 9 Jul 19  2021 /usr/bin/python3 -> python3.8

但是pip3命令仍会将所有内容安装到Python 3.8目录中。

# pip3 install --upgrade --find-links file:///path/to/directory <...>

我想通过更新符号链接 /usr/bin/python3 到 /usr/bin/python3.9 来更改默认的 pip3 行为。

怎么做呢?

# update-alternatives --set python3 /usr/bin/python3.9
This command will not work as expected.

这里是pip3信息:

# which pip3
/usr/bin/pip3
# ls -alith /usr/bin/pip3
12589712 -rwxr-xr-x 1 root root 367 Jul 13  2021 /usr/bin/pip3
# pip3 -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
# 

alias 命令将不起作用:

# alias python3=python3.9
# ls -alith /usr/bin/python3
12583916 lrwxrwxrwx 1 root root 9 Jul 19  2021 /usr/bin/python3 -> python3.8

https://dev59.com/-FgQ5IYBdhLWcg3wXyxv#51275052如何将Ubuntu默认的pip更改为正确的pip版本? - Adid
谢谢。我已经在帖子中添加了pip3的信息。但我仍然不知道如何操作,请问你能帮忙吗? - stackbiz
一个更简单的选项是通过 python 命令本身 "正确地" 运行 pip: python -m pip install ... - Adid
当我添加了pip3选项“--find-links file:///path/to/directory”时,“python3.9 -m pip install ...”会出现问题,它根本无法识别/path/to/directory。但是如果我使用上面的“pip3 install .. -find-links file:///path/to/directory”,那么pyhton 3.8可以正确识别它。所以我需要将默认的python 3.8版本更改为python 3.9,然后像往常一样使用pip3。 - stackbiz
明白了。问题在于你还需要更改pip可执行文件的符号链接,就像你已经为python可执行文件所做的那样。 - Adid
2个回答

22
你可以使用 python3.9 -m pip install <package> 命令,指定使用 Python 3.9 版本的 pip 来安装软件包。

完整说明文档请参见:https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

如果你想让 python3 指向 python3.9,那么可以使用一个简单的方法。

alias python3=python3.9

编辑:

尝试重新创建您的问题,

# which python3
/usr/bin/python3
# python3 --version
Python 3.8.10
# which python3.8
/usr/bin/python3.8
# which python3.9
/usr/bin/python3.9

然后更新备选项,并设置新的优先级:

# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
# sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

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

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

检查新版本:

# ls -alith /usr/bin/python3
3338 lrwxrwxrwx 1 root root 25 Feb  8 14:33 /usr/bin/python3 -> /etc/alternatives/python3
# python3 -V
Python 3.9.5
# ls -alith /usr/bin/pip3
48482 -rwxr-xr-x 1 root root 367 Jul 13  2021 /usr/bin/pip3
# pip3 -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)

希望这可以帮到你(我在WSL2 Ubuntu 20.04 LTS中尝试过)


2
这个命令不起作用:"alias python3=python3.9",执行完命令后,“ls”仍然显示Python 3.8。我已经在帖子中添加了别名命令的输出,请帮忙检查一下。 - stackbiz

11

更改Python版本:

首先需要使用以下命令检查Python中的版本:sudo ls /usr/bin/python*

然后需要使用此命令创建符号链接:

sudo ln -sf /usr/bin/python3.9 /usr/bin/python3 

当你使用这个命令时,它将把 python3.9 作为默认的 python3

我在我的Mac上遇到了"Operation not permitted"的错误。 - raba64577

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接