#!/usr/bin/env python
here? #!/usr/bin/env python
here?Of course, if your script will run under both Python 2 and 3, I don't know why you would want to use these contrivances to simply give preference to Python 3.
The article is literally about this scenario, which is why I'm also confused. Yes, `/usr/bin/env python` is a bad idea IF your script depends on a certain version, but that's exactly what the author doesn't need!
Since Debian 11 and Ubuntu 20.04 LTS it's a matter of installing the matching package:
python-is-python2 [1] for having /usr/bin/python link to Python 2.7, python-is-python3 [2] for having /usr/bin/python link to Python 3.x or none of them to avoid having /usr/bin/python at all. The latter one will still result in a working system, as all Python scripts shipped by Debian/Ubuntu nowadays explicitly specify /usr/bin/python2 or /usr/bin/python3.
Performance gains, preferring future-looking development, and better error messages.
Or theoretically also the other way around.
#!/usr/bin/env python
If you do, then `python2` or `python3` will be around, and you should specify them explicitly instead of `python`. I'm happy to be corrected on this one, but I always slap this at the top of my scripts: #!/usr/bin/env python3
I'm not aware of anywhere that wouldn't invoke a Python 3 interpreter if one is installed.The original post just seems like one big faff.
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3
https://packages.ubuntu.com/focal/python-is-python3https://www.python.org/dev/peps/pep-0394/
"The python command should always invoke Python 2 (to prevent hard-to-diagnose errors when Python 2 code is run on Python 3)."
So there was indeed a time where the mentioned scenario was possible.
Edit: love those people who got to downvote reality. At no point was python pointing to either 2 or 3 will-nilly, but there was always a PEP specifying best practice behaviour.
The current version of PEP 394 says:
] If the python command is installed, it is expected to invoke either the same version of Python as the python3 command or as the python2 command.
] Distributors may choose to set the behavior of the python command as follows:
python2,
python3,
not provide python command,
allow python to be configurable by an end user or a system administrator.
The part you quoted is from the section "History of this PEP". Just before your quoted text is:] This PEP originally provided a very simple mechanism ...
and just after it is:
] However, these recommendations implicitly assumed that Python 2 would always be available.
This historic advice is no longer recommended.
The change was made on July 5, 2019 - https://github.com/python/peps/commit/ae932bd6fd2c493d7d64ce... .
(Ubuntu LTSs and macOs are an example ...)
When designing things for 'just work' scripts you have to take esoteric environments into account.
There is no guarantee that '#!/usr/bin/env python' will not error in an environment with only python3 installed.
So I do wonder how many distros (and manual install workflows) for python could cause this to happen if python2 got uninstalled from a system that had both, where python get removed, rather then updated to point to python3.
This is despite guidance from PEP 0394[1]