本文介绍了无法为nltk导入名称defaultdict错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功安装了自然语言工具包 nltk .然后,我在控制台中运行Python并键入"import nltk",然后出现以下错误.我不知道为什么,网上找不到任何东西.为什么会有这种情况的任何建议?

I successfully installed the Natural Language Toolkit nltk. Then I ran Python in the console and typed "import nltk" and I get the following error. I have no idea why and I can't find anything online. Any suggestions on why this is the case?

这是我得到的错误:

>>> import nltk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/myusername/anaconda/lib/python2.7/site-packages/nltk/__init__.py", line 114, in <module>
    from nltk.collocations import *
  File "/Users/myusername/anaconda/lib/python2.7/site-packages/nltk/collocations.py", line 37, in <module>
    from nltk.probability import FreqDist
  File "/Users/myusername/anaconda/lib/python2.7/site-packages/nltk/probability.py", line 47, in <module>
    from collections import defaultdict, Counter
  File "/Users/myusername/anaconda/lib/python2.7/site-packages/nltk/collections.py", line 13, in <module>
    import pydoc
  File "/Users/myusername/anaconda/lib/python2.7/pydoc.py", line 56, in <module>
    import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings
  File "/Users/myusername/anaconda/lib/python2.7/inspect.py", line 39, in <module>
    import tokenize
  File "tokenize.py", line 2, in <module>
    from nltk.corpus import stopwords
  File "/Users/myusername/anaconda/lib/python2.7/site-packages/nltk/corpus/__init__.py", line 64, in <module>
    from nltk.tokenize import RegexpTokenizer
  File "/Users/myusername/anaconda/lib/python2.7/site-packages/nltk/tokenize/__init__.py", line 67, in <module>
    from nltk.tokenize.mwe      import MWETokenizer
  File "/Users/myusername/anaconda/lib/python2.7/site-packages/nltk/tokenize/mwe.py", line 31, in <module>
    from nltk.util import Trie
  File "/Users/myusername/anaconda/lib/python2.7/site-packages/nltk/util.py", line 21, in <module>
    from collections import defaultdict, deque
ImportError: cannot import name defaultdict

是否可能存在一些冲突,或者我缺少先决条件?

Could there some conflicts or am I missing a prerequisite?

当我冻结点子时,我得到以下信息:

When i do pip freeze I got the following:

bos-myusername:Demo myusername$ pip freeze
alabaster==0.7.7
anaconda-client==1.4.0
anaconda-navigator==1.1.0
appnope==0.1.0
appscript==1.0.1
argcomplete==1.0.0
astropy==1.1.2
Babel==2.2.0
backports-abc==0.4
backports.ssl-match-hostname==3.4.0.2
beautifulsoup4==4.4.1
bitarray==0.8.1
blaze==0.9.1
bokeh==0.11.1
boto==2.39.0
Bottleneck==1.0.0
cdecimal==2.3
certifi==2017.11.5
cffi==1.5.2
chardet==3.0.4
chest==0.2.3
click==6.7
cloudpickle==0.1.1
clyent==1.2.1
colorama==0.3.7
conda==4.0.8
conda-build==0+unknown
conda-env==2.4.5
conda-manager==0.3.1
configobj==5.0.6
cryptography==1.3
cycler==0.10.0
cymem==1.31.2
Cython==0.23.4
cytoolz==0.8.2
dask==0.8.1
datashape==0.5.1
decorator==4.0.9
dill==0.2.7.1
Django==1.11.2
docutils==0.12
dynd==0.7.3.dev1
enum34==1.1.2
et-xmlfile==1.0.1
fastcache==1.0.2
Flask==0.13.dev0
Flask-Cors==2.1.2
ftfy==4.4.3
funcsigs==0.4
futures==3.0.3
gevent==1.1.0
greenlet==0.4.9
grin==1.2.1
h5py==2.5.0
HeapDict==1.0.0
html5lib==0.999999999
idna==2.6
ipaddress==1.0.14
ipykernel==4.3.1
ipython==4.1.2
ipython-genutils==0.1.0
ipywidgets==4.1.1
itsdangerous==0.24
jdcal==1.2
jedi==0.9.0
Jinja2==2.8
jsonschema==2.4.0
jupyter==1.0.0
jupyter-client==4.2.2
jupyter-console==4.1.1
jupyter-core==4.1.0
llvmlite==0.9.0
locket==0.2.0
lxml==3.6.0
MarkupSafe==0.23
matplotlib==1.5.1
mistune==0.7.2
mpmath==0.19
msgpack-numpy==0.4.2
msgpack-python==0.4.8
multipledispatch==0.4.8
murmurhash==0.28.0
nbconvert==4.1.0
nbformat==4.0.1
networkx==1.11
nltk==3.2.5
nose==1.3.7
notebook==4.1.0
numba==0.24.0
numexpr==2.5
numpy==1.13.3
odo==0.4.2
openpyxl==2.3.2
pandas==0.18.0
partd==0.3.2
path.py==0.0.0
pathlib==1.0.1
patsy==0.4.0
pep8==1.7.0
pexpect==4.0.1
pickleshare==0.5
Pillow==3.1.1
plac==0.9.6
ply==3.8
preshed==1.0.0
psutil==4.1.0
ptyprocess==0.5
py==1.4.31
pyasn1==0.1.9
PyAudio==0.2.7
pycosat==0.6.1
pycparser==2.14
pycrypto==2.6.1
pycurl==7.19.5.3
pyflakes==1.1.0
Pygments==2.1.1
pyOpenSSL==0.15.1
pyparsing==2.0.3
pytest==2.8.5
python-dateutil==2.5.1
pytz==2016.2
PyYAML==3.11
pyzmq==15.2.0
QtAwesome==0.3.2
qtconsole==4.2.0
QtPy==1.0
redis==2.10.3
regex==2017.4.5
requests==2.18.4
rope==0.9.4
scikit-image==0.12.3
scikit-learn==0.17.1
scipy==0.17.0
simplegeneric==0.8.1
singledispatch==3.4.0.3
six==1.11.0
sklearn==0.0
snowballstemmer==1.2.1
sockjs-tornado==1.0.1
spacy==2.0.3
Sphinx==1.3.5
sphinx-rtd-theme==0.1.9
spyder==2.3.8
SQLAlchemy==1.0.12
statsmodels==0.6.1
sympy==1.0
tables==3.2.2
termcolor==1.1.0
terminado==0.5
thinc==6.10.1
toolz==0.8.2
tornado==4.3
tqdm==4.19.4
traitlets==4.2.1
ujson==1.35
unicodecsv==0.14.1
urllib3==1.22
virtualenv==15.1.0
wcwidth==0.1.7
webencodings==0.5.1
Werkzeug==0.11.4
wrapt==1.10.11
xlrd==0.9.4
XlsxWriter==0.8.4
xlwings==0.7.0
xlwt==1.0.0

这是当前目录的ls的输出

This is the output of ls for the current directory

bos-myusername:Demo myusername$ ls
Contact.csv     removeStopWords.py  tokenize.pyc
genDb.py        tokenize.py

推荐答案

从回溯中可以看到,当前目录中有一个名为tokenize.py的文件.重命名该文件并删除tokenize.pyc,这样它们就不会掩盖标准库的 tokenize .

I can see from the traceback that you have a file called tokenize.py in the current directory. Rename that file and delete the tokenize.pyc, so they're not shadowing the import of the standard library's tokenize.

这篇关于无法为nltk导入名称defaultdict错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:15