如何让 Mac OS 使用 Homebrew 安装的 python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18419500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Mac OS use the python installed by Homebrew
提问by photosynthesis
I have searched online for a while for this question, and what I have done so far is
我在网上搜索了一段时间这个问题,到目前为止我所做的是
installed python32 in homebrew
changed my .bash_profile and added the following line to it:
在自制软件中安装 python32
更改了我的 .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
谁能帮忙,谢谢
采纳答案by jabaldonedo
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
回答by Pan Long
You may try adding this line to your .bash_profile
您可以尝试将此行添加到您的 .bash_profile
alias python='python3'
回答by kip2
From $ brew info python
:
来自$ brew info python
:
This formula installs a python2 executable to /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"
此公式将 python2 可执行文件安装到 /usr/local/bin。如果你希望在你的 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
$ python --version
回答by Callum M
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:$PATHpython3
,python3-config
,pip3
etc., respectively, have been installed into /usr/local/opt/python/libexec/bin
未版本控制的符号链接
python
,python-config
,pip
等指向出口PATH =在/ usr / local / bin目录:在/ usr / local / sbin中:〜/斌:$ PATHpython3
,python3-config
,pip3
等,已经分别安装在/ usr /本地的/ opt /蟒蛇/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 3.7.0
$ pip --version
pip 18.0 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
$ python --version
蟒蛇 3.7.0
$ pip --version
来自 /usr/local/lib/python3.7/site-packages/pip (python 3.7) 的 pip 18.0
回答by S.Mishra
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/clirepository. You can find that issue here. I am providing the same solution here which was very easy and fixed my issue:
我遇到了同样的问题并做了一些研究。我发现有人在azure/cli存储库下为同一问题创建了一个错误。您可以在此处找到该问题。我在这里提供了相同的解决方案,这非常简单并解决了我的问题:
Most probably the Brew is broken and needs some patching or fixing. So run brew doctor
command which will give you a summary about what is happening.
Below is what I got:
很可能 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
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.
So, I ran all the commands suggested by the brew and to link I ran the following command:
该命令的绝妙之处brew doctor
在于,它不仅可以告诉您问题,还可以在大多数情况下向您建议解决方案步骤。因此,我运行了 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. So, I ran the following command and gave enough rights to my current user:
似乎/urs/local/Frameworks
对我当前的用户没有足够的权限。因此,我运行了以下命令并为当前用户提供了足够的权限:
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. There might be a chance that your Mac would have python2
set by default. If the version is not python3
then you have to a couple of steps to use the latest python3
over python2
version. Here are the steps:
以上命令应该为您提供3.7.1
(截至 2018 年 12 月 21 日)或新版本的 python。您的 Mac 可能已python2
设置为默认设置。如果版本不是python3
那么您必须执行几个步骤才能使用最新python3
的python2
版本。以下是步骤:
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. You may have to usesudo
to edit them. Add following steps to it:
PATH="/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}" export PATH
Also, add following for backup:
alias python=python3
- 在编辑模式下打开
~/.bash_login
or~/.bash_profile
or~/.cshrc
or~/.profile
or~/.tcshrc
or~/.zprofile
,无论您用于命令的shell。您可能必须使用sudo
来编辑它们。 为其添加以下步骤:
PATH="/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}" 导出路径
另外,添加以下备份:
别名 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!!!
再会!!!
回答by Justin Rice
Installing with Homebrew is recommended on macOS. That being said, Python 2.7 now comes with Mac OS.
建议在 macOS 上使用 Homebrew 安装。话虽如此,Python 2.7 现在随 Mac OS 一起提供。
- Install Homebrew.
- 安装 Homebrew。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Run
brew install python3
- 跑
brew install python3
Once it is installed, it will say that it has installed it, but that you already have Python 2.7 installed. This is nice, but we want to set it to actually see python3 as an option
一旦安装,它会说它已经安装了它,但你已经安装了 Python 2.7。这很好,但我们希望将其设置为实际将 python3 视为一个选项
- Use
brew link
- Confirm by running
python3
- 用
brew link
- 运行确认
python3