自制的python 2.7 vs OS X python 2.7

Jos*_*jal 3 macos homebrew python-2.7

这是我第一次在这里问一个问题,如果我做错了,请提前对不起.问题是我使用Homebrew安装python 2以避免使用OS X系统python(因为建议这样做),但是当我输入终端-python时,它仍然使用系统python.

-a python给出了:/ usr/bin/python

写python给出:

Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit() 
Run Code Online (Sandbox Code Playgroud)

并做python2给出:

Python 2.7.13 (default, Jul 18 2017, 09:16:53) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
Run Code Online (Sandbox Code Playgroud)

我想编写python而不是python2来使用Homebrew版本.

另外,回显$ PATH

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
Run Code Online (Sandbox Code Playgroud)

和/ etc/paths

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/etc/paths (END)
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能使用Homebrew python而不是默认的系统python.PD我也用Hombrew安装了python 3.

bfo*_*ine 6

Homebrew 最近改变了它处理Python 2.x和3.x的方式.它不再是阴影的MacOS的python默认,而是安装的Python 2.x的作为python2和Python 3.x作为python3.

事实上,它确实安装python但没有符号链接/usr/local/bin,因此你的shell无法找到它.如果您希望一切正常,您需要将其添加到您的$PATH:

export PATH="$(brew --prefix python)/libexec/bin:$PATH"
Run Code Online (Sandbox Code Playgroud)

你也可以从添加一个别名python,以python2pippip2,但它是一个糟糕的解决方案,因为你需要为每个可执行一个别名.

# in your ~/.bash_profile
alias python=python2
alias pip=pip2
Run Code Online (Sandbox Code Playgroud)

然后启动新的终端会话以使更改生效.

有关详细信息,请参阅官方文档.这也概括在brew info python:

$ brew info python
python: stable 2.7.13, HEAD
...
==> 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 <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages
...
Run Code Online (Sandbox Code Playgroud)