2017-08-10 118 views
1

这是我第一次在这里问一个问题,所以提前抱歉,如果我做错了什么。问题是我使用Homebrew安装了python 2以避免使用OS X系统python(因为它被推荐这么做),但是当我输入终端-python时,它仍然使用系统python。Homebrew python 2.7 vs OS X python 2.7

其中-a蟒蛇给: 在/ 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() 

,做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() 

我想只写python而不是python2来使用Homebrew版本。

此外,回声$ PATH

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin 

和/ etc /路径

/usr/local/bin 
/usr/bin 
/bin 
/usr/sbin 
/sbin 
/etc/paths (END) 

我应该怎么做才能使用自制蟒蛇,而不是默认的系统蟒蛇。 P.D.我也用Hombrew安装了python 3。

+1

您可以设置一个别名在'.bash_profile'中,为了让命令'python'执行已安装的Homebrew,但我建议使用'pyenv'来更好地组织所有的python版本。 –

+0

你打开了一个新的终端会话吗?除非您开始一个新的会话或'source'您的配置文件,否则您的现有会话将不会有新的PATH。 – JoePasq

回答

3

Homebrew recently改变了它处理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" 

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

# in your ~/.bash_profile 
alias python=python2 
alias pip=pip2 

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

有关更多信息,请参阅the official documentation。这也在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 
... 
+0

感谢您的回答,并将其添加到我的〜/。bash_profile诀窍: export PATH =“$(brew --prefix python)/ libexec/bin:$ PATH” export PATH =“/ usr/local/opt/python/libexec/bin:$ PATH” –

+0

'$ (brew --prefix python)'和'/ usr/local/opt/python'是等价的,如果你的Homebrew安装在'/ usr/local'中的话。你可以删除前者;) – bfontaine