【问题标题】:Homebrew python 2.7 vs OS X python 2.7自制 python 2.7 与 OS X python 2.7
【发布时间】:2018-01-19 05:48:02
【问题描述】:

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

which -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() 

并且做 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/paths

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

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

【问题讨论】:

  • 您可以在您的.bash_profile 中设置一个别名,以便命令python 执行安装的Homebrew,但我建议使用pyenv 来更好地组织您所有的python 版本。
  • 您是否打开了一个新的终端会话?除非您开始新会话或 source 您的个人资料,否则您现有的会话将不会有新的 PATH。

标签: macos python-2.7 homebrew


【解决方案1】:

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"

您还可以添加一个从 pythonpython2 和从 pippip2 的别名,但这是一个更糟糕的解决方案,因为您需要为每个可执行文件添加一个别名。

# 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
...

【讨论】:

  • 感谢您的回答,将其添加到我的 ~/.bash_profile 中就可以了:export PATH="$(brew --prefix python)/libexec/bin:$PATH" export PATH="/ usr/local/opt/python/libexec/bin:$PATH"
  • $(brew --prefix python)/usr/local/opt/python 如果您的 Homebrew 安装在 /usr/local(它是)中,则等效;你可以删除前者;)
【解决方案2】:

去下载安装新版本any version of Python 2 or 3 for Mac

并确保您在正确的本地(主)目录中看起来像

compname:~ yourname$

【讨论】:

    猜你喜欢
    • 2012-08-24
    • 2014-07-04
    • 2016-11-29
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多