21

I know I have seen the answer somewhere a couple of weeks ago but I can't find it now.

Simple urllib.urlretrieve in Python 3.3. How do you do it? I'm trying to download an mp4/html(Page does not exist) scenario, either the page if it does not exist or the mp4 if it does(I can delete the file if the file size is to small or keep it otherwise).

The code I have runs fine and does exactly what I want it to do in 2.7, but it doesn't work in 3.3.

I'm dealing with both giving me headaches, Tkinter and urllib between the two version of Python. Tkinter works fine in 3.3, but urllib doesn't and urllib works fine in 2.7 but Tkinter doesn't. How do I get this to download correctly in 3.3?

I know 3.3 has changed the urllib but I can't find what I saw a week or two ago to guide me in the correct direction.

2.7 does pretty much nothing when I add the Tkinter change over in and 3.3 just comes up and gives me urllib doesn't have an attribute 'urlretrieve;. I prefer 3.3.

confused
  • 1,283
  • 6
  • 21
  • 37
  • Please post the code you have that works in 2.7. – senshin Jan 16 '14 at 20:05
  • 3
    In Python 3 `urlretrieve` is in [`urllib.request`](http://docs.python.org/3.3/library/urllib.request.html). – eskaev Jan 16 '14 at 20:07
  • I realize and have beat myself on the head trying import urllib.request followed by urllib.request.urlretrieve. It doesn't function the same. Instead of coming up and downloading the html file that by its size tells me that it isn't the mp4 file I'm looking for instead it keeps coming up and giving me HTTTPError: HTTP Error 404 Not Found. In 2.7 it downloads the html file that I delete, in 3.3 it just flags me back the error. Is there a way around that? – confused Jan 16 '14 at 20:23
  • Sorry, just paid more attention to the answer below and it partially worked but I do see I have another problem to deal with. I'm doing this inside of threads. It's now giving me the error 'global name "urllib" is not defined. Do I have to import into each of the threads? – confused Jan 16 '14 at 20:27
  • Note that `urlretrieve` is specifically called a "legacy function" that may get deprecated soon in the docs. And in 2.7, it was in a "legacy module," `urllib`, that should not be used in new code. So… why are you using it in the first place? – abarnert Jan 16 '14 at 20:34
  • Also, the answer to this question is right at the top of the documentation for 2.7's [`urllib`](http://docs.python.org/2.7/library/urllib.html). And if you want someone else to fix it for you, the `2to3` tool does it automatically, which is a lot easier than posting it online and trying to get humans to do the same job. – abarnert Jan 16 '14 at 20:34
  • How do you define "doesn't" in the statement "urllib works fine in 2.7 but Tkinter doesn't."? tkinter works perfectly fine in 2.7 unless you have a broken installation. – Bryan Oakley Jan 16 '14 at 20:57
  • I initially had put urllib.request in but I wasn't doing it as from urllib.request import urlretrieve just instead import urllib.request. Like I said at the beginning I knew I had seen somewhere the change but couldn't remember what the change was. With that being changed I still get the 'urllib is not defined error'. – confused Jan 16 '14 at 20:59
  • @BryanOakley It won't even bring up the gui. Not sure why but when I change to run it in 2.7 it downloads fine but it won't display any gui at all. Like I said though I'm kinda prefer the 3.3 format anyways. I'm trying to learn one format of Python and stick with...admittedly I know I have seen a few ways in which 2.7 is almost necessary for some things. I stumbled into that a couple of days ago. – confused Jan 16 '14 at 21:02

1 Answers1

51

In Python 3.x, the urlretrieve function is located in the urllib.request module:

>>> from urllib import urlretrieve
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name urlretrieve
>>>
>>> from urllib.request import urlretrieve
>>> urlretrieve
<function urlretrieve at 0x023C2390>
>>>