saulspatz
saulspatz

Reputation: 5261

pip Install on Yosemite with Homebrew

I asked a question about this yesterday, and some nice people helped me, but I guess I'm just stupid. I'm running Yosemite on my Mac. I've installed both python2 and python3 with Homebrew, and I've gotten both pip and pip3 from Homebrew.

Now I installed nltk with pip3 install nltk` and it works fine on python 3, but not, of course, on python 2.

So I tried pip install nltk and I got

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)

Then I tried

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)

And of course, I still can't import nltk in python 2.

I haven't had any trouble with pip and pip3 in the past, and I haven't intentionally changed my environment. How can get import nltk to work in python 2?

As suggested, I tried getting the version of pip:

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

So it looks like it python 2, as I thought.

I also tried pip2, as recommended, but I got an exception:

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'

The last line is really strange, because it suggests that installation is being attempted not in the python 2 that I installed with Homebrew but in the python 2 that comes with the Mac.

Can you tell me what's wrong and how to rectify it?

Upvotes: 0

Views: 883

Answers (3)

Alexander
Alexander

Reputation: 109546

An alternative solution is to use the miniconda installer and set up separate environments.

Download miniconda from here. Quick install instructions are here. A summary is below:

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.

To set-up your environments from the terminal and include both the nltk and ipython packages (together with all dependencies):

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

To activate your environment:

source activate python2_env

Then type the following from a terminal within your active environment to fire up an iPython console window:

$ ipython qtconsole --pylab=inline

Upvotes: 1

henrikstroem
henrikstroem

Reputation: 3068

I would strongly suggest using virtualenv rather than pip installing into the system.

Using virtualenv you can isolate the environment for each of your projects, and specify which Python you are running.

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174624

It sounds like the default Python for your system is Python3; which is why pip is pointing to the Python 3 site-packages location.

To install nltk on Python 2, try pip2 install nltk; and then type python2 to launch Python 2.

Upvotes: 3

Related Questions