Python 2.7.10 error "from urllib.request import urlopen" no module named request

Steve Maguire picture Steve Maguire · Jul 24, 2015 · Viewed 101.3k times · Source

I opened python code from github. I assumed it was python2.x and got the above error when I tried to run it. From the reading I've seen Python 3 has depreciated urllib itself and replaced it with a number of libraries including urllib.request.

It looks like the code was written in python 3 (a confirmation from someone who knows would be appreciated.) At this point I don't want to move to Python 3 - I haven't researched what it would do to my existing code.

Thinking there should be a urllib module for Python 2, I searched Google (using "python2 urllib download") and did not find one. (It might have been hidden in the many answers since urllib includes downloading functionality.) I looked in my Python27/lib directory and didn't see it there. Can I get a version of this module that runs on Python27? Where and how?

Answer

NevDev picture NevDev · Jul 24, 2015

Try using urllib2:

https://docs.python.org/2/library/urllib2.html

This line should work to replace urlopen:

from urllib2 import urlopen

Tested in Python 2.7 on Macbook Pro

Try posting a link to the git in question.