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

时间:2017-01-14 06:29:07

标签: python macos install homebrew

我正在运行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 个答案:

答案 0 :(得分:4)

根据您要调用的shell,只需更新$PATH变量以指向Python 2.7.13(或).bashrc.bash_profile的最新版本,

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 :(得分:0)

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

brew upgrade python

答案 2 :(得分:-2)

有时,只是尝试新的shell会有所帮助。

我刚刚做了同样的事情,它告诉我python 2.7.10在我的系统上。但我打开了一个新的外壳并尝试了,瞧!这是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