使用 Homebrew 在 Yosemite 上安装 pip

pip Install on Yosemite with Homebrew

我昨天问了一个 关于这个问题,一些好心人帮助了我,但我想我只是愚蠢。我 运行 Yosemite Mac。我已经用 Homebrew 安装了 python2 和 python3,并且我从 Homebrew 获得了 pip 和 pip3。

现在我使用 pip3 install nltk` 安装了 nltk,它在 python 3 上运行良好,但当然在 python 2 上运行不正常。

所以我尝试了 pip install nltk 并且得到了

pip install nltk
Requirement already satisfied (use --upgrade to upgrade): nltk in /usr/local/lib/python3.4/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.9.0 in /usr/local/lib/python3.4/site-packages (from nltk)

然后我试了

pip install --upgrade nltk
Requirement already up-to-date: nltk in /usr/local/lib/python3.4/site-packages
Requirement already up-to-date: six>=1.9.0 in /usr/local/lib/python3.4/site-packages (from nltk)

当然,我仍然无法在 python 2.

中导入 nltk

我以前用pip和pip3都没出过问题,也没有刻意换过环境。如何让 import nltk 在 python 2 中工作?

按照建议,我尝试获取 pip 的版本:

pip -V
pip 7.1.2 from /usr/local/lib/python2.7/site-packages (python 2.7) 

原来是这样python2,和我想的一样

我也按照推荐的方式尝试了 pip2,但出现异常:

pip2 install nltk
Collecting nltk
  Using cached nltk-3.0.5.tar.gz
Collecting six>=1.9.0 (from nltk)
  Using cached six-1.9.0-py2.py3-none-any.whl
Building wheels for collected packages: nltk
  Building wheel for nltk failed: [Errno 13] Permission denied: '/Users/saul/Library/Caches/pip/wheels/f6'
Failed to build nltk
Installing collected packages: six, nltk
  Found existing installation: six 1.8.0
    Uninstalling six-1.8.0:
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/pip/basecommand.py", line 211, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/site-packages/pip/commands/install.py", line 311, in run
    root=options.root_path,
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_set.py", line 640, in install
    requirement.uninstall(auto_confirm=True)
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_install.py", line 716, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_uninstall.py", line 125, in remove
    renames(path, new_path)
  File "/usr/local/lib/python2.7/site-packages/pip/utils/__init__.py", line 315, in renames
    shutil.move(old, new)
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 303, in move
    os.unlink(src)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/six-1.8.0.dist-info/DESCRIPTION.rst'

最后一行真的很奇怪,因为它表明正在尝试安装的不是我用 Homebrew 安装的 python 2,而是 [=50 附带的 python 2 =].

你能告诉我哪里出了问题以及如何纠正吗?

听起来您系统的默认 Python 是 Python3;这就是 pip 指向 Python 3 站点包位置的原因。

要在 Python 2 上安装 nltk,请尝试 pip2 install nltk;然后键入 python2 以启动 Python 2.

另一种解决方案是使用 miniconda 安装程序并设置单独的环境。

here. Quick install instructions are here 下载 miniconda。摘要如下:

OS X Miniconda install In your browser download the Miniconda installer for OS X, then in your terminal window type the following and follow the prompts on the installer screens. If unsure about any setting, simply accept the defaults as they all can be changed later.

bash Miniconda3-latest-MacOSX-x86_64.sh

Now close and re-open your terminal window for the changes to take effect.

To test your installation, enter the command conda list. If installed correctly, you will see a list of packages that were installed.

Then to update all of the packages, type conda update conda.

要从终端设置您的环境并包括 nltk 和 ipython 包(以及所有依赖项):

$ conda create -n python2_env python=2 nltk ipython pyqt qtconsole
$ conda create -n python3_env python=3 nltk ipython pyqt qtconsole

要激活您的环境:

source activate python2_env

然后从您活动环境中的终端输入以下内容以启动 iPython 控制台 window:

$ ipython qtconsole --pylab=inline

我强烈建议使用 virtualenv 而不是 pip 安装到系统中。

使用 virtualenv 您可以隔离每个项目的环境,并指定您是哪个 Python 运行。