Python ImportError: cannot import name itemgetter

我们两清 提交于 2021-02-02 08:33:37

问题


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

This issue occurs when I run import urllib. Python verison is 2.7. Does is Python's version problem ? Could someone tell me how to fix it ? Thanks a lot !


回答1:


You have a file called operator.py in the current directory, so import operator is picking up your module and not the Python standard library module operator.

You should rename your file to not conflict with Python's standard library.



来源:https://stackoverflow.com/questions/21657685/python-importerror-cannot-import-name-itemgetter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!