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

7

22

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?

Skylar answered 24/7, 2015 at 2:28 Comment(0)
D
39

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.

Disentitle answered 24/7, 2015 at 2:43 Comment(1)
This definitely works. You should have to replace urllib.request.urlopen with just urllib2.urlopenReservoir
M
9

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)
Mansard answered 22/6, 2017 at 11:31 Comment(0)
H
8
from urllib.request import urlopen, Request

Should solve everything

Haft answered 20/11, 2016 at 16:6 Comment(6)
Yea this works for me, not sure why u got down voted by othersRaymonderaymonds
Still same message here on Heroku: No module named requestNepali
If there is no urllib.request, it's not possible to import its sub-module.Synthetic
@Nepali Did you ever figure out why Heroku couldn't find requests?Leung
sorry @Siddhartha, to be honest i don't remember what i did back there, but considering that i no longer have any python apps running on Heroku i guess i gave up trying to figure it out. My jobs are running on Azure today.Nepali
@Nepali no worries, thanks for the reply. I ended up moving to an alternate python library called requests, it's almost the same, instead of urllib.open(url), you do requests.get(url).contentLeung
D
7

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.

Darice answered 4/12, 2015 at 1:33 Comment(0)
V
5

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

Voluble answered 21/1, 2019 at 7:29 Comment(0)
M
2

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

Malvern answered 24/7, 2015 at 3:51 Comment(1)
Thanks all. As it turned out it wasn't hard to translate to Python 2.7: just this reference to urllib and a use of it with .decode('utf-8'). I appreciate the help.Skylar
H
0

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

Hittel answered 4/5, 2016 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.