-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from 1 commit
68475c0
abf3bf3
4a4cdf7
36e61fa
268e96d
5e439bd
2e763f3
627ae44
4af8f14
8d88ea0
9546b15
fee50a4
4b20f8c
cca28c0
5d3e9d8
6dbfc2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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``, | ||
ncoghlan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
``python2`` command. | ||
* Some newer Linux distributions will provide a ``python`` command that | ||
refers to Python 3 | ||
hroncok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Some Linux distributions will not provide a ``python`` command at | ||
all by default, but will provide a ``python3`` command by default | ||
hroncok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* When potentially targeting these environments, developers may either | ||
willingc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
@@ -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]_). | ||
hroncok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
History of this PEP | ||
|
@@ -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 | ||
encukou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
hroncok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cross-distribution experience. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 If we are recommending venv for deploys, are we positive they are stable, reliable, and relocatable? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Tools like 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". |
||
|
||
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 | ||
hroncok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
hroncok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
@@ -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 | ||
=========== | ||
|
Uh oh!
There was an error while loading. Please reload this page.