Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing NLTK in Python 2.7 when it already exists in Python 3

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

like image 689
kyttcar Avatar asked Jan 28 '23 23:01

kyttcar


2 Answers

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.

like image 99
Giulio Bambini Avatar answered Mar 02 '23 14:03

Giulio Bambini


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

like image 45
so2 Avatar answered Mar 02 '23 15:03

so2