Maria
Maria

Reputation: 23

urllib.parse vs urlparse: How do I get python 2.7 to recognize urllib?

I've seen some topics related to this same issue, but have been unable to find a solution that fits my situation. While trying to run a python library, I received the error ImportError: No module named urllib.parse, which I understand is because I'm running Python 2.7 and not Python 3. I do have urllib3 in my python folder, it seems like it's just unable to access it because of the name difference. I am not able to change this in the source code, nor do I want to switch to Python 3. Is there any other way to solve this problem and get Python 2.7 to recognize urllib3?

Upvotes: 1

Views: 5143

Answers (1)

Boris Burkov
Boris Burkov

Reputation: 14436

Version-compatible solution (tested on 2.7.10 and 3.6.0):

import six

if six.PY2:
    from urlparse import urlparse
elif six.PY3:
    from urllib.parse import urlparse

Upvotes: 2

Related Questions