【问题标题】:When I type 'python -V' in my terminal...why do I see Python 2.7.10当我在终端中键入“python -V”时...为什么会看到 Python 2.7.10
【发布时间】:2018-07-03 09:50:55
【问题描述】:

我只在我的 Mac 上安装了 Python3.6。 我还安装了 miniconda。 我是否运行命令:

$python -V

在我的主目录或特定项目目录中,输出为 Python 2.7.10。为什么不是 Python 3.6,如何更改此默认值?

【问题讨论】:

  • 在默认安装 python2.x 的基于 unix 的系统上,调用 python [args](在这种情况下,[args]-V)将始终使用默认安装的 python 版本。由于您安装了 Python 3,假设安装将 python3 添加到您的 $PATH 环境变量中,您应该能够使用 python3 -V 并且它应该返回 3.6.x
  • 运行 $python3 -V
  • 谢谢大家!这就说得通了。 Python3 -V 显示正确。
  • 运行which python
  • 只安装了 Python 3.6,但你的 Mac预装了 Python 2.7。

标签: python bash macos conda


【解决方案1】:

可能发生的情况是您的环境中有 2 个版本的 Python:

  • 您手动安装的 3.6
  • 2.7.10 由 miniconda 安装

由于某种原因,python 命令的环境变量设置为 2.7.10,所以在你的 shell 中,python 命令指的是 2.7 版本,如果你想让它指向 3.6,你需要更新python 命令的路径变量。

如果你想管理更多的环境,我建议你使用 Anaconda/miniconda 并为 python 2 和 3 设置虚拟环境,这样你就可以在两者之间切换。

如果您只对 Python 的一个版本感兴趣,请使用 conda 发行版或纯 Python,因为混合使用通常会产生此类问题。

有关管理虚拟环境的更多信息:

Python:http://docs.python-guide.org/en/latest/dev/virtualenvs/

蟒蛇:https://conda.io/docs/user-guide/tasks/manage-environments.html

【讨论】:

    【解决方案2】:

    实际上,这取决于系统的行为是什么。请参阅PEP 394 了解更多信息,但大致而言,

    • python2 应该始终将您指向 Python 2.x
    • python3 应该始终将您指向 Python 3.x
    • 今天大多数发行版上的python 将指向 Python 2.x
    • 除了少数,它现在指向 Python 3.x
    • 在给定的系统上,python 可能会被覆盖以指向 Python 3.x
    • 在 virtualenv / venv 中,python 可以指向任何一个,具体取决于它的设置方式

    换句话说,如果您不想真正担心它,请使用python2python3 命令而不是python

    尽管 macOS 通过安装 python 而不使用 python2 链接(但有 python2.7 链接)违反了此模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2016-01-29
      • 2013-02-21
      • 2018-05-31
      • 1970-01-01
      • 2019-01-08
      • 2021-11-18
      相关资源
      最近更新 更多