简体   繁体   English

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

[英]How to make Mac OS use the python installed by Homebrew

I have searched online for a while for this question, and what I have done so far is我在网上搜索了一段时间这个问题,到目前为止我所做的是

  1. installed python32 in homebrew在自制软件中安装 python32

  2. changed my .bash_profile and added the following line to it:更改了我的 .bash_profile 并向其添加了以下行:

export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH导出路径=/usr/local/bin:/usr/local/sbin:~/bin:$PATH

but when I close the terminal and start again, I type 'which python', it still prints:但是当我关闭终端并重新开始时,我输入“which python”,它仍然打印:

/usr/bin/python /usr/bin/python

and type 'python --version' still got:并输入 'python --version' 仍然得到:

Python 2.7.2蟒蛇 2.7.2

I also tried the following instruction:我还尝试了以下说明:

brew link --overwrite python酿造链接--覆盖python

or try to remove python installed by homebrew by running this instruction:或者尝试通过运行以下指令删除自制软件安装的python:

brew remove python酿造删除蟒蛇

but both of the above two instructions lead to this error:但是以上两条指令都会导致此错误:

Error: No such keg: /usr/local/Cellar/python错误:没有这样的桶:/usr/local/Cellar/python

can anybody help, thanks谁能帮忙,谢谢

If you want to install Python 3 using Homebrew:如果要使用 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

Once installed update your system PATH variable, add the next line to ~/.bash_profile安装后更新系统PATH变量,将下一行添加到~/.bash_profile

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

And then:接着:

$ source ~/.bash_profile

Now launch Python:现在启动 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.
>>>

You can check python3 path:您可以检查python3路径:

$ which python3
/usr/local/bin/python3

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

alias python='python3'

brew install python3 output mentions: brew install python3输出提到:

Unversioned symlinks python , python-config , pip etc. pointing to export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH python3 , python3-config , pip3 etc., respectively, have been installed into /usr/local/opt/python/libexec/bin分别指向 export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH python3python3-configpip3等的未版本化符号链接pythonpython-configpip等安装到 /usr/local/opt/python/libexec/bin

So Adding export PATH=/usr/local/opt/python/libexec/bin:$PATH to ~/.bash_profile and then running source ~/.bash_profile gets you those symlinks created by Homebrew - python=python3, pip=pip3 etc :)因此,将export PATH=/usr/local/opt/python/libexec/bin:$PATH~/.bash_profile然后运行source ~/.bash_profile会为您提供 Homebrew 创建的符号链接 - python=python3, pip=pip3 等: )

$ python --version $ python --version

Python 3.7.0蟒蛇 3.7.0

$ pip --version $ pip --version

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

I came through the same issue and did some research.我遇到了同样的问题并做了一些研究。 I found that someone has created a bug for the same issue under the azure/cli repository.我发现有人在azure/cli存储库下为同一问题创建了一个错误。 You can find that issue here .您可以在此处找到该问题。 I am providing the same solution here which was very easy and fixed my issue:我在这里提供了相同的解决方案,这非常简单并解决了我的问题:

Most probably the Brew is broken and needs some patching or fixing.很可能 Brew 坏了,需要一些修补或修复。 So run brew doctor command which will give you a summary about what is happening.所以运行brew doctor命令,它会给你一个关于正在发生的事情的摘要。 Below is what I got:以下是我得到的:

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

The awesome stuff about the command brew doctor is that it not only tells you issues but also suggests you the solution steps in most of the cases.命令brew doctorbrew doctor在于,它不仅可以告诉您问题,还可以在大多数情况下为您提供解决方案的建议。 So, I ran all the commands suggested by the brew and to link I ran the following command:所以,我运行了 brew 建议的所有命令并链接我运行了以下命令:

brew link python

Above command threw me an error:上面的命令给我一个错误:

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

It seems that /urs/local/Frameworks doesn't have enough rights for my current user.似乎/urs/local/Frameworks对我当前的用户没有足够的权限。 So, I ran the following command and gave enough rights to my current user:因此,我运行了以下命令并为当前用户提供了足够的权限:

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

After running above command I ran linking command again, and it worked!!!运行上述命令后,我再次运行链接命令,它起作用了!!!

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

Now run following command to get the current selected python version:现在运行以下命令以获取当前选择的 python 版本:

python --version

Above command should give you 3.7.1 (as of 21st Dec 2018) or new version for the python.上面的命令应该为您提供3.7.1 (截至 2018 年 12 月 21 日)或新版本的 python。 There might be a chance that your Mac would have python2 set by default.您的 Mac 可能默认设置了python2 If the version is not python3 then you have to a couple of steps to use the latest python3 over python2 version.如果版本不是python3那么您必须执行几个步骤才能使用最新的python3不是python2版本。 Here are the steps:以下是步骤:

Using Shell:使用外壳:

  • Open ~/.bash_login or ~/.bash_profile or ~/.cshrc or ~/.profile or ~/.tcshrc or ~/.zprofile , whatever shell you are using for commands, in edit mode.在编辑模式下打开~/.bash_login~/.bash_profile~/.cshrc~/.profile~/.tcshrc~/.zprofile ,无论您用于命令的shell。 You may have to use sudo to edit them.您可能必须使用sudo来编辑它们。
  • Add following steps to it:为其添加以下步骤:

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

  • Also, add following for backup:另外,添加以下备份:

    alias python=python3别名 python=python3

OR Using homebrew:使用自制软件:

Run following commands to unlink python2 and link python3:运行以下命令来取消链接 python2 和链接 python3:

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

Above will unlink python2 and link python3.以上将取消链接 python2 和链接 python3。

Hope some of you will get helped from this answer.希望你们中的一些人会从这个答案中得到帮助。

Good Day!!!再会!!!

From $ brew info python :$ brew info python

This formula installs a python2 executable to /usr/local/bin.此公式将 python2 可执行文件安装到 /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"如果你希望在你的 PATH 中有这个公式的 python 可执行文件,那么将以下内容添加到 ~/.bash_profile: export PATH="/usr/local/opt/python/libexec/bin:$PATH"

Then confirm your python executable corresponds to the correct installation:然后确认您的 python 可执行文件对应于正确的安装:

$ which python or $ which python

$ python --version

Installing with Homebrew is recommended on macOS.建议在 macOS 上使用 Homebrew 安装。 That being said, Python 2.7 comes with Mac OS;话虽如此,Mac OS 附带 Python 2.7; however, it is deprecated and will be removed soon.但是,它已被弃用并将很快被删除。 As such, you should be using Python3 and newer.因此,您应该使用 Python3 和更新版本。

  1. Install Homebrew.安装 Homebrew。
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  1. Run brew install python运行brew install python

Once Python is installed, Homebrew will say that the installation is complete, but that you already have Python 2.7 installed.安装 Python 后,Homebrew 会说安装已完成,但您已经安装了 Python 2.7。 This is nice, but we want to set it to actually see python3 as an option这很好,但我们希望将其设置为实际将 python3 视为一个选项

  1. Use brew link使用brew link
  2. Confirm by running which python3 , the path should be /usr/local/bin/python3通过运行which python3确认,路径应该是/usr/local/bin/python3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM