8000 PEP 394: Allow not installing unversioned "python" command · encukou/peps@fe6bd46 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe6bd46

Browse files
committed
PEP 394: Allow not installing unversioned "python" command
The intended future for the ``python`` command is: > ``python`` doesn't exist and one always has to specify ``python2`` > or ``python3``. ( – Guido, python#630 (comment) ) There are three conflicting ideas around the ``python`` command, longer-term: 1. The ``python`` command should continue to refer to Python 2 (if Python 2 is available). 2. ``python`` is a correct shebang for py2/py3 source-compatible scripts. 3. Python 2 should *not* be available (by default / after 2010). One of these has to give. It seems that (2) is the easiest to shed, so this proposal does just that. * Make ``python3`` the preferred shebang for py2/py3 compatible scripts, as that's the only shebang that'll work on py2-less systems. (An exception is made for scripts targetting the *system* Python on e.g. macOS/RHEL.) * Make the unversioned ``python`` command optional (along with ``idle``, ``pydoc``, and ``python-config``). Distributions now do not need to install it by default, even if Python 2 is installed. (But they should make it installable explicitly, as long as they ship Python 2.) This should introduce more people to systems without the "legacy" ``python`` command, encourage the use of explicit ``python2``/``python3``, and ease switching to systems that don't have Python 2 at all. * Clarify that distributions *should not* make unversioned ``python`` configurable. (That might work for carefully managed systems, but the ecosystem should converge on ``python3``, not "your ``python`` needs to be set properly".) * Remove mentions of choosing to link ``python`` to ``python3`` in the future, as we don't expect to start recommending that. * Use the term **"unversioned"** ``python`` when contrasting it with ``python3``/``python2``. I found that this makes the message much clearer.
1 parent c66c90d commit fe6bd46

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

pep-0394.txt

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ Python interpreter (i.e. the version invoked by the ``python`` command).
2323

2424
* ``python2`` will refer to some version of Python 2.x.
2525
* ``python3`` will refer to some version of Python 3.x.
26-
* for the time being, all distributions *should* ensure that ``python``,
27-
if installed, refers to the same target as ``python2``, unless the user
28-
deliberately overrides this or a virtual environment is active.
29-
* however, end users should be aware that ``python`` refers to ``python3``
26+
* ``python`` may be missing, but if it is installed, it *should* refer to the
27+
same target as ``python2``, unless the user deliberately overrides this.
28+
* End users should be aware that ``python`` refers to ``python3``
3029
on at least Arch Linux (that change is what prompted the creation of this
31-
PEP), so ``python`` should be used in the shebang line only for scripts
32-
that are source compatible with both Python 2 and 3.
33-
* in preparation for an eventual change in the default version of Python,
30+
PEP), and it may be missing entirely (for example, if Python 2 itself is
31+
not installed).
32+
* For new scripts that are source compatible with both Python 2 and 3,
33+
using ``python3`` in the shebang line is now preferred.
34+
``python`` may still be used for scripts that need to work with the
35+
system-provided Python of older distributions.
36+
* In preparation for an eventual change in the default version of Python,
3437
Python 2 only scripts should either be updated to be source compatible
3538
with Python 3 or else to use ``python2`` in the shebang line.
3639

