pythonpython-3.xshellshebang

Is it possible to construct a shebang that works for both Python 2 and 3?


I need to write a Python script that's compatible with both Python 2 and Python 3, can be called directly from a shell (meaning it has a shebang), and can run on systems that have either Python version installed, but possibly not both.

Normally I can write a shebang for Python 2 like so:

#!/usr/bin/env python

and for Python 3:

#!/usr/bin/env python3

But both of these will fail if their corresponding Python version is not installed, since as far as I'm aware, systems that have Python 3 but not Python 2 do not alias or symlink python to python3. (Do correct me if I'm wrong.)

So is there a way to write a Python script with a shebang that will execute the script using either Python 2 or Python 3 so long as one of them is installed?

This is mainly intended to solve the removal of Python 2 from upcoming releases of macOS, (Note: I originally wrote that under the mistaken understanding that Python 2 would be replaced with Python 3 in macOS, but in reality Apple is removing Python completely.) but can apply to Linux as well.


Solution

  • Realistically I would just specify python3 and make that a prerequisite.

    However, it's technically possible to trick a Python file into being its own shell script wrapper:

    #!/bin/sh
    ''':'
    for name in python3 python2 python
    do
        type "$name" > /dev/null 2>&1 && exec "$name" "$0" "$@"
    done
    echo >&2 "Please install python"
    exit 1
    ':'''
    
    print("Hello from Python")