How to check a Python script's the correct version automatically while the shebang is not accurate?
Question: For example, I find
[download_model_binary.py](https://github.com/BVLC/caffe/blob/master/scripts/download_model_binary.py)'s
shebang is wrong while it contains a Python 2 library function
`urllib.urlretrieve`.

I try to use two python interpreters to execute the file and watch its return
value in script, but it will lead side effect.

Note: I am asking how detect the correct version of a existing Python 2 script
like `download_model_binary.py` which has the wrong shebang, not how to
rewrite it to be compatible.


Answer: You could do something like this:

    
    
    import sys
    
    if sys.version_info.major < 3:
         from urllib import urlretrieve
    else:
         from urllib.request import urlretrieve
    

And later use:

    
    
    urlretrieve(frontmatter['caffemodel_url'], model_filename, reporthook)
    

If you need this often consider using [Python Future](http://python-
future.org/index.html). It offers a [good solution](http://python-
future.org/quickstart.html#standard-library-reorganization) for this type of
problem.

