avoiding long paths to interpreter

Robin Munn rmunn at pobox.com
Fri Jan 10 14:34:49 CET 2003

Erik Max Francis <max at alcyone.com> wrote:
> holger krekel wrote:
>> Probably right, although one could try putting the below into
>> a file:
>> ------------------------------------------------------------
>> #!/bin/sh
>> # find a specific python version you want to use from
>> # e.g. scanning PATH contents
>> # but i am far too lazy to write shell scripts now
>> # so i shortcut :-)
>> python=/usr/bin/python
>> $python <<__HERE__
> 	...
> But this is precisely what env does in the first place, so you might as
> well just left the bangpath alone.
> My point was not that using the (very common, and widely recommended)
> bangpath
> 	#!/usr/bin/env python
> won't work, it's just that which Python interpreter it finds depends on
> the PATH (because that is precisely what env does).  If you have
> multiple intepreters installed, then you may get unpleasant surprises if
> you're relying on finding a particular one, since PATHs will vary widely
> from interactive shell to cron job to CGI script.

If you're dependent on a particular version of Python, then it makes the
most sense to simply check sys.version_info as soon as possible, and
exit gracefully with an appropriate error message, e.g.:

    import sys

    if sys.version_info < (2,2):    # Require at least Python 2.2
        sys.stderr.write("Sorry, I require at least Python 2.2.\n")
        sys.stderr.write("Current version: %s\n" % (sys.version_info,))

    # Version-specific code goes here

Robin Munn <rmunn at pobox.com>
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838

More information about the Python-list mailing list