这就是令我困惑的问题以及如何解决。
$ which python
/usr/bin/python
$ which python3
/usr/local/bin/python3
$ ls /usr/local/bin/python
ls: /usr/local/bin/python: No such file or directory
因此请注意,我没有安装python2.7的HomeBrew,但是安装了python3。 / usr / bin / python下的版本使用的是系统默认值。 您可以根据模块搜索路径判断:
$ /usr/bin/python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
`enter code here`Type "help", "copyright", "credits" or "license" for
more information.
>>> import sys
>>> sys.path
['', '/Library/Python/2.7/...
请注意“ / Library / Python” ...,这是Mac OS的python版本。 但我想严格遵守用户安装的版本(即HomeBrew)。
所以这是我为解决此问题所做的工作:
$ brew install python
...
Warning: python 2.7.13 is already installed, it's just not linked.
You can use `brew link python` to link this version.
$ brew link --overwrite python
$ which python
/usr/local/bin/python
$ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/Cellar/python/2.7.13...
它不再是/ Library / ..而是/ usr / local。
现在可以找到我所有的pip安装模块! 问题解决了!
更新:
将brew更新到1.5.4版后,似乎删除了符号链接。 现在,您必须将其添加到路径中:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
阅读“ brew info python”中的“警告”部分:
==> Caveats
This formula installs a python2 executable to /usr/local/bin.
If you wish to have this formula's python executable in your PATH then add
the following to ~/.bash_profile:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Pip and setuptools have been installed. To update them
pip2 install --upgrade pip setuptools
You can install Python packages with
pip2 install
They will install into the site-package directory
/usr/local/lib/python2.7/site-packages
See: https://docs.brew.sh/Homebrew-and-Python.html