How to Fix Error: No Module Named ‘urlparse’ (Easily)

4/5 - (1 vote)

You may experience the following error message in your Python code when trying to import urlparse:

ModuleNotFoundError: No module named 'urlparse'

>>> import urlparse
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import urlparse
ModuleNotFoundError: No module named 'urlparse'

How to fix it and what is the reason this error occurs?

This error usually occurs because urlparse has been renamed to urllib.parse. So you need to pip install urllib and then import urllib and call urllib.parse to make it work.

To accomplish this, follow the tutorial outlined next or simply try running this command in your terminal, console, shell, or command line:

pip install urllib

After that, you can use urllib.parse like so (source):

from urllib.parse import urlparse
urlparse("scheme://netloc/path;parameters?query#fragment")


o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
             "highlight=params#url-parsing")
print(o)

Output:

ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')

🌍 Recommended Tutorial: How to Install urllib in Python?