urllib.request in Python 2.7
Asked Answered
P

3

25

I can use urllib.request module with Python 3.1. But when I execute the same program using Python 2.7, an error comes along the lines of;

AttributeError: 'module' object has no attribute 'request'.

I believe this error is because theres no request module in urllib for Python 2.7. Because I need to use tweepy i'll have to stick with Python 2.7 since tweepy does not support Python 3.

So how I can use urllib.request module in Python 2.7?

Phallicism answered 19/9, 2010 at 13:0 Comment(0)
T
30

Use urllib2.urlopen for Python 2.

Tress answered 19/9, 2010 at 13:13 Comment(0)
T
37

It is also possible to use six module to make code for both python2 & python3:

from six.moves import urllib
# ...
result = urllib.request.urlopen(url)
Traverse answered 27/1, 2016 at 13:56 Comment(2)
This should be the top comment.Iraidairan
This should be the top commentFaucher
T
30

Use urllib2.urlopen for Python 2.

Tress answered 19/9, 2010 at 13:13 Comment(0)
P
4

Take a look at http://docs.python.org/library/urllib2.html.

The urllib2 module is the predecessor to urllib.request/urllib.error (it has been split into those modules in Python 3.0).

Praline answered 19/9, 2010 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.