Python ImportError: 无法导入名称itemgetter。

3
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”的模块吗? - zengr
我只是运行Python Shell,而不是Python代码... - changzhi
1
你当前的目录下是否有一个名为 operator.pyoperator.pyc 的文件? - Luke Woodward
4
这段代码的输出结果是:ImportError: No module named 'operator' - Ashwini Chaudhary
1个回答

16

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

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


(感谢 @Ashwini Chaudhary 最先注意到这一点)。 - user9876
不一定是文件。我使用名为operator的目录时也会出现相同的错误。 - user3837712

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接