Mac OS安装Homebrew 以及‘Permission denied’问题解决

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mac OS安装Homebrew 以及‘Permission denied’问题解决相关的知识,希望对你有一定的参考价值。

参考技术A Homebrew就不需要解释是什么了吧, 官方地址戳我 。对它不了解的同学,建议还是读一下人家的文档啦。

安装比较简单,在终端中执行下面这行命令就可以了:

但是,安装到最后,报错啦:

懂点英文的话,可能立刻就知道了:权限不足。
不懂英文也没有关系,查下字典也马上就知道了。

然后,如果我们在安装的命令前面加上‘sudo’是不是就可以了呢?

好吧,貌似不行。那就想别的办法吧,我们还可以给文件夹增加权限:

执行这两句命令之后,重新执行homebrew的命令,done!

下面附上卸载的方法
虽然我不知道你为什么要卸载它,但是如果你执意要这么做的话,可以参考下面的方法:

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

【中文标题】如何让 Mac OS 使用 Homebrew 安装的 python【英文标题】:How to make Mac OS use the python installed by Homebrew 【发布时间】:2013-08-27 11:44:18 【问题描述】:

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

    在自制软件中安装了 python32

    更改了我的 .bash_profile 并在其中添加了以下行:

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

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

/usr/bin/python

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

Python 2.7.2

我还尝试了以下说明:

brew 链接 --overwrite python

或尝试通过运行以下指令来删除由 homebrew 安装的 python:

brew 删除 python

但是以上两条指令都会导致这个错误:

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

谁能帮忙,谢谢

【问题讨论】:

【参考方案1】:

如果你想使用 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

【讨论】:

如果你从 Homebrew 安装 python 3,那么这个 bin 被称为python3 如果您使用自制软件(使用brew install python3)安装python 3,那么要运行python,您应该从终端调用python3,否则您将运行python 2。 当我在终端输入'python3'时,我得到'-bash: python3: command not found' 恕我直言,在这里使用别名可能会令人困惑,只要 python 通常引用版本 2,我无法判断这是否是正常行为,您应该问另一个问题(可能在 superuser.com 中) 如果你不想称它为 python3 而是保持简单,因为 python 运行这些brew unlink python@2 &amp;&amp; brew link python@3。然后将此添加到您的 bash 个人资料中 export PATH="/usr/local/opt/python/libexec/bin:$PATH"【参考方案2】:

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

alias python='python3'

【讨论】:

刚试过,如果我输入'which python',我得到'/usr/bin/python',而'python --version'得到'-bash: python3: command not found'跨度> @photosynthesis 我只是用这个方法让'python'调用'python2.6'。它应该和 python3 一样工作【参考方案3】:

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

【讨论】:

感谢您的回答,我为此尝试了一个小时。 提醒人们不要盲目地将其放入路径中,例如,此答案中建议的目录对我来说不存在。在我的系统上,Python 版本号是路径目录的一部分,我通过自制软件安装了几个版本的 python,所以我需要选择一个来代表 bash 配置文件中的 python3。【参考方案4】:

我遇到了同样的问题并做了一些研究。我发现有人在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 版本。步骤如下:

使用外壳:

在编辑模式下打开~/.bash_login~/.bash_profile~/.cshrc~/.profile~/.tcshrc~/.zprofile,无论您使用什么shell来执行命令。您可能需要使用 sudo 来编辑它们。

添加以下步骤:

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

另外,添加以下备份:

别名 python=python3

使用自制软件:

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

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

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

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

美好的一天!!!

【讨论】:

TL;DR:brew unlink python@2 &amp;&amp; brew link python@3 帮助了我。其他文字将无法阅读。谢谢。【参考方案5】:

来自$ brew info python

此公式将 python2 可执行文件安装到 /usr/local/bin。 如果你希望在你的 PATH 中有这个公式的 python 可执行文件,然后添加 以下到〜/ .bash_profile: 导出 PATH="/usr/local/opt/python/libexec/bin:$PATH"

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

$ which python

$ python --version

【讨论】:

【参考方案6】:

建议在 macOS 上使用 Homebrew 安装。话虽如此,Python 2.7 是 Mac OS 自带的。但是,它已被弃用,很快就会被删除。因此,您应该使用 Python3 和更新版本。

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

一旦安装了 Python,Homebrew 会说安装完成,但是你已经安装了 Python 2.7。这很好,但我们希望将其设置为实际将 python3 视为一个选项

    使用brew link 运行which python3确认,路径应该是/usr/local/bin/python3

【讨论】:

以上是关于Mac OS安装Homebrew 以及‘Permission denied’问题解决的主要内容,如果未能解决你的问题,请参考以下文章

Mac OS : Homebrew安装,配置maven 环境变量

Mac OS X 包管理工具 Homebrew 安装与使用

解决Mac os 安装软件一直卡在 Updating Homebrew

为 xampp 安装 homebrew mac os x

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

如何在Mac OS X上安装 Ruby运行环境