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.