我的 Mac 上有什么版本的 Python?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33175827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:56:35  来源:igfitidea点击:

What version of Python is on my Mac?

pythonmacos

提问by Bouchaib Mounir

I have a mac, when I do:

我有一个 mac,当我这样做时:

python --version 

I got:

我有:

Python 2.7.6

but when I got to:

但是当我到达时:

/System/Library/Frameworks/Python.framework/Versions/3.3

where is 2.7.6 located ? any advice what should I do?

2.7.6 在哪里?有什么建议我该怎么办?

UPDATE:

更新:

$ which -a python python3
/usr/bin/python
/usr/local/bin/python3

$ brew list python python3
Error: No such keg: /usr/local/Cellar/python

$ locate python

WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:



sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.

in PyCharm editor, the default settings interpreter shows:

在 PyCharm 编辑器中,默认设置解释器显示:

采纳答案by kenorb

You could have multiple Python versions on your macOS.

您的 macOS 上可以有多个 Python 版本。

You may check that by command, typeor whichcommand, like:

您可以通过command,typewhich命令进行检查,例如:

which -a python python2 python2.7 python3 python3.6

Or type pythonin Terminaland hit Tabfew times for auto completion, which is equivalent to:

或输入python终端和命中Tab为自动完成,这相当于几次:

compgen -c python

By default python/pipcommands points to the first binary found in PATHenvironment variable depending what's actually installed. So before installing Python packages with Homebrew, the default Python is installed in /usr/binwhich is shipped with your macOS (e.g. Python 2.7.10 on High Sierra). Any versions found in /usr/local(such as /usr/local/bin) are provided by external packages.

默认情况下,python/ pipcommands 指向在PATH环境变量中找到的第一个二进制文件,具体取决于实际安装的内容。因此,在使用 Homebrew 安装 Python 包之前,默认的 Python 已安装在/usr/bin您的 macOS 附带的(例如 High Sierra 上的 Python 2.7.10)中。在/usr/local(例如/usr/local/bin)中找到的任何版本都是由外部包提供的。

It is generally advised, that when working with multiple versions, for Python 2 you may use python2/pip2command, respectively for Python 3 you can use python3/pip3, but it depends on your configuration which commands are available.

通常建议,当使用多个版本时,对于 Python 2,您可以使用python2/pip2命令,对于 Python 3,您可以分别使用python3/ pip3,但这取决于您的配置,哪些命令可用。

It is also worth to mention, that since release of Homebrew 1.5.0+(on 19 January 2018), the pythonformula has been upgraded to Python 3.x and a python@2formula will be added for installing Python 2.7. Before, pythonformula was pointing to Python 2.

还值得一提的是,自Homebrew 1.5.0+ 发布(2018 年 1 月 19 日)python以来,该公式已升级到 Python 3.x,python@2并将添加用于安装 Python 2.7的公式。之前,python公式指向 Python 2。

For instance, if you've installed different version via Homebrew, try the following command:

例如,如果您通过 Homebrew 安装了不同版本,请尝试以下命令:

brew list python python3

or:

或者:

brew list | grep ^python

it'll show you all Python files installed with the package.

它将向您显示随包安装的所有 Python 文件。

Alternatively you may use aproposor locate pythoncommand to locate more Python related files.

或者,您可以使用aproposlocate python命令来定位更多与 Python 相关的文件。

To check any environment variables related to Python, run:

要检查与 Python 相关的任何环境变量,请运行:

env | grep ^PYTHON


To address your issues:

解决您的问题:

  • Error: No such keg: /usr/local/Cellar/python

    Means you don't have Python installed via Homebrew. However double check by specifying only one package at a time (like brew list python python2 python3).

  • The locate database (/var/db/locate.database) does not exist.

    Follow the advice and run:

    sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
    

    After the database is rebuild, you can use locatecommand.

  • 错误:没有这样的桶:/usr/local/Cellar/python

    意味着您没有通过 Homebrew 安装 Python。然而,通过一次只指定一个包来仔细检查(如brew list python python2 python3)。

  • 定位数据库 ( /var/db/locate.database) 不存在。

    按照建议运行:

    sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
    

    数据库重建后,您可以使用locate命令。

回答by knightrider

Use the which command. It will show you the path

使用which command. 它会告诉你路径

which python

回答by skrrgwasme

Take a look at the docs regarding Python on Mac.

查看有关 Mac 上 Python文档

The version at /System/Library/Frameworks/Python.framework is installed by Apple and is used by the system. It is version 3.3 in your case. You can access and use this Python interpreter, but you shouldn't try to remove it, and it may not be the one that comes up when you type "Python" in a terminal or click on an icon to launch it.

/System/Library/Frameworks/Python.framework 中的版本由 Apple 安装并由系统使用。在您的情况下,它是 3.3 版。您可以访问和使用这个 Python 解释器,但您不应该尝试删除它,并且当您在终端中键入“Python”或单击图标启动它时,它可能不会出现。

You must have installed another version of Python (2.7) on your own at some point, and now that is the one that is launched by default.

您一定在某个时候自己安装了另一个版本的 Python (2.7),现在这是默认启动的版本。

As other answers have pointed out, you can use the command which pythonon your terminal to find the path to this other installation.

正如其他答案所指出的,您可以使用which python终端上的命令来查找其他安装的路径。

回答by YòG?

To check third version, we can use python3 --version

要检查第三个版本,我们可以使用 python3 --version

回答by Ayush

Use below command to see all python installations :

使用以下命令查看所有 python 安装:

which -a python