8000 PEP 394: Allow for more flexibility in handling /usr/bin/python by hroncok · Pull Request #989 · python/peps · GitHub
[go: up one dir, main page]

Skip to content

PEP 394: Allow for more flexibility in handling /usr/bin/python #989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PEP 394: Keep version agnostic shebang recommendation
This update reverts back to the version agnostic "python" invocation
as the default recommendation for developers, and rewords the
rest of the PEP accordingly.

In particular, it makes it clear that publishers are free to adopt
the attitude of "we assume you are using a virtual environment",
while at the same time granting the distributors the freedom they
need to make software with the expectation work correctly when
run directly against a system Python installation.
  • Loading branch information
ncoghlan authored and hroncok committed May 8, 2019
commit 268e96d192ded6ef8431b58d2df69dbbea26cd18
91 changes: 54 additions & 37 deletions pep-0394.txt
8000
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,42 @@ For distributors
For developers
--------------

* In order to tolerate differences across platforms, new code that needs
to invoke the Python interpreter should not specify ``python``, but rather
should specify either ``python3`` or ``python2`` (or the more specific
``python3.x`` and ``python2.x`` versions; see the `Migration Notes`_).
This distinction should be made in shebangs, when invoking from a shell
script, when invoking via the system() call, or when invoking in any other
context.
* When reinvoking the interpreter from a Python script, querying
``sys.executable`` to avoid hardcoded assumptions regarding the
interpreter location remains the preferred approach.
* Scripts that are deliberately written to be source compatible with both
Python 3.x and 2.x should use ``python3`` on their shebang line
(see `Rationale`_ for details).
* One exception to this is scripts that are deliberately written to be used
on an “*old system*” where ``python3`` (or even ``python2``) is not available
(such as the default Python installed on macOS or RHEL 6).
Such scripts may continue to use ``python`` on their shebang line.
* While far from being universally available, ``python`` remains the
preferred spelling for explicitly invoking Python, as this is the
spelling that virtual environments make consistently available
across different platforms and Python installations.
* In shebang lines, the preferred spelling is ``/usr/bin/env python``,

Uh oh!

There was an error while loading. Please reload this page.

as this instructs the script to respect the active virtual environment.
* For Python 3 only scripts that do not support being executed on Python
2 at all, it is recommended to instead use ``python3`` and
``/usr/bin/env python3``, as these will never invoke Python 2, and are
expected to work for both Python 3 virtual environments and Python 3
system installations
* For Python 2 only scripts that do not support being executed on Python
3 at all, it is recommended to instead use ``python2`` and
``/usr/bin/env python2``, as these will never invoke Python 3, and are
expected to work for Python 2 virtual environments and at least some
Python 2 system installations.
* In cases where the script is expected to be executed outside virtual
environments, developers will need to be aware of the following
discrepancies across platforms and installation methods:

* Older Linux distributions will provide a ``python`` command that
refers to Python 2. Most of these distros do *not* provide a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second sentence feels a little too imprecise. What exactly is an "older Linux distribution" and how many of them count as "most"? Perhaps:

Older Linux distributions will provide a python command that refers to Python 2, and will likely not provide a python2 command

``python2`` command.
* Some newer Linux distributions will provide a ``python`` command that
refers to Python 3
* Some Linux distributions will not provide a ``python`` command at
all by default, but will provide a ``python3`` command by default

* When potentially targeting these environments, developers may either
use a Python package installation tool that rewrites shebang lines for
the installed environment, provide instructions on updating shebang lines
interactively, or else use more specific shebang lines that are
tailored to the target environment.
* Scripts targeting both “*old systems*” and systems without the default
``python`` command need to make a compromise and document this situation.
Avoiding shebangs (via the console_scripts Entry Points ([9]_) or similar
Expand All @@ -105,9 +124,9 @@ For developers

These recommendations are the outcome of the relevant python-dev discussions
in March and July 2011 ([1]_, [2]_), February 2012 ([4]_),
September 2014 ([6]_), discussion on GitHub in April 2018 ([7]_)
and on python-dev in February 2019 ([8]_).

September 2014 ([6]_), discussion on GitHub in April 2018 ([7]_),
on python-dev in February 2019 ([8]_), and during the PEP update review
in May 2019 ([10]_).


History of this PEP
Expand Down Expand Up @@ -137,35 +156,30 @@ However, these recommendations implicitly assumed that Python 2 would always be
available. As Python 2 is nearing its end of life in 2020 (PEP 373, PEP 404),
distributions are making Python 2 optional or removing it entirely.
This means either removing the ``python`` command or switching it to invoke
Python 3, invalidating respectively the first or second recommendation.
Also, some distributors decided that their users are better served by
ignoring the PEP's recommendations, making the PEP's nominally
cross-platform recommendations on ``python`` and ``python2`` in shebangs
increasingly unreliable.
Python 3. Some distributors also decided that their users were better served by
ignoring the PEP's recommendations, and provided system administrators with the
freedom to configure their systems based on the needs of their particular
environment.


.. _rationale:

Current Rationale
=================

As of 2019, nearly all new systems include Python 3 and the ``python3``
command. This makes the ``python3`` command the best general choice for
code that can run on either Python 3.x or 2.x, even though it is not
available everywhere.
As of 2019, activating a Python virtual environment (or its functional
equivalent) is the best way to obtain a consistent cross-platform and
cross-distribution experience.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we now recommending venvs for deployed scripts? Isn't that problematic? We've seen cases where venvs (IIRC, created with virtualenv not python3 -m venv, but I'm not sure about that), ties the installation to the Python version, so if you update the patched version, it breaks the venv.

If we are recommending venv for deploys, are we positive they are stable, reliable, and relocatable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're not writing Linux distro packages, you should absolutely be using a venv for everything.

You do need to use pipx, and you do need to reinstall them if you change Python versions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't assume the use of a venv, then everything is awful:

  • python probably doesn't work on Windows (or runs something random)
  • py only works on Windows
  • python is only reliably available inside a venv
  • python2 isn't reliably available anywhere
  • python3 doesn't work on Windows

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, you run them using an explicit interpreter invocation, and forget about relying on shebang lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all feels like it's outside the scope of this PEP. With this view, we have to endorse a third party tool (pipx) and a deployment workflow, and teach people how to use virtual environments (and do they use virtualenv or $python -m venv?). The reinstallation issue is problematic too. I don't know what any of this has to do with our recommendations to *nix distributions. If we really want to make these recommendations, we should do that in a separate PEP, IMHO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One other thought: If you're using zip applications (pex, shiv), you should absolutely not be using venvs or /usr/bin/env.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a response to the original draft of this PR, which advised all script publishers to unconditionally use /usr/bin/python3 in their scripts, which I consider to be a bad default recommendation, since it breaks virtual environments.

/usr/bin/env python is better generic advice, since it respects virtual environments, and leaves the choice of specifying something else in their shebang line to folks that specifically want to support running outside a venv, and should have a better idea of which kinds of environments they want to target, and hence which compatibility trade-offs they can make in choosing an exact spelling.

Tools like pipx, zipapp, shiv etc all run an installer at some point, so they allow publishers to rely on console script definitions which write the shebang line at install time, rather than needing to rely on a hardcoded shebang line in the source file(s).

The relevance of all this to PEP 394 is that platform providers need to account for publisher behaviour in designing their Python user experience, and that's easier for them to do if our advice to publishers is "assume platforms will either rely on a Python installer, so you can use generated console scripts, or else that they will make themselves look somewhat like a virtual environment".

Uh oh!

There was an error while loading. Please reload this page.


The recommendation is skewed toward current and future systems, leaving
behind “*old systems*” (like RHEL 6 or default Python installed on macOS).
On these systems, Python software is rarely updated and any recommendations
this PEP makes would likely be ignored.
Accordingly, it is entirely reasonable for publishers to expect the
availability of such an environment to get their software working
correctly, and push responsibility for handling other environments
(such as system Python installations) onto consumers of the software.

Also, since distributors often ignored recommendations the PEP gave
regarding the ``python`` command (for what they saw as legitimate special
needs), this PEP now gives them broad control over the command.
Correspondingly, users are advised to not use the ``python`` command
in cross-platform code.
Instead, this PEP specifies the expected behavior of the ``python3`` and
``python2`` commands, which is not controversial.
As part of that however, it is also appropriate to give Python
distributors the flexibility they need in order to make the
behaviour of their systems as similar as possible to the behaviour
of an activated virtual environment.


Future Changes to this Recommendation
Expand Down Expand Up @@ -350,6 +364,9 @@ References
.. [9] The console_scripts Entry Point
(https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point)

.. [10] May 2019 PEP update review
(https://github.com/python/peps/pull/989)


Copyright
===========
Expand Down
0