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 python
should 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.
env
is a system binary in/usr/bin
that searches$PATH
for strings containing the provided argument and returns the first instance it finds. In the above syntax,env
will search for the first instance ofpython
in$PATH
and 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
python
interpreter exists in/bin
,/usr/bin
,/usr/local/bin
, or another custom path.Since
python
may be an alias forpython2
orpython3
(typically, it is an alias forpython2
), the developer may choose to be specific and use the shebang#!/usr/bin/env python3
where desired.
Here is my rationale:
-
env
in/usr/bin
is not just for virtual environments (i.e. python virutal environments, which the bullet seems to suggest at first read).env
is available by default in most Linux distros, but technically not part of thePOSIX
standard. For Windows users, it is easy to conflateenv
with Python virtual environments as opposed to theenv
tool in Linux. I discovered I was making this mistake after seeing thatpython
is separated from/usr/bin/env
with a space, indicating it was an argument passed toenv
per the#! interpreter [optional-arg]
shebang syntax. -
The following Migration bullet in the PEP recommends being explicit wherever possible using
python3
as opposed topython
where 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 environments
has appeared to be a source of confusion for most readers of the PEP. I believe this is rooted in confusion over theenv
tool 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 python
to new readers seems odd. Without clarification, especially for Windows users and the uninitiated,/usr/bin/env
seems 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.