I often see this at the top of .py
files:
#!/usr/bin/env python
With the unknown state of the OS defaults, I wonder why it's not:
#!/usr/bin/env python2
#!/usr/bin/env python3
If most OSs provide this versioned symlink, wouldn't that be better?
I found PEP 394 while looking around, which states that for the time being, developers should assume python -> python2 -> python2.x
. It also states that one can assume that the versioned equivalents, python3
and python2
, exist. So what's the harm in not leaving it to chance and providing that extra character?
If one doesn't have python2
installed because the OS ships by default with python -> python3
(like Arch linux), I'd much prefer this issue when running a script or program:
/usr/bin/env: ‘python2’: No such file or directory
Alternatively, this error is much worse, particularly for a new user (python
exists, it's just the wrong version):
File "<stdin>", line 1
print 'Hello world'
^
SyntaxError: Missing parentheses in call to 'print'
That said, I find the latter far more common. At least now I know some typical compatibility errors well enough to think to myself "Oh, right. It's the python
symlink."
Two questions ask how to specify/verify the desired version:
This 2010 question suggests treating
python
as meaningpython2
andpython3
as required to explicitly call outpython3
.This 2013 question: maybe implies that
python3
shouldn't be used because not all distros ship with it?
Is there an obvious reason I (or python
programmers) shouldn't use a versioned env
call if most OSs provide it? To use just python
is seeming to cater to a minority who don't have the versioned command while causing confusion to the vast majority who do.
Maybe the answer is to use versioned commands (I'm getting that from PEP 394) but enough time hasn't elapsed to see them appear. To date I've never seen a versioned env
call... then again, if it works I never look. If it breaks, it's always a version-less python
line so my mental counts are probably skewed!
Some github stats on searches; I was curious on usage:
#!/usr/bin/env python2
: ~210k python file code hits#!/usr/bin/env python3
: ~460k#!/usr/bin/env python
: ~6 million
This could mean that most code is old enough that if the recommendations of the questions above was the prevailing wisdom, folks just haven't updated their files?
I looked at these popular OSs and found them all to use versioned commands:
- ubuntu: python-minimal and python3-minimal
- arch: python and python2
- fedora: python2 and python3
- debian: same names/state as ubuntu
- various sources suggest that OS X uses versioned commands
- windows: I think there's evidence that windows has versioned commands available