2

I can't import urllib or urllib2 on my Python 2.6 or Python 2.7 installs.

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] 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/lib/python2.7/urllib.py", line 30, in <module>
    from urlparse import urljoin as basejoin
  File "/usr/lib/python2.7/urlparse.py", line 110, in <module>
    from collections import namedtuple
  File "/usr/lib/python2.7/collections.py", line 10, in <module>
    from keyword import iskeyword as _iskeyword
ImportError: cannot import name iskeyword

I can't seem to find an answer Googling "cannot import name iskeyword" either.

rypel
  • 4,686
  • 2
  • 25
  • 36
monkeylytics
  • 51
  • 1
  • 5
  • maybe it's because of a circular import of the urlparse & urllib libraries. check this out. http://thoughtsaby.blogspot.com/2012/11/circular-imports-in-python.html – maheshakya Nov 13 '12 at 05:19

1 Answers1

5

There must be some importing conflicts. Do you have any modules named keyword in you workspace? Try next:

>>> import keyword
>>> dir(keyword)
Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
  • 2
    Ah. Yes, I apparently created a test file a while ago in that folder called keyword.py that isn't used but is causing a collision with urllib. Removing it causes the import urllib to work fine. Thanks for the help! I need to learn the Python namespace better. Any general words of wisdom to avoid something like this happening again where my filenames cause a conflict with modules that are being imported? – monkeylytics May 22 '11 at 19:13
  • 1
    @monkeylytics Nothing special - just to be careful with module names. Now you know what to do with such type of mistakes :) – Roman Bodnarchuk May 22 '11 at 20:06