1

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)
Community
  • 1
  • 1
user41965
  • 111
  • 1
  • 7

1 Answers1

0

please use virtualenv, because it is not good if you update or install python packages global, because e.g. ubuntu use some special package versions for their own software and if you change something this could create a lot of problems. You can install it with:

pip install virtualenv

Here is a link with a very good introduction how to use it.

This will probably solve your problem too. I tested it also with ubuntu 16.04 by myself.

ikreb
  • 2,133
  • 1
  • 16
  • 35