Faling Dutchman
Faling Dutchman

Reputation: 271

urllib does not have the request attribute

It happend to me a few times the last week, urllib that acts up for some reason.

req = urllib.request.Request(oauth_uri)
req.add_header('User-Agent', "Python client")
resp = urllib.request.urlopen(req, bytes_)
data = resp.read().decode("utf-8")

It works, then it says req = urllib.request.Request(oauth_uri) AttributeError: module 'urllib' has no attribute 'request'and then it as sudden as it acts up, it works again.

Does anyone know how this happens and how to solve it? I need it to function reliable.

Upvotes: 0

Views: 2394

Answers (1)

jDo
jDo

Reputation: 4010

Probably a python 2/3 issue. Sure you're opening your script with python3?

Check this related question.

EDIT:

I think Carpetsmoker figured it out. This works here:

import urllib.request

req = urllib.request.Request("http://example.com")
req.add_header('User-Agent', "Python client")
resp = urllib.request.urlopen(req)
data = resp.read().decode("utf-8")
print(data)

I've no clue why it only works sometimes though. Maybe a subtle bug in your code? It seems extremely odd if python was to suddenly "forget" an attribute of an imported module.

Upvotes: 1

Related Questions