On 2016-10-22, Ovidiu M <[email protected]> wrote:
> 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

Opinions vary but I'm not a fan of using env for this, I don't normally
want to trust the first file of that name found in the path.

> 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?

Can you just use setuptools? That generally does the right thing.

For ports/packages we generally overwrite the shebang line with the specific
one for the wanted version in those cases where setuptools (or similar)
doesn't take care of this.

> 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" # '''

This wouldn't be useful for a package, we want deterministic behaviour.

Reply via email to