繁体   English   中英

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

[英]How to set default python3 to python 3.9 instead of python 3.8 in Ubuntu 20.04 LTS

我在 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

您应该能够使用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 中尝试过)

Changing Python version: First we need to check what are the versions in Python using cmd: sudo ls /user/bin/python* And then need to use this cmd: sudo ln -sf /user/bin/python3.9 /user/ bin/python3 当您使用第二个 cmd 时,它将从默认更改为 python3.9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM