Search code examples
pythonhttplib

httplib does not work in script


On loading httplib in terminal returns no error, but when I load the library in a script, I got this error:

File "test2.py", line 1, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 74, in <module>
from urlparse import urlsplit
File "/home/nakisa/Desktop/testURL/urlparse.py", line 2, in <module>
from tld import get_tld
File "/home/nakisa/Desktop/testURL/src/tld/src/tld/__init__.py",  
line    9, in <module>
from tld.utils import get_tld, get_tld_names, update_tld_names, Result
File "/home/nakisa/Desktop/testURL/src/tld/src/tld/utils.py", line 
 13, in <module>
from six.moves.urllib.parse import urlparse
ImportError: cannot import name urlparse

Does anybody have any suggestion?


Solution

  • You have a file

    /home/nakisa/Desktop/testURL/urlparse.py
    

    which is shadowing the built-in Python library urlparse that is required by httplib.

    Rename your file.

    Edit:

    This is what is happening on your machine.

    $ cat so.py
    import httplib
    $ cat urlparse.py
    # this is empty
    $ python2.7 so.py
    Traceback (most recent call last):
      File "so.py", line 1, in <module>
        import httplib
      File "/usr/lib/python2.7/httplib.py", line 74, in <module>
        from urlparse import urlsplit
    ImportError: cannot import name urlsplit
    

    Edit 2:

    Rename your file urlparse.py to something else like myurlparse.py. Then you can import httplib.