为什么在虚拟环境中有两个 python 版本?

Why there are two python versions in a virtual environment?

我在 VS 中使用 anaconda 创建了一个虚拟环境。当环境活跃时。我使用 python --version 检查版本,它给出以下输出 Python 3.9.9,而当我使用 which python 并从路径 /usr/bin/python --version 检查版本时,我得到一个不同的版本 Python 2.7.18。为什么会这样,环境使用哪个版本?

一旦您的虚拟环境被激活,python 命令将使用您的 venv 中的 python 版本(位于 path/to/.venv/bin/python)。 which python/usr/bin/python 强制使用安装在 /usr/bin/python 中的 python 版本,在您的情况下似乎是版本 2.7.18。如果你想更改你的默认 Python 版本(与 python 一起使用但没有激活 venv 的版本)你可以使用 sudo ln -s /usr/bin/python /usr/bin/python3.9.