0

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?

Community
  • 1
  • 1
saulspatz
  • 5,011
  • 5
  • 36
  • 47

3 Answers3

3

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.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thanks. This sure looks right, but when I try it, I get an exception. Please look at the additional information I've added to my question. – saulspatz Sep 12 '15 at 21:23
  • I still haven't solved my problem, but this has got me headed in the right direction. I think I've got my environment messed up somehow. – saulspatz Sep 13 '15 at 01:00
1

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
Alexander
  • 105,104
  • 32
  • 201
  • 196
  • Thanks. I'll look into this later, but it's a bit more than I want to do just at the moment. – saulspatz Sep 12 '15 at 21:11
  • Although it appears daunting, it is actually quite easy to setup. All environments are self contained, so you don't have to worry about mucking up your system install. You can also easily create different environments depending on your need (as I have above). – Alexander Sep 12 '15 at 21:13
  • I really do mean to try it, but I don't use iPython (another thing I've been meaning to try.) Will it work with just a plain old python REPL? – saulspatz Sep 12 '15 at 21:25
  • It took me just over 10 mins to set up these environments from a brand new user account (including download time). iPython is a nice REPL environment with tab completion, etc. Once you start using it, you'll never go back... – Alexander Sep 12 '15 at 21:36
  • One thought. If I go this route, how will it address my pip problems? The conda docs say that there are packages you can install with pip that you can't install with conda. (Naturally) If I need to do that, how do I get the installation into the correct environment? – saulspatz Sep 13 '15 at 01:03
  • You just pip install any package that Conda doesn't support. Not a problem. – Alexander Sep 13 '15 at 01:17
  • Do you mean that I first activate the appropriate environment with `source activate` and then use pip? – saulspatz Sep 13 '15 at 01:27
  • Yes. Let say you wanted to install the package `pandas`. Just type `source activate python3_env` (or whatever name you used for your environment). Following that command, enter `condo install pandas`. If it is an unsupported package, you can still fall back to `pip install my_package`. – Alexander Sep 13 '15 at 01:41
  • Conda vs pip vs virtualenv: http://conda.pydata.org/docs/_downloads/conda-pip-virtualenv-translator.html – Alexander Sep 13 '15 at 01:42
  • Thanks a lot. I'll stop bugging you now. – saulspatz Sep 13 '15 at 01:53
1

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.

henrikstroem
  • 2,978
  • 4
  • 35
  • 51
  • I do that for major projects, but I also like to have a global environment to use for quick and dirty scripts. The trouble I'm having today suggests that maybe I ought to change my ways, but you know what they say about old dogs and new tricks. – saulspatz Sep 12 '15 at 21:45