Could the following bullet in PEP 394 please be clarified?:
For scripts that are only expected to be run in an activated virtual environment, shebang lines can be written as #!/usr/bin/env python, as this instructs the script to respect the active virtual environment.
I believe it would help “newbies” like me better understand what’s going on here if there was a little more, like this:
The shebang
#!/usr/bin/env pythonshould be used in scripts where it is desired to have a single shebang with the greatest degree of cross-platform compatibility, both for multiple versions of Python and multiple operating systems.
envis a system binary in/usr/binthat searches$PATHfor strings containing the provided argument and returns the first instance it finds. In the above syntax,envwill search for the first instance ofpythonin$PATHand return it.This shebang option works well both when a script is to be run from within an activated virtual environment or if it is uncertain whether the
pythoninterpreter exists in/bin,/usr/bin,/usr/local/bin, or another custom path.Since
pythonmay be an alias forpython2orpython3(typically, it is an alias forpython2), the developer may choose to be specific and use the shebang#!/usr/bin/env python3where desired.
Here is my rationale:
-
envin/usr/binis not just for virtual environments (i.e. python virutal environments, which the bullet seems to suggest at first read).envis available by default in most Linux distros, but technically not part of thePOSIXstandard. For Windows users, it is easy to conflateenvwith Python virtual environments as opposed to theenvtool in Linux. I discovered I was making this mistake after seeing thatpythonis separated from/usr/bin/envwith a space, indicating it was an argument passed toenvper the#! interpreter [optional-arg]shebang syntax. -
The following Migration bullet in the PEP recommends being explicit wherever possible using
python3as opposed topythonwhere intended:
It is strongly encouraged that distribution-specific packages use python3 (or python2) rather than python, even in code that is not intended to operate on other distributions. This will reduce problems if the distribution later decides to change the version of the Python interpreter that the python command invokes, or if a sysadmin installs a custom python command with a different major version than the distribution default.
- The fact that the PEP states the shebang usage should be used for
activated virtual environmentshas appeared to be a source of confusion for most readers of the PEP. I believe this is rooted in confusion over theenvtool and python virtual environments. Specifically, as there is no guarantee the end user will have an activated virtual environment even if they are encouraged to do so, specifying#!/usr/bin/env pythonto new readers seems odd. Without clarification, especially for Windows users and the uninitiated,/usr/bin/envseems like a folder that gets created when virtual environments are activated. For example, please see the following Stack Overflow posts regarding this topic:
- Should I put #! (shebang) in Python scripts, and what form should it take?
- What’s the difference between python shebangs with /usr/bin/env rather than hard-path?
I believe such a clarification would also benefit PEP 397 as the Python Launcher admits “virtual” shebangs, so it will provide further impetus for Windows users to add #!/usr/bin/env python3 to their scripts to ensure the greatest degree of cross-platform compatability between Linux, MacOS, and Windows.