29

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

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

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

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

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

/usr/bin/python

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

Python 2.7.2

我还尝试了以下说明:

酿造链接--覆盖python

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

冲泡删除蟒蛇

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

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

谁能帮忙,谢谢

4

6 回答 6

31

如果你想使用 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
于 2013-08-24T14:32:53.050 回答
11

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

alias python='python3'
于 2013-08-24T14:41:01.853 回答
11

brew install python3输出提到:

指向 export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH 的未版本化符号链接 , , 等 已分别python安装python-config到/ usr/local/opt/python /libexec/binpippython3python3-configpip3

因此,添加export PATH=/usr/local/opt/python/libexec/bin:$PATH~/.bash_profile然后运行source ~/.bash_profile会为您提供 Homebrew 创建的那些符号链接 - python=python3、pip=pip3 等 :)

$蟒蛇--版本

Python 3.7.0

$点子--版本

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

于 2018-09-19T11:13:28.943 回答
5

我遇到了同样的问题并做了一些研究。我发现有人在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 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,那么您必须执行几个步骤才能使用最新python3python2版本。以下是步骤:

使用外壳:

  • 在编辑模式下打开~/.bash_loginor ~/.bash_profileor ~/.cshrcor ~/.profileor ~/.tcshrcor ~/.zprofile,无论您使用什么外壳来执行命令。您可能必须使用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。

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

再会!!!

于 2018-12-21T18:52:10.980 回答
0

来自$ 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

于 2017-11-29T08:53:32.963 回答
0

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

  1. 安装自制软件。
/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
于 2019-07-22T00:47:12.040 回答