自制python 2.7与OS X python 2.7 [英] Homebrew python 2.7 vs OS X python 2.7
问题描述
这是我第一次在这里问问题,如果我做错了事,请提前抱歉.问题是我使用Homebrew安装python 2以避免使用OS X系统python(因为建议这样做),但是当我在终端输入python时,它仍在使用系统python.
this is my first time asking a question here so sorry in advance if I'm doing something wrong. The problem is I installed python 2 using Homebrew to avoid using OS X system python (since it is recommended to do so), but when I type in the terminal - python, it is still using the system python.
哪个-a python给出: /usr/bin/python
which -a python gives: /usr/bin/python
编写python会给出:
writing python gives:
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可以得到:
and doing python2 gives:
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版本.
I would like to just write python and not python2 to use the Homebrew version.
另外,回显$ PATH
Also, echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
和/etc/paths
and /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/etc/paths (END)
我应该怎么做才能使用Homebrew python,而不要使用默认的系统python. P.D.我也用Hombrew安装了python 3.
What should I do to use Homebrew python and not the default system python. P.D. I installed python 3 with Hombrew too.
推荐答案
自制的最近更改了处理Python 2.x和3.x的方式.默认情况下,它不再隐藏macOS的python
,而是将Python 2.x安装为python2
,而将Python 3.x安装为python3
.
Homebrew recently changed the way it handles Python 2.x vs 3.x. It no longer shadows macOS’s python
by default and instead installs Python 2.x as python2
and Python 3.x as python3
.
实际上,它确实安装了python
,但没有在/usr/local/bin
中进行符号链接,因此为什么您的shell无法找到它.如果您希望一切正常运行,则需要将其添加到$PATH
:
In fact, it does install python
but doesn’t symlink it in /usr/local/bin
, hence why your shell can’t find it. If you wish to get everything working as before you need to add it to your $PATH
:
export PATH="$(brew --prefix python)/libexec/bin:$PATH"
您还可以从python
到python2
以及从pip
到pip2
添加别名,但这是更糟糕的解决方案,因为每个可执行文件都需要一个别名.
You could also add an alias from python
to python2
and from pip
to pip2
but it’s a worse solution because you need one alias for each executable.
# in your ~/.bash_profile
alias python=python2
alias pip=pip2
然后启动新的终端会话以使更改生效.
Then start a new terminal session for the changes to take effect.
有关更多信息,请参见官方文档.在brew info python
中也对此进行了概述:
See the official documentation for more info. This is also outlined in 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
...
这篇关于自制python 2.7与OS X python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!