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
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
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:
easy_install pip
This will automatically install all the dependencies for your current Python versions. (See the picture below)IMPORTANT: If you don't have the easy_install
command just run:
curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python
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:
cd
command.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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With