Cannot install python module urlparse

some program written in Python 2.7 complains that ImportError: No module named 'urlparse'. So I need to install the module, but I am not able to do it. The module does exist, it is described e.g. at https://docs.python.org/2/library/urlparse.html. However, neither apt-get install, nor pip install are able to find a module named urlparse, python-urlparse, urllib, python-urllib... -- I'm getting messages like Could not find any downloads that satisfy the requirement ... The only exception is the package python-urllib3 which probably contains the needed files but for Python 3 and installation of which did not help.

I've installed pip, not pip3 since I need the module for Python 2 (pip 1.4.1 from /usr/lib/python2.7/dist-packages (python 2.7)). My Ubuntu is Xubuntu 13.10.

Where's the problem, please? Is pip searching for the module in the right places? I don't know what locations it should search in...


urlparse is part of the standard Python 2 library. It's shipped as part of Python; it isn't packaged separately on PyPI et al. urlparse.urlparse (the function) was renamed in Python 3 to urllib.parse.

So, a few things to look at:

  • Your Python 2 program might be running under Python 3. Check the launch script to see how it picks which version of Python. It should probably start #!/usr/bin/env python2 but also double check (by running env python2) that that loads up Python 2.

  • Something has eaten /usr/lib/python2.7/urlparse.py, in which case reinstall the libpython2.7-stdlib package with:

    sudo apt-get install --reinstall libpython2.7-stdlib
    
  • Or you have a local file causing mischief...


If you need to write code which is Python2 and Python3 compatible you can use the following import

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

If you are using python 2 install it using

pip install urlparse2