I've just installed nltk through pip using the command:
sudo pip install -U nltk
I've also installed numpy immediately in similar way, I tried to import nltk and test and typed 'import nltk' after typing 'python' in terminal, then I got this:
>>> import nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "nltk.py", line 3, in <module>
from nltk.collocations import *
ImportError: No module named collocations
>>>
I tried to find solution online and found this link Importing Libraries Issue - "ImportError: No Module named ____",
so I tried this command: export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
, but nothing changed maybe because there the module itself is not found. I also tried to use the command
sudo pip install -U collocations
but it says
Collecting collocations
Could not find a version that satisfies the requirement collocations (from versions: )
No matching distribution found for collocations
When I first tried it also said You are using pip version 8.1.1, however version 9.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
I couldn't upgrade directly but could upgrade when I used: sudo -H pip install --upgrade pip
But still I'm getting the same thing. I'm really new to python and like to know if I'm doing anything wrong. I had to reinstall Ubuntu 16.04 recently so I think the operating system is working fine. Thank you very much
Edit: So there was a file called nltk.py in my home folder, maybe that was effecting this, I've removed the file but still it's getting created somehow when I try to import nltk and printing the same thing as above traceback.... The contents of the file were:
import sys
import nltk
from nltk.collocations import *
bigram_measures = nltk.collocations.BigramAssocMeasures()
trigram_measures = nltk.collocations.TrigramAssocMeasures()
# change this to read in your data
finder = BigramCollocationFinder.from_words(
nltk.corpus.genesis.words('annotation/dataset.txt'))
# only bigrams that appear 3+ times
finder.apply_freq_filter(3)
# return the 10 n-grams with the highest PMI
finder.nbest(bigram_measures.pmi, 10)