@@ -51,33 +54,46 @@ Recommendation
5154
command; see the `Rationale`_ and `Migration Notes`_ below).
5255
* The Python 2.x ``idle``, ``pydoc``, and ``python-config`` commands should
5356
likewise be available as ``idle2``, ``pydoc2``, and ``python2-config``,
54-
with the original commands invoking these versions by default, but possibly
55-
invoking the Python 3.x versions instead if configured to do so by the
56-
system administrator.
57+
with the unversioned commands either not available or invoking the
58+
Python 2 versions by default.
59+
If configured to do so by the system administrator, they may invoke the
60+
Python 3.x versions instead.
5761
* In order to tolerate differences across platforms, all new code that needs
5862
to invoke the Python interpreter should not specify ``python``, but rather
5963
should specify either ``python2`` or ``python3`` (or the more specific
6064
``python2.x`` and ``python3.x`` versions; see the `Migration Notes`_).
6165
This distinction should be made in shebangs, when invoking from a shell
6266
script, when invoking via the system() call, or when invoking in any other
6367
context.
64-
* One exception to this is scripts that are deliberately written to be source
65-
compatible with both Python 2.x and 3.x. Such scripts may continue to use
66-
``python`` on their shebang line.
68+
* Scripts that are deliberately written to be source compatible with both
69+
Python 2.x and 3.x *should* now use ``python3`` on their shebang line,
70+
unless they target the system-provided Python of distributions that do not
71+
provide ``python3`` (e.g. macOS or RHEL/CentOS 7).
72+
Alternatively, they may continue to use ``python``, with the caveat that
73+
this will make them unusable on systems that do not have the
74+
unversioned command (or even Python 2 itself) installed.
6775
* When packaging software that is source compatible with both versions,
68-
distributions may change such ``python`` shebangs to ``python3``.
76+
distributions may change unversioned ``python`` shebangs to ``python3``.
6977
This ensures software is used with the latest version of
7078
Python available, and it can remove a dependency on Python 2.
79+
(However, note that there is no automatic way to determine whether a script
80+
is compatible with both versions and uses ``python`` by conscious decision,
81+
or if it's a Python 2-only one that was not yet updated.)
7182
* When reinvoking the interpreter from a Python script, querying
7283
``sys.executable`` to avoid hardcoded assumptions regarding the
7384
interpreter location remains the preferred approach.
74-
* In controlled environments aimed at expert users, where being explicit
75-
is valued over user experience (for example, in test environments and
76-
package build systems), distributions may choose to not provide the
77-
``python`` command even if ``python2`` is available.
78-
(All software in such a controlled environment must use ``python3`` or
79-
``python2`` rather than ``python``, which means scripts that deliberately
80-
use ``python`` need to be modified for such environments.)
85+
* Distributions may choose to not provide the ``python`` command by default.
86+
In this case, they should still make it possible for sysadmins to install
87+
the command explicitly (if Python 2 is available).
88+
All software intended for such a distribution must use ``python3`` (or
89+
``python2``) rather than ``python``.
90+
* Distributions *should not* empower sysadmins to easily switch the ``python``
91+
command to invoke Python 3.
92+
The message ``python: command not found`` (possibly augmented with
93+
a suggestion to explicitly use ``python3`` or ``python2``) is much clearer
94+
and more actionable than errors from accidentally running a script with the
95+
wrong Python version -- especially for those relatively unfamiliar
96+
with Python.
8197
* When a virtual environment (created by the PEP 405 ``venv`` package or a
8298
similar tool) is active, the ``python`` command should refer to the
8399
virtual environment's interpreter. In other words, activating a virtual
@@ -86,7 +102,8 @@ Recommendation
86102

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

91108

92109
Rationale
@@ -111,7 +128,7 @@ Future Changes to this Recommendation
111128
This recommendation will be periodically reviewed over the next few years,
112129
and updated when the core development team judges it appropriate. As a
113130
point of reference, regular maintenance releases for the Python 2.7 series
114-
will continue until at least 2020.
131+
will continue until January 2020.
115132

116133

117134
Migration Notes
@@ -144,11 +161,9 @@ making such a change.
144161

145162
(In Python 3.4.2+, that generic error message has been replaced with the
146163
more explicit "SyntaxError: Missing parentheses in call to 'print'")
147-
* Avoiding breakage of such third party scripts is the key reason this
148-
PEP recommends that ``python`` continue to refer to ``python2`` for the
149-
time being. Until the conventions described in this PEP are more widely
150-
adopted, having ``python`` invoke ``python2`` will remain the recommended
151-
option.
164+
* Avoiding hard-to-debug breakage of such third party scripts is the key reason
165+
this PEP recommends that ``python`` either continue to refer to ``python2``,
166+
or be missing entirely.
152167
* The ``pythonX.X`` (e.g. ``python2.6``) commands exist on some systems, on
153168
which they invoke specific minor versions of the Python interpreter. It
154169
can be useful for distribution-specific packages to take advantage of these
@@ -164,21 +179,15 @@ making such a change.
164179
rather than being provided as a separate binary file.
165180
* It is strongly encouraged that distribution-specific packages use ``python2``
166181
or ``python3`` rather than ``python``, even in code that is not intended to
167-
operate on other distributions. This will reduce problems if the
168-
distribution later decides to change the version of the Python interpreter
169-
that the ``python`` command invokes, or if a sysadmin installs a custom
170-
``python`` command with a different major version than the distribution
171-
default.
172-
* If the above point is adhered to and sysadmins are permitted to change the
173-
``python`` command, then the ``python`` command should always be implemented
182+
operate on other distributions. This will avoid problems if the ``python``
183+
command is unavailable, or if a sysadmin installs a custom ``python``
184+
command with a different major version.
185+
* If sysadmins are permitted to change the ``python`` command (which is *not*
186+
recommended), then the ``python`` command should always be implemented
174187
as a link to the interpreter binary (or a link to a link) and not vice
175188
versa. That way, if a sysadmin does decide to replace the installed
176189
``python`` file, they can do so without inadvertently deleting the
177190
previously installed binary.
178-
* If the Python 2 interpreter becomes uncommon, scripts should nevertheless
179-
continue to use the ``python3`` convention rather that just ``python``. This
180-
will ease transition in the event that yet another major version of Python
181-
is released.
182191
* If these conventions are adhered to, it will become the case that the
183192
``python`` command is only executed in an interactive manner as a user
184193
convenience, or to run scripts that are source compatible with both Python
@@ -281,6 +290,8 @@ References
281290
minor edits
282291
(https://github.com/python/peps/pull/630)
283292

293+
.. [7] XXX: link to the discussion
294+
284295
Copyright
285296
===========
286297
This document has been placed in the public domain.

0 commit comments

Comments
 (0)
0