Hi everyone,

I wrote a script which may end up as part of a package on various
Linux and BSD flavors, and I have hit the problem of getting the
shebang working everywhere. You might know that Python is installed in
different locations, with the binary having different names (for
example python sometimes points to python2.x, sometimes to python3.x
etc.).

Currently my code is compatible with both Python 2 and 3, but to keep
things simple I'd rather use only one of them, probably 3.

What is the correct way to write a shebang on OpenBSD? I thought that
the following would work on most Unixes:

#!/usr/bin/env python3

or even this, with the risk of not knowing if it points to 2 or 3:

#!/usr/bin/env python

Unfortunately when I install the python3 package with pkg_add
(actually 3.5) on OpenBSD, no symlink is created from python3 to 3.5,
so this fails. I do not want to add a hard dependency on specifically
3.5 in the shebang of my script, since it may become obsolete in a
couple of years, and it may be incompatible with other OSes which
might not have 3.5, but maybe 3.4 or 3.6.

Should I instead use an installation-time script that generates the
right shebang?

How about a wrapper shell script which searches for a python
executable and then calls it with a path to the script? For example
the following polyglot script would work:

  #!/bin/sh
  ''''which python3 >/dev/null 2>&1 && exec python3 "$0" "$@" # '''
  ''''which python3.6 >/dev/null 2>&1 && exec python3.6 "$0" "$@" # '''
  ''''which python3.5 >/dev/null 2>&1 && exec python3.5 "$0" "$@" # '''
  ''''which python3.4 >/dev/null 2>&1 && exec python3.4 "$0" "$@" # '''
  ''''exec echo "fatal: cannot find python3 binary" # '''

All these methods seem quite convoluted for doing something so simple.
Please let me know what you think.

Thanks in advance for your advice.
Ovidiu

Reply via email to