<p>At the beginning of all my executable Python scripts I put the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)" rel="noreferrer">shebang</a> line:</p>

<pre><code>#!/usr/bin/env python
</code></pre>

<p>I'm running these scripts on a system where <code>env python</code> yields a Python 2.2 environment. My scripts quickly fail because I have a manual check for a compatible Python version:</p>

<pre><code>if sys.version_info &lt; (2, 4):
    raise ImportError("Cannot run with Python version &lt; 2.4")
</code></pre>

<p>I don't want to have to change the shebang line on every executable file, if it's possible; however, I don't have administrative access to the machine to change the result of <code>env python</code> and I don't want to force a particular version, as in:</p>

<pre><code>#!/usr/bin/env python2.4
</code></pre>

<p>I'd like to avoid this because system may have a newer version than Python 2.4, or may have Python 2.5 but no Python 2.4.</p>

<p>What's the elegant solution?</p>

<p>[Edit:] I wasn't specific enough in posing the question -- I'd like to let users execute the scripts without manual configuration (e.g. path alteration or symlinking in <code>~/bin</code> and ensuring your PATH has <code>~/bin</code> before the Python 2.2 path). Maybe some distribution utility is required to prevent the manual tweaks?</p>

[{'answer_id': 40715, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': True, 'text': '<p>"env" simply executes the first thing it finds in the PATH env var. To switch to different python, prepend the directory for that python\'s executable to the path before invoking your script.</p>\n'}, {'answer_id': 40721, 'author': 'Douglas Leeder', 'author_id': 3978, 'author_profile': 'https://Stackoverflow.com/users/3978', 'pm_score': 2, 'selected': False, 'text': "<p>If you are running the scripts then you can set your PATH variable to point to a private bin directory first:</p>\n\n<pre><code>$ mkdir ~/bin\n$ ln -s `which python2.4` ~/bin/python\n$ export PATH=~/bin:$PATH\n</code></pre>\n\n<p>Then when you execute your python script it'll use python 2.4. You'll have to change your login scripts to change your PATH.</p>\n\n<p>Alternatively run your python script with the explicit interpreter you want:</p>\n\n<pre><code>$ /path/to/python2.4 &lt;your script&gt;\n</code></pre>\n"}, {'answer_id': 42516, 'author': 'morais', 'author_id': 2846, 'author_profile': 'https://Stackoverflow.com/users/2846', 'pm_score': 2, 'selected': False, 'text': '<p>Pretty hackish solution - if your check fails, use this function (which probably could be significantly improved) to determine the best interpreter available, determine if it is acceptable, and if so relaunch your script with os.system or something similar and your sys.argv using the new interpreter.</p>\n\n<pre><code>import os\nimport glob\ndef best_python():\n    plist = []\n    for i in os.getenv("PATH").split(":"):\n        for j in glob.glob(os.path.join(i, "python2.[0-9]")):\n             plist.append(os.path.join(i, j))\n    plist.sort()\n    plist.reverse()\n    if len(plist) == 0: return None\n    return plist[0]\n</code></pre>\n'}, {'answer_id': 42794, 'author': 'cdleary', 'author_id': 3594, 'author_profile': 'https://Stackoverflow.com/users/3594', 'pm_score': 0, 'selected': False, 'text': '<p>@morais: That\'s an interesting idea, but I think maybe we can take it one step farther. Maybe there\'s a way to use <a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow noreferrer">Ian Bicking\'s virtualenv</a> to:</p>\n\n<ul>\n<li>See if we\'re running in an acceptable environment to begin with, and if so, do nothing.</li>\n<li>Check if there exists a version-specific executable on the <code>PATH</code>, i.e. check if <code>python2.x</code> exists <code>for x in reverse(range(4, 10))</code>. If so, re-run the command with the better interpreter.</li>\n<li>If no better interpreter exists, use virtualenv to try and install a newer version of Python from the older version of Python and get any prerequisite packages.</li>\n</ul>\n\n<p>I have no idea if virtualenv is capable of this, so I\'ll go mess around with it sometime soon. :)</p>\n'}, {'answer_id': 19188285, 'author': 'Alex Nelson', 'author_id': 1207160, 'author_profile': 'https://Stackoverflow.com/users/1207160', 'pm_score': 0, 'selected': False, 'text': '<p>Here\'s a solution if you\'re (1) absolutely set on using shebangs and (2) able to use Autotools in your build process.</p>\n\n<p>I just found last night that you can use the autoconf macro <code>AM_PATH_PYTHON</code> to find a minimal Python <strong>2</strong> binary.  The how-to is <a href="https://stackoverflow.com/a/19173018/1207160">here</a>.</p>\n\n<p>So, your process would be:</p>\n\n<ul>\n<li>Issue an <code>AM_PATH_PYTHON(2.4)</code> in your <code>configure.ac</code></li>\n<li>Rename all of your <code>.py</code> scripts to <code>.py.in</code> (in my experience, this doesn\'t confuse <code>vi</code>)</li>\n<li>Name all of those Python scripts you want to generate with <code>AC_CONFIG_FILES</code>.</li>\n<li>Instead of starting with <code>#!/usr/bin/env python</code>, use <code>#!@PYTHON@</code></li>\n</ul>\n\n<p>Then your <em>resultant</em> Python scripts will always have an appropriate shebang.</p>\n\n<p>So, you have this solution, at least possible, if not practical.</p>\n'}]
['https://Stackoverflow.com/questions/40705', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3594/']