如何在Ubuntu 20.04 LTS中将默认的python3设置为python 3.9而不是python 3.8



我在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/pyton3.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

您应该能够使用python3.9 -m pip install <package>在特定的python版本中运行pip,在本例中为3.9。

关于这方面的完整文档如下: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中尝试过(

更改Python版本:

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

然后需要使用此命令来符号链接它们:

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

当您使用该命令时,它将使用python3.9作为默认的python3

相关内容

  • 没有找到相关文章

最新更新