Search code examples
pythonsyntaxurllib2urllibtraceback

Python can't import urllib2 (gives an error)


I just started with programming, I wanted to import the urllib resource but when I just use this code:

import urllib2

It worked 2 months ago (took a break). It gives me this error:

    Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import urllib2
  File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
    import httplib
  File "/usr/lib/python2.7/httplib.py", line 1157, in <module>
    import ssl
  File "/usr/lib/python2.7/ssl.py", line 58, in <module>
    import textwrap
  File "/usr/lib/python2.7/textwrap.py", line 10, in <module>
    import string, re
  File "/home/younes/Python/string.py", line 11
    for total:
             ^

I used Python 2 for that, also when using Python 3 it doesn't find the module. (it also doesn't find the urllib3 module?) I have reinstalled Python but it didn't work. I am using Ubuntu 14.04.


Solution

  • The file /home/younes/Python/string.py is preventing import of the standard library module string which in turn prevent import of modules depending on the string module. In addition to that, the file has an syntax error.

    Remove the file or rename the file. If there's /home/younes/Python/string.pyc, also remove/rename it.