Skip to main content
 首页 » 编程设计

python之导入 nltk 无法理解的错误

2025年04月02日27zfyouxi

我刚刚在 CentOS 服务器上安装了 nltk。但是当我进入 python 并尝试导入 nltk 时,出现以下错误。

有什么想法吗?

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()} 

请您参考如下方法:

从 3.1 版开始,NLTK 不再支持 Python < 2.7,来自 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

由于字典理解是 Python >=2.7 的一项功能,https://www.python.org/dev/peps/pep-0274/ , 使用 NLTK >=3.1 会在发生字典理解时导致错误。

强烈建议升级到 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

或者使用 conda 也会简化问题:https://docs.continuum.io/anaconda/install#linux-install

但如果确实需要 Python 2.6:

pip install -U nltk==3.0.5