繁体   English   中英

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

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

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

  1. 在自制软件中安装 python32

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

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

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

/usr/bin/python

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

蟒蛇 2.7.2

我还尝试了以下说明:

酿造链接--覆盖python

或者尝试通过运行以下指令删除自制软件安装的python:

酿造删除蟒蛇

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

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

谁能帮忙,谢谢

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

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

alias python='python3'

brew install python3输出提到:

分别指向 export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH python3python3-configpip3等的未版本化符号链接pythonpython-configpip等安装到 /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

蟒蛇 3.7.0

$ pip --version

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

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

很可能 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 doctorbrew 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。

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

再会!!!

$ brew info python

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

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

$ which python

$ python --version

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

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

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

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

暂无
暂无

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

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