我的Mac上使用什么版本的Python? [英] What version of Python is on my Mac?
问题描述
当我这样做时,我有一台Mac:
I have a mac, when I do:
python --version
我得到了:
Python 2.7.6
但是当我到达时:
/System/Library/Frameworks/Python.framework/Versions/3.3
2.7.6在哪里? 有什么建议我该怎么办?
where is 2.7.6 located ? any advice what should I do?
更新:
$ 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.
在PyCharm编辑器中,默认设置解释器显示:
in PyCharm editor, the default settings interpreter shows:
推荐答案
您的macOS上可能有多个Python版本.
You could have multiple Python versions on your macOS.
您可以通过command
,type
或which
命令进行检查,例如:
You may check that by command
, type
or which
command, like:
which -a python python2 python2.7 python3 python3.6
或在 Terminal 中键入python
并按 Tab 几次以自动完成,这等效于:
Or type python
in Terminal and hit Tab few times for auto completion, which is equivalent to:
compgen -c python
默认情况下,python
/pip
命令指向在PATH
环境变量中找到的第一个二进制文件,具体取决于实际安装的文件.因此,在使用Homebrew安装Python软件包之前,默认的Python已安装在macOS随附的/usr/bin
中(例如High Sierra上的Python 2.7.10). /usr/local
中找到的任何版本(例如/usr/local/bin
)都由外部程序包提供.
By default python
/pip
commands points to the first binary found in PATH
environment variable depending what's actually installed. So before installing Python packages with Homebrew, the default Python is installed in /usr/bin
which 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 2,您可以分别使用python2
/pip2
命令;对于Python 3,您可以使用python3
/pip3
,但这取决于您的配置哪些命令可用.
It is generally advised, that when working with multiple versions, for Python 2 you may use python2
/pip2
command, respectively for Python 3 you can use python3
/pip3
, but it depends on your configuration which commands are available.
值得一提的是,自发布Homebrew 1.5.0版本以来, + (于2018年1月19日),python
公式已升级到Python 3.x,并且将添加python@2
公式以安装Python 2.7.以前,python
公式指向的是Python 2.
It is also worth to mention, that since release of Homebrew 1.5.0+ (on 19 January 2018), the python
formula has been upgraded to Python 3.x and a python@2
formula will be added for installing Python 2.7. Before, python
formula was pointing to Python 2.
例如,如果您通过Homebrew安装了其他版本,请尝试以下命令:
For instance, if you've installed different version via Homebrew, try the following command:
brew list python python3
或:
brew list | grep ^python
它将向您显示与软件包一起安装的所有Python文件.
it'll show you all Python files installed with the package.
或者,您可以使用apropos
或locate python
命令来查找更多与Python相关的文件.
Alternatively you may use apropos
or locate python
command to locate more Python related files.
要检查与Python相关的任何环境变量,请运行:
To check any environment variables related to Python, run:
env | grep ^PYTHON
要解决您的问题,
To address your issues:
错误:没有这样的小桶:/usr/local/Cellar/python
Error: No such keg: /usr/local/Cellar/python
意味着您没有通过Homebrew安装Python.但是,请通过一次仅指定一个程序包来进行仔细检查(如brew list python python2 python3
).
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
).
定位数据库(
/var/db/locate.database
)不存在.
遵循建议并运行:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
重建数据库后,可以使用locate
命令.
After the database is rebuild, you can use locate
command.
这篇关于我的Mac上使用什么版本的Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!