如何让Mac OS使用Homebrew安装的python

时间:2013-08-24 14:16:49

标签: python macos python-2.7

我已经在网上搜索过这个问题一段时间了,到目前为止我所做的是

  1. 在自制软件中安装了python32

  2. 更改了我的.bash_profile并添加了以下行:

  3.   

    export PATH = / usr / local / bin:/ usr / local / sbin:〜/ bin:$ PATH

    但当我关闭终端并重新开始时,我输入'which python',它仍会打印:

      

    的/ usr /斌/蟒

    并输入'python --version'仍然得到:

      

    Python 2.7.2

    我也尝试了以下指示:

      

    brew link --overwrite python

    或尝试通过运行此指令删除自制程序安装的python:

      

    brew remove python

    但上述两条指令都会导致此错误:

      

    错误:没有这样的桶:/ usr / local / Cellar / python

    任何人都可以帮忙,谢谢

6 个答案:

答案 0 :(得分:16)

如果您想使用Homebrew安装Python 3:

$ brew install python3
==> Downloading http://python.org/ftp/python/3.3.0/Python-3.3.0.tar.bz2
Already downloaded: /Library/Caches/Homebrew/python3-3.3.0.tar.bz2
==> ./configure --prefix=/usr/local/Cellar/python3/3.3.0 --enable-ipv6 --datarootdir=/usr/local/Cell
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0/share/python3
==> Downloading https://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz
Already downloaded: /Library/Caches/Homebrew/distribute-0.6.35.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Downloading https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz
Already downloaded: /Library/Caches/Homebrew/pip-1.3.1.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Caveats
Homebrew's Python3 framework
  /usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework

Distribute and Pip have been installed. To update them
  pip3 install --upgrade distribute
  pip3 install --upgrade pip

To symlink "Idle 3" and the "Python Launcher 3" to ~/Applications
  `brew linkapps`

You can install Python packages with
  `pip3 install <your_favorite_package>`

They will install into the site-package directory
 /usr/local/lib/python3.3/site-packages
Executable python scripts will be put in:
 /usr/local/share/python3
so you may want to put "/usr/local/share/python3" in your PATH, too.

See: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python

安装完毕后,请更新系统PATH变量,将下一行添加到~/.bash_profile

export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH

然后:

$ source ~/.bash_profile

现在启动Python:

$ python3
Python 3.3.0 (default, Mar 26 2013, 10:01:40) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

你可以查看python3路径:

$ which python3
/usr/local/bin/python3

答案 1 :(得分:9)

您可以尝试将此行添加到.bash_profile

alias python='python3'

答案 2 :(得分:2)

brew install python3输出中提到:

  

未版本化的符号链接pythonpython-configpip等指向   导出PATH = / usr / local / bin:/ usr / local / sbin:〜/ bin:$ PATH   python3python3-configpip3等已分别安装到     / usr / local / opt / python / libexec / bin

因此,将export PATH=/usr/local/opt/python/libexec/bin:$PATH添加到~/.bash_profile然后运行source ~/.bash_profile会得到由Homebrew创建的符号链接-python = python3,pip = pip3等:)

  

$ python --version

     

Python 3.7.0

     

$ pip --version

     

/usr/local/lib/python3.7/site-packages/pip(python 3.7)中的pip 18.0

答案 3 :(得分:0)

来自$ brew info python

  

此公式将python2可执行文件安装到/ usr / local / bin。   如果你希望在你的PATH中有这个公式的python可执行文件,那么添加   以下〜/ .bash_profile:     export PATH =&#34; / usr / local / opt / python / libexec / bin:$ PATH&#34;

然后确认您的python可执行文件对应于正确的安装:

$ which python

$ python --version

答案 4 :(得分:0)

我遇到了同样的问题,并做了一些研究。我发现有人在azure/cli存储库下针对同一问题创建了一个错误。您可以找到该问题here。我在这里提供的解决方案非常简单,并且解决了我的问题:

很可能是Brew损坏了,需要修补或修复。因此,运行brew doctor命令将为您提供有关正在发生的事情的摘要。 以下是我得到的:

mymac:bin sidmishra$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: The following directories do not exist:
/usr/local/sbin

You should create these directories and change their ownership to your account.
  sudo mkdir -p /usr/local/sbin
  sudo chown -R $(whoami) /usr/local/sbin

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
  /usr/local/lib/LibSideSyncOSX9.dylib
  /usr/local/lib/ss_conn_lib.dylib

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  python

关于命令brew doctor的奇妙之处在于,它不仅可以告诉您问题,而且可以在大多数情况下为您建议解决方案。 因此,我运行了brew建议的所有命令,并链接了以下命令:

brew link python

以上命令使我出错:

mymac$ brew link python
Linking /usr/local/Cellar/python/3.7.1... Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks/Python.framework

看来/urs/local/Frameworks对我当前的用户没有足够的权限。因此,我运行了以下命令,并为当前用户赋予了足够的权限:

sudo chown -R $(whoami) /usr/local/Frameworks/

运行上述命令后,我再次运行了链接命令,并且成功了!

mymac$ brew link python
Linking /usr/local/Cellar/python/3.7.1... 1 symlinks created

现在运行以下命令以获取当前选定的python版本:

python --version

以上命令应为您提供3.7.1(自2018年12月21日起)或python的新版本。默认情况下,您的Mac可能会设置python2。如果版本不是python3,则必须执行几个步骤才能使用python3上的最新python2版本。步骤如下:

使用Shell:

  • 在编辑中打开~/.bash_login~/.bash_profile~/.cshrc~/.profile~/.tcshrc~/.zprofile,无论您使用的命令外壳是什么模式。您可能必须使用sudo进行编辑。
  • 添加以下步骤:

    PATH =“ / Library / Frameworks / Python.framework / Versions / 3.2 / bin:$ {PATH}” 导出路径

  • 此外,添加以下内容进行备份:

    alias python = python3

使用自制软件:

运行以下命令以取消链接python2和链接python3:

mymac$ brew unlink python@2
mymac$ brew link python@3

以上将取消链接python2和链接python3。

希望你们中的一些人会从这个答案中得到帮助。

晚安!

答案 5 :(得分:0)

建议在macOS上使用Homebrew进行安装。话虽如此,Python 2.7现在随Mac OS一起提供。

  1. 安装Homebrew。
    { 
        "options": {
            "readOnly": {}
        }
    }
  1. 运行 { "transaction": "EcMecxy5IXKoIlkAIx+ixOCJ/NT6gvpbaEwWk/5YkOxVxSeMWpOdG4H2nZGK3Y0Pmcj+lbvk6sAlw68UpFgjd8puTpyS2Vwm2X6mw2SKKKKK9OakXxkGgi+8o4vx70Qd4YQaGg==" }

一旦安装,它将说它已经安装了,但是您已经安装了Python 2.7。很好,但是我们希望将其设置为将python3作为选项

  1. 使用/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. 通过运行brew install python3
  3. 进行确认