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

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?

This question is related to python urllib

The answer is


Instead of using urllib.request.urlopen() remove request for python 2.

urllib.urlopen() you do not have to request in python 2.x for what you are trying to do. Hope it works for you. This was tested using python 2.7 I was receiving the same error message and this resolved it.


from urllib.request import urlopen, Request

Should solve everything


You are right the urllib and urllib2 packages have been split into urllib.request , urllib.parse and urllib.error packages in Python 3.x. The latter packages do not exist in Python 2.x

From documentation -

The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.

From urllib2 documentation -

The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error.

So I am pretty sure the code you downloaded has been written for Python 3.x , since they are using a library that is only present in Python 3.x .

There is a urllib package in python, but it does not have the request subpackage. Also, lets assume you do lots of work and somehow make request subpackage available in Python 2.x .

There is a very very high probability that you will run into more issues, there is lots of incompatibility between Python 2.x and Python 3.x , in the end you would most probably end up rewriting atleast half the code from github (and most probably reading and understanding the complete code from there).

Even then there may be other bugs arising from the fact that some of the implementation details changed between Python 2.x to Python 3.x (As an example - list comprehension got its own namespace in Python 3.x)

You are better off trying to download and use Python 3 , than trying to make code written for Python 3.x compatible with Python 2.x


For now, it seems that I could get over that by adding a ? after the URL.


You can program defensively, and do your import as:

try:
    from urllib.request import urlopen
except ImportError:
    from urllib2 import urlopen

and then in the code, just use:

data = urlopen(MIRRORS).read(AMOUNT2READ)

Change

from urllib.request import urlopen

to

from urllib import urlopen

I was able to solve this problem by changing like this. For Python2.7 in macOS10.14


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.