如何使用 brew 在 Mac OS Sierra 上安装更新的 Python?

How to install updated Python on Mac OS Sierra with brew?

我是 运行 macOS Sierra 版本 10.12.2,我正在尝试从预装的系统 Python 2.7.10 安装单独的 Python 版本.有人建议我安装 Python 的新版本,否则可能会搞砸系统 Python install.

我安装了 Python 2.7.13 使用 brew:

brew install python

但是我的终端还是默认系统安装

$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
[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.
>>> 

安装 Python 2.7 的更新(非系统)版本并让 OS 识别并默认使用新 (2.7.13) 版本的首选方法是什么?

编辑:

当我使用以下内容更新 .bash_profile 文件时,启动 bash 终端时出现错误。

PATH="/usr/local/Cellar/python/2.7.13/bin:${PATH}"
export "$PATH"

这是我收到的错误:

-bash: export: `/usr/local/Cellar/python/2.7.13/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin': not a valid identifier

只需更新 $PATH 变量以指向 .bashrcPython 2.7.13 的最新版本(或).bash_profile,具体取决于您正在调用的 shell ,

export PATH="/path/to/your/python2.7.13/bin:${PATH}"

即类似于 /Library/Frameworks/Python.framework/Versions/,即

export PATH="/Library/Frameworks/Python.framework/Versions/2.7.13/bin:${PATH}"

有时候,尝试新的 shell 会有帮助。

我刚才做了同样的事情,它告诉我 python 2.7.10 在我的系统上。但是我打开了一个新的 shell 并尝试了,瞧!这是 python.

的正确版本
$ brew install python
==> Downloading https://homebrew.bintray.com/bottles/python-2.7.13.sierra.bottle.1.tar.gz
Already downloaded: /Users/sptamhan/Library/Caches/Homebrew/python-2.7.13.sierra.bottle.1.tar.gz
==> Pouring python-2.7.13.sierra.bottle.1.tar.gz
==> Using the sandbox
==> /usr/local/Cellar/python/2.7.13/bin/python -s setup.py --no-user-cfg install --force --verbose --single-version-externally-managed --record=installed.txt --install-scripts=/usr/local/Cellar/python/2.7.13/bin --install-lib=/usr/local/lib/python2.7/site-packages
==> /usr/local/Cellar/python/2.7.13/bin/python -s setup.py --no-user-cfg install --force --verbose --single-version-externally-managed --record=installed.txt --install-scripts=/usr/local/Cellar/python/2.7.13/bin --install-lib=/usr/local/lib/python2.7/site-packages
==> /usr/local/Cellar/python/2.7.13/bin/python -s setup.py --no-user-cfg install --force --verbose --single-version-externally-managed --record=installed.txt --install-scripts=/usr/local/Cellar/python/2.7.13/bin --install-lib=/usr/local/lib/python2.7/site-packages
==> Caveats
Pip and setuptools have been installed. To update them
  pip install --upgrade pip setuptools

You can install Python packages with
  pip install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: http://docs.brew.sh/Homebrew-and-Python.html
==> Summary
  /usr/local/Cellar/python/2.7.13: 3,526 files, 48MB
$ python -V
Python 2.7.10

在新终端中:

$ python -V
Python 2.7.13

如果您使用 Home Brew 安装了 Python,那么请执行以下操作:

brew upgrade python