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

4
我正在运行 macOS Sierra 版本 10.12.2,尝试安装一个与系统预装的 Python 2.7.10 版本不同的 Python 版本。我被建议安装一个新版本的 Python,否则可能会破坏系统 Python 的安装。
我已经使用 brew 安装了 Python 2.7.13:
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版本,并让操作系统识别并默认使用新版本(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
3个回答

4

只需更新$PATH变量,将其指向最新版本的Python 2.7.13,并将其保存在.bashrc(或).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}"

1
看起来 brew 会将 Python 安装在 /usr/local/Cellar/python/2.7.13/bin/python2.7 - Borealis

1
如果您使用 Home Brew 安装了 Python,则执行以下操作:
brew upgrade python

-2
有时候,尝试在一个新的 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接