How can I use urllib.request.urlretrieve with python 2.7

17,275

Solution 1

You just need to import urllib without '2'

import urllib
urllib.urlretrieve(i, "Negatives/"+str(num)+".jpg")

Solution 2

I found that on different build systems, I'd have different version of Python available, totally out of my control.

So I adjusted my script to get urlretrieve this way:

import sys
print("Python: " + sys.version)
sys.stdout.flush()

import os, re, difflib

# because somewhere along the line they  may have deprecated `urlretrieve`
#   mentioned in docs for python [3.5.5, 3.6.6, 3.7.0, 3.8.0a0] that:
#       `[urlretrieve] might become deprecated at some point in the future.`
def UrlRetrieve(url, fname=None):
  if sys.version_info[0] <= 2:
    import urllib
    return urllib.urlretrieve(url, fname)
  elif sys.version_info[0] <= 3:
    import urllib.request
    return urllib.request.urlretrieve(url, fname)
  else:
    import shutil
    import tempfile
    import urllib.request
    with urllib.request.urlopen(url) as response:
      if fname is None:
        with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
          shutil.copyfileobj(response, tmp_file)
          return (tmp_file.name, response.info())
      else:
        with io.open(fname) as the_file:
          shutil.copyfileobj(response, the_file)
          return (fname, response.info())

Then, use it this way:

url = "http://...whatever.../bootstrap.zip"
pair = UrlRetrieve(url)

And then, because what I was importing was definitely python 2, I needed to do this in the 3 world:

if sys.version_info[0] >= 3:
  import zipfile
  zip_ref = zipfile.ZipFile(pair[0], 'r')
  zip_ref.extractall(pair[0] + ".d")
  zip_ref.close()
  import os
  os.system("2to3 -w " + pair[0] + ".d")
  sys.path.append(pair[0] + ".d")
else:
  sys.path.append(pair[0])
from bootstrap_common import *

I now keep these snippets handle for future scripts where I need to use urlretrieve on both Python 2 and 3.

Share:
17,275
Loanb222
Author by

Loanb222

I am an iOS Game Developer.

Updated on June 04, 2022

Comments

  • Loanb222
    Loanb222 almost 2 years

    I am trying to get images dowloaded from image-net.org so that I can create a haar cascade classifier. I am following this tutorial https://www.youtube.com/watch?v=z_6fPS5tDNU&list=PLQVvvaa0QuDdttJXlLtAJxJetJcqmqlQq&index=18 but I am using python 2.7 instead of python 3. So in the tutorial he has the line:

    urllib.request.urlretrieve(img, pathToImage)
    

    Instead of import urllib.request I did this import urllib2 So I tried this but it isn't vaild

    urllib2.urlretrieve(i, "Negatives/"+str(num)+".jpg")
    

    Thank you in Advance!