我成功安装了自然语言工具包nltk。然后我在控制台中运行Python并键入" import nltk"我收到以下错误。我不知道为什么,我无法在网上找到任何东西。为什么会出现这种情况的任何建议?
这是我得到的错误:
>>> 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
可能存在一些冲突,还是我错过了先决条件?
当我做pip冻结时,我得到以下内容:
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输出
bos-myusername:Demo myusername$ ls
Contact.csv removeStopWords.py tokenize.pyc
genDb.py tokenize.py
答案 0 :(得分:3)
我可以从回溯中看到您在当前目录中有一个名为tokenize.py
的文件。重命名该文件并删除tokenize.pyc
,这样他们就不会影响标准库tokenize
的导入。