python 3(new ):tutorial 22

0 Monika Suryan · March 20, 2015
code isnt working..
gives below error :
C:\Python34\python.exe C:/Users/Monika/PycharmProjects/untitled/
Traceback (most recent call last):
  File "C:/Users/Monika/PycharmProjects/untitled/", line 6, in <module>
    urllib.request.urlretrieve( url ,file_name)
NameError: name 'url' is not defined

Post a Reply


Oldest  Newest  Rating
0 The BM · March 22, 2015
please post the whole code of your script.(Use also the code tags..)
So we can see whats the error....
0 Monika Suryan · March 24, 2015
import random
import urllib.request

def download_web_image(url):
    name = random.randrange(1, 1000)
    file_name = str(name)+".jpg"
    urllib.request.urlretrieve(url, file_name)
0 Jeff T. · March 25, 2015
It looks like your missing an 'L' when you import url library. You have urlib.request, it should be urllib.request. (2) 'l''s.
Try that, the code worked for me, Good Luck!!
0 Monika Suryan · March 25, 2015
i copied the source also not working,,,
0 Jeff T. · March 26, 2015
 What isn't working? Are you getting another error and if so please post it. What are you using to run Python, Pycharm, IDLE,... ?     Your link to the pic is good, I ran it with the link you provided and got the pic no problem.
Make sure you type in the source code, sometimes if you copy and paste the indentations don't line up right and that will cause errors too.
Please provide answers to the above questions we can go from there.
0 Monika Suryan · March 26, 2015
i also went to settings and project interpreter and tried download 'request' so that above code can is not getting downloaded...every time it shows error
0 Monika Suryan · March 26, 2015
0 Jeff T. · March 27, 2015
  You shouldn't have to download anything. 'Request' is a function of the urllib. The urllib is part of the installed libraries/ modules in Python, like the Random library.
   When you type in import and start typing url... does the choices window which appears below where your typing show the urllib as an option like the random option appears when you start typing ra...? If it does, select it and then put the dot in and after the . (dot) request is the first option which shows up in the choices window. If the urllib is not an option it maybe something didn't get installed properly.
  I use the free version of Pycharm and have not downloaded any other modules. In my project interpreter I only have two items listed under packages; 1- setuptools   2- pip. If you don't have those listed under packages try downloading them, both are available.
   One other thing I noticed in your original code is in the function call, you were also missing the second 'L' in urllib.request. It's the line below the .'jpg' line.
   I hope this wasn't too confusing and hopefully it solves the problem.
0 Philippe Trinh · March 27, 2015
I too encountered that error a while ago. As you can see in the window it tells you to run the command 

pip install --user request 

in the system terminal. I'm using a Mac and I ran that command via terminal an and it downloaded the package for me and after that I was able to run the program error free. 

Try and run pip install --user request in Command Prompt for windows. Hope this helps!
  • 1



This section is all about snakes! Just kidding.

Bucky Roberts Administrator