【问题标题】:Python ImportError: cannot import name itemgetterPython ImportError:无法导入名称itemgetter
【发布时间】:2014-02-09 09:59:36
【问题描述】:
Python 2.7.5 (default, Sep 12 2013, 12:43:04) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/urllib.py", line 33, in <module>
  from urlparse import urljoin as basejoin
File "/usr/local/lib/python2.7/urlparse.py", line 118, in <module>
  from collections import namedtuple
File "/usr/local/lib/python2.7/collections.py", line 9, in <module>
  from operator import itemgetter as _itemgetter, eq as _eq
ImportError: cannot import name itemgetter

当我运行 import urllib 时会出现此问题。 Python 版本是 2.7。这是 Python 的版本问题吗?谁能告诉我怎么解决?

【问题讨论】:

  • 你的代码中有名为“operator”的模块吗?
  • 我只是运行 python shell,而不是 python 代码...
  • 当前目录下是否有名为operator.pyoperator.pyc的文件?
  • import operator;print operator.__file__ 这个输出是什么?

标签: python urllib


【解决方案1】:

您在当前目录中有一个名为 operator.py 的文件,因此 import operator 正在选择您的模块,而不是 Python 标准库模块 operator

您应该重命名您的文件,以免与 Python 的标准库冲突。

【讨论】:

  • (感谢@Ashwini Chaudhary 率先发现这一点)。
  • 不一定是文件。我在一个名为 operator 的目录中遇到了同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 2014-09-27
  • 2020-04-09
  • 2016-12-24
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多