Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I run the following code in Python 3.3:

import urllib
tempfile = urllib.request.urlopen("http://yahoo.com")

I get the following error:

enter image description here

I did this too to verify:

enter image description here

What am I doing wrong?

like image 910
Pruthvi Raj Avatar asked Mar 09 '14 06:03

Pruthvi Raj


3 Answers

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


Import urllib.request instead of urllib.

import urllib.request
like image 55
falsetru Avatar answered Nov 07 '22 23:11

falsetru


Interestingly, I noticed some IDE-depending behavior.

Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do

import urllib.request

while in Spyder,

import urllib

does fine

like image 26
JB Lepetit Avatar answered Nov 08 '22 00:11

JB Lepetit


If this is on PyCharm, as was mine, make sure your file name isn't urllib.py.

like image 1
Anonymous Person Avatar answered Nov 07 '22 23:11

Anonymous Person