@@ -23,14 +23,17 @@ Python interpreter (i.e. the version invoked by the ``python`` command).
23
23
24
24
* ``python2`` will refer to some version of Python 2.x.
25
25
* ``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``
30
29
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,
34
37
Python 2 only scripts should either be updated to be source compatible
35
38
with Python 3 or else to use ``python2`` in the shebang line.
36
39
@@ -51,33 +54,46 @@ Recommendation
51
54
command; see the `Rationale`_ and `Migration Notes`_ below).
52
55
* The Python 2.x ``idle``, ``pydoc``, and ``python-config`` commands should
53
56
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.
57
61
* In order to tolerate differences across platforms, all new code that needs
58
62
to invoke the Python interpreter should not specify ``python``, but rather
59
63
should specify either ``python2`` or ``python3`` (or the more specific
60
64
``python2.x`` and ``python3.x`` versions; see the `Migration Notes`_).
61
65
This distinction should be made in shebangs, when invoking from a shell
62
66
script, when invoking via the system() call, or when invoking in any other
63
67
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.
67
75
* 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``.
69
77
This ensures software is used with the latest version of
70
78
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.)
71
82
* When reinvoking the interpreter from a Python script, querying
72
83
``sys.executable`` to avoid hardcoded assumptions regarding the
73
84
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.
81
97
* When a virtual environment (created by the PEP 405 ``venv`` package or a
82
98
similar tool) is active, the ``python`` command should refer to the
83
99
virtual environment's interpreter. In other words, activating a virtual
@@ -86,7 +102,8 @@ Recommendation
86
102
87
103
These recommendations are the outcome of the relevant python-dev discussions
88
104
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]_).
90
107
91
108
92
109
Rationale
@@ -111,7 +128,7 @@ Future Changes to this Recommendation
111
128
This recommendation will be periodically reviewed over the next few years,
112
129
and updated when the core development team judges it appropriate. As a
113
130
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.
115
132
116
133
117
134
Migration Notes
@@ -144,11 +161,9 @@ making such a change.
144
161
145
162
(In Python 3.4.2+, that generic error message has been replaced with the
146
163
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.
152
167
* The ``pythonX.X`` (e.g. ``python2.6``) commands exist on some systems, on
153
168
which they invoke specific minor versions of the Python interpreter. It
154
169
can be useful for distribution-specific packages to take advantage of these
@@ -164,21 +179,15 @@ making such a change.
164
179
rather than being provided as a separate binary file.
165
180
* It is strongly encouraged that distribution-specific packages use ``python2``
166
181
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
174
187
as a link to the interpreter binary (or a link to a link) and not vice
175
188
versa. That way, if a sysadmin does decide to replace the installed
176
189
``python`` file, they can do so without inadvertently deleting the
177
190
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.
182
191
* If these conventions are adhered to, it will become the case that the
183
192
``python`` command is only executed in an interactive manner as a user
184
193
convenience, or to run scripts that are source compatible with both Python
@@ -281,6 +290,8 @@ References
281
290
minor edits
282
291
(https://github.com/python/peps/pull/630)
283
292
293
+ .. [7] XXX: link to the discussion
294
+
284
295
Copyright
285
296
===========
286
297
This document has been placed in the public domain.
0 commit comments