CodeBird CodeBird - 3 months ago 24
Python Question

import nltk ununderstandable error

I just installed nltk on a CentOS server. But when I go into python and try to import nltk I get the following error.

Any ideas?

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/nltk/__init__.py", line 114, in <module>
from nltk.collocations import *
File "/usr/lib/python2.6/site-packages/nltk/collocations.py", line 38, in <module>
from nltk.util import ngrams
File "/usr/lib/python2.6/site-packages/nltk/util.py", line 1361
d = {k: _default_to_regular(v) for k, v in d.items()}

Answer

Since version 3.1, NLTK drops supports for Python < 2.7, from http://www.nltk.org/news.html:

NLTK 3.1 released : October 2015

Add support for Python 3.5, drop support for Python 2.6, sentiment analysis package and several corpora, improved POS tagger, Twitter package, multi-word expression tokenizer, wrapper for Stanford Neural Dependency Parser, improved translation/alignment module including stack decoder, skipgram and everygram methods, Multext East Corpus and MTECorpusReader, minor bugfixes and enhancements For details see: https://github.com/nltk/nltk/blob/develop/ChangeLog

Since dictionary comprehension is a feature from Python >=2.7, https://www.python.org/dev/peps/pep-0274/, using NLTK >=3.1 will lead to error when a dictionary comprehension occurs.

Strongly encouraged to upgrade to Python >= 2.7: https://github.com/h2oai/h2o-2/wiki/Installing-python-2.7-on-centos-6.3.-Follow-this-sequence-exactly-for-centos-machine-only

Or using conda would simplify the problem too: https://docs.continuum.io/anaconda/install#linux-install

But if Python 2.6 is really necessary:

pip install -U nltk==3.0.5