urllib.request not working

+1 Rishabh gupta · December 29, 2014
i am trying to download a image from the web  using urllib.request,but i am getting import error which says "no module named request"


i am using pycharm 4.0.3 which actually doesn't have urllib.request module .so i installed a package named "yieldfrom.urllib.request" but still i am getting invalid syntax error .

my code:
import yieldfrom.urllib.request

name="messi.jpg"
url="http://cdn.sportabsorb.com/wp-content/uploads/2014/09/messi-2.jpg"

yieldfrom.urllib.request.urlrertrieve(url,name)

can anyone explain what am i doing wrong ..??

Post a Reply

Replies

Oldest  Newest  Rating
0 Rishabh gupta · December 30, 2014
sorted 

i actually named my file urllib.py...and basically this was the only reason of why i kept getting errors ..

changing  the name of file to url.py sorted the issue ..in future if anyone encounters this issue ,just make sure you keep your files
name different from modules or packages name... 


and thanks vaggelis
0 Vaggelis Theodoridis · December 30, 2014
Oh, now that im thinking it again, maybe it cant find the module urllib because in path there is only python2. Check it ;)
0 Vaggelis Theodoridis · December 30, 2014
Im sorry but it should work. Something is meshed up.
Does it look inside python when importing? Print ur sys path so we can see from where does it look for modules:
import sys
print(sys.path)

Also, how u named the file? maybe a strange name like urllib.py? It will mesh things up with python..
0 Rishabh gupta · December 30, 2014
 i was using the python 2.7.9 interpreter so i changed it to 3.4 but i still i am getting the error...
my code:
import urllib.request

urllib.request.urlretrieve("https://scontent-b-sin.xx.fbcdn.net/hphotos-xpf1/v/t1.0-         

9/969714_4906822960850_92143533_n.jpg?oh=08128d1879a090836428db59abc6f588&oe=55410389","abc.jpg")

and this is what i get ..

/images/forum/upload/2014-12-30/f9759a5312057e1d371df4cbdb2462fe.JPG
0 Vaggelis Theodoridis · December 30, 2014
Maybe you have installed python 2?
If so, in python 2 its urllib2, so you should use urllib2.urlopen(url)
In python 3 became urllib where we import urllib.request and then we use urllib.request.urlopen(url)
  • 1

Python

107,097 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator