4

I am trying to install NLTK package in Python 2.7 - I already have it installed in Python 3. So every time I run :

sudo pip install nltk

I get:

Requirement already satisfied: nltk in /anaconda/lib/python3.6/site-packages

Requirement already satisfied: six in /anaconda/lib/python3.6/site-packages (from nltk)

How do i specifically install nltk in python 2.7 instead?

Thanks a lot!

Jay

3 Answers 3

6

The easiest way for install the nltk module with Python 2.7 version is this one:

sudo pip2 install nltk

It will automatically recognize your Python 2.7 version. But you can also be more specific if you have more than one version for Python 2. In that case you could change pip2 to pip2.7. In general the PIP command from version 1.5 supports the pipVERSION argument (see below some examples for different versions of Python environment):

$ pip2.6 install SomePackage # Python 2.6
$ pip2.7 install SomePackage # Python 2.7
$ pip3.6 install SomePackage # Python 3.6

How to solve the sudo:pip2 command not found

(IMPORTANT: be sure of have the correct version of Python 2.7 installed. If you are not sure, please just download it from : https://www.python.org/download/releases/2.7/. For example if you are on Mac machine you need for sure to download it again cause the default version already installed doesn't work properly sometimes with NLTK module).

As the user @kittcar encountered this kind of error I'll show a couple of solutions for find a way around the problem:

  • The first option is to type on command line: easy_install pip This will automatically install all the dependencies for your current Python versions. (See the picture below)

execution of easy_install pip command

IMPORTANT: If you don't have the easy_install command just run:

curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python

  • The second option (if for some reasons the first option doesn't work) is to type:

curl -O https://bootstrap.pypa.io/get-pip.py and python27 get-pip.py

Basically you take the source from the target url and then you install PIP for Python 2.7 version.

  • The third option is to use the conda instead of pip command if you use (like in my personal case) the Anaconda Environment and you want to install the nltk module fastly. In that case you just need to follow these steps:

    1. Download the zip source: https://gist.github.com/danielfrg/d17ffffe0dc8ed56712a0470169ff546.
    2. Extract the folder and rename as "nltk-with-data".
    3. Change directory to one directory above the nltk-with-data directory with cd command.
    4. Run conda build for different Python versions that you need, selecting the packages for the platform and OS you are running the command on.

Below the command list:

conda build nltk-with-data --python 2.7 # you need this one! :-)
conda build nltk-with-data --python 3.4
conda build nltk-with-data --python 3.5
conda build nltk-with-data --python 3.6

Finally you just need to run conda install nltk-with-data and ipython for conclude the nltk installation. And then you just need to type:

import nltk.corpus
nltk.corpus.treebank

As you can see from my screenshot everything went fine and I have successfully installed the nltk module for Python 2.7 with the Anaconda Environment:

nltk-conda-install

Feel free to ask me everything, in particular let me know if you successfully fixed your problem or not. If not, please update your question with command line error logs and your current machine details. So I can understand better what exactly causes your problem and I can suggest you the worth solution for solve it.

Sign up to request clarification or add additional context in comments.

11 Comments

Yah I tried : sudo pip2 install nltk , it says : sudo:pip2 command not found
Probably you didn't install PIP for Python 2.7 but only PIP3 (it's just for Python 3 version). Try easy_install pip
@UgoL yes. Actually this is a possibility. For completeness try to see also the PIP version installed by typing one of the following: pip -V or pip --version or pip list
yah it only shows it installed on python 3.6 pip 9.0.1 from /anaconda/lib/python3.6/site-packages (python 3.6) ........Any idea how to install it in python 2.7.....easy_install pip didn't do it
@kyttcar be sure to have Python 2.7 correctly installed (be sure of this point). If you have it, try pip install --upgrade pip. If it doesn't work you need to try this: curl -O https://bootstrap.pypa.io/get-pip.py python27 get-pip.py Basically you are trying to install the specific PIP version for Python 2.7 from a specific url.
|
4

Unfortunately, the solutions given in the 2 answers posted above did not work for me. Both pip and pip2 were installing the same new version of nltk that was not compatible with python2.7. Thankfully, the accepted answer of another question, Install older (but stable) NLTK version compatible with python 2, solved the problem. The solution is to specify explicitly the version of nltk to be installed when using the pip command:

pip install nltk==3.4.5

Versions of nltk higher than 3.4.5 happen to be incompatible with Python 2

Comments

2

you should use different pip for python 2 and 3. Or just virtual env. Anyway, another possible idea to Giulio Bambini's response is:

python2.7 -m pip install <module>

with

sudo

if necessary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.