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:
#!/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
$PATHfor strings containing the provided argument and returns the first instance it finds. In the above syntax,
envwill search for the first instance of
$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
/usr/local/bin, or another custom path.
pythonmay be an alias for
python3(typically, it is an alias for
python2), the developer may choose to be specific and use the shebang
#!/usr/bin/env python3where desired.
Here is my rationale:
/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 the
POSIXstandard. For Windows users, it is easy to conflate
envwith Python virtual environments as opposed to the
envtool in Linux. I discovered I was making this mistake after seeing that
pythonis separated from
/usr/bin/envwith a space, indicating it was an argument passed to
#! interpreter [optional-arg]shebang syntax.
The following Migration bullet in the PEP recommends being explicit wherever possible using
python3as opposed to
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 the
envtool 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.