@@ -5,104 +5,195 @@ Last-Modified: $Date$
5
5
Author: Kerrick Staley <
[email protected] >,
6
6
7
7
8
-
8
+ Petr Viktorin <
[email protected] >,
9
+
10
+ Carol Willing <
[email protected] >,
9
11
Status: Active
10
12
Type: Informational
11
13
Content-Type: text/x-rst
12
14
Created: 02-Mar-2011
13
- Post-History: 04-Mar-2011, 20-Jul-2011, 16-Feb-2012, 30-Sep-2014, 28-Apr-2018
15
+ Post-History: 04-Mar-2011, 20-Jul-2011, 16-Feb-2012, 30-Sep-2014, 28-Apr-2018,
16
+ 26-Jun-2019
14
17
Resolution: https://mail.python.org/pipermail/python-dev/2012-February/116594.html
15
18
16
19
17
20
Abstract
18
21
========
19
22
20
- This PEP provides a convention to ensure that Python scripts can continue to
21
- be portable across ``*nix`` systems, regardless of the default version of the
22
- Python interpreter (i.e. the version invoked by the ``python`` command).
23
+ This PEP outlines the behavior of Python scripts when the ``python`` command
24
+ is invoked.
25
+ Depending on a distribution or system configuration,
26
+ ``python`` may or may not be installed.
27
+ If ``python`` is installed its target interpreter may refer to ``python2``
28
+ or ``python3``.
29
+ End users may be unaware of this inconsistency across Unix-like systems.
30
+ This PEP's goal is to reduce user confusion about what ``python`` references
31
+ and what will be the script's behavior.
23
32
24
- * ``python2`` will refer to some version of Python 2.x.
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``
30
- 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,
34
- Python 2 only scripts should either be updated to be source compatible
35
- with Python 3 or else to use ``python2`` in the shebang line.
33
+ The recommendations in the next section of this PEP will outline the behavior
34
+ when:
35
+
36
+ * using virtual environments
37
+ * writing cross-platform scripts with shebangs for either ``python2`` or ``python3``
38
+
39
+ The PEP's goal is to clarify the behavior for script end users, distribution
40
+ providers, and script maintainers / authors.
36
41
37
42
38
43
Recommendation
39
44
==============
40
45
41
- * Unix-like software distributions (including systems like Mac OS X and
42
- Cygwin) should install the ``python2`` command into the default path
46
+
47
+ Our recommendations are detailed below.
48
+ We call out any expectations that these recommendations are based upon.
49
+
50
+ For Python runtime distributors
51
+ -------------------------------
52
+
53
+ * We expect Unix-like software distributions (including systems like macOS and
54
+ Cygwin) to install the ``python2`` command into the default path
43
55
whenever a version of the Python 2 interpreter is installed, and the same
44
56
for ``python3`` and the Python 3 interpreter.
45
57
* When invoked, ``python2`` should run some version of the Python 2
46
58
interpreter, and ``python3`` should run some version of the Python 3
47
59
interpreter.
48
- * If the ``python`` command is installed, it should invoke the same version of
49
- Python as the ``python2`` command (however, note that some distributions
50
- have already chosen to have ``python`` implement the ``python3``
51
- command; see the `Rationale`_ and `Migration Notes`_ below).
52
- * The Python 2.x ``idle``, ``pydoc``, and ``python-config`` commands should
53
- 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
- * In order to tolerate differences across platforms, all new code that needs
58
- to invoke the Python interpreter should not specify ``python``, but rather
59
- should specify either ``python2`` or ``python3`` (or the more specific
60
- ``python2.x`` and ``python3.x`` versions; see the `Migration Notes`_).
61
- This distinction should be made in shebangs, when invoking from a shell
62
- script, when invoking via the system() call, or when invoking in any other
63
- 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.
67
- * When packaging software that is source compatible with both versions,
68
- distributions may change such ``python`` shebangs to ``python3``.
69
- This ensures software is used with the latest version of
70
- Python available, and it can remove a dependency on Python 2.
60
+ * If the ``python`` command is installed, it is expected to invoke either
61
+ the same version of Python as the ``python3`` command or as the ``python2``
62
+ command.
63
+ * Distributors may choose to set the behavior of the ``python`` command
64
+ as follows:
65
+
66
+ * ``python2``,
67
+ * ``python3``,
68
+ * not provide ``python`` command,
69
+ * allow ``python`` to be configurable by an end user or
70
+ a system administrator.
71
+
72
+ * The Python 3.x ``idle``, ``pydoc``, and ``python-config`` commands should
73
+ likewise be available as ``idle3``, ``pydoc3``, and ``python3-config``;
74
+ Python 2.x versions as ``idle2``, ``pydoc2``, and ``python2-config``.
75
+ The commands with no version number should either invoke the same version
76
+ of Python as the ``python`` command, or not be available at all.
77
+ * When packaging third party Python scripts, distributors are encouraged to
78
+ change less specific shebangs to more specific ones.
79
+ This ensures software is used with the latest version of Python available,
80
+ and it can remove a dependency on Python 2.
81
+ The details on what specifics to set are left to the distributors;
82
+ though. Example specifics could include:
83
+
84
+ * Changing ``python`` shebangs to ``python3`` when Python 3.x is supported.
85
+ * Changing ``python`` shebangs to ``python2`` when Python 3.x is not yet
86
+ supported.
87
+ * Changing ``python3`` shebangs to ``python3.8`` if the software is built
88
+ with Python 3.8.
89
+
90
+ * When a virtual environment (created by the PEP 405 ``venv`` package or a
91
+ similar tool such as ``virtualenv`` or ``conda``) is active, the ``python``
92
+ command should refer to the virtual environment's interpreter and should
93
+ always be available.
94
+ The ``python3`` or ``python2`` command (according to the environment's
95
+ interpreter version) should also be available.
96
+
97
+ For Python script publishers
98
+ ----------------------------
99
+
71
100
* When reinvoking the interpreter from a Python script, querying
72
101
``sys.executable`` to avoid hardcoded assumptions regarding the
73
102
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.)
81
- * When a virtual environment (created by the PEP 405 ``venv`` package or a
82
- similar tool) is active, the ``python`` command should refer to the
83
- virtual environment's interpreter. In other words, activating a virtual
84
- environment counts as deliberate user action to change the default
85
- ``python`` interpreter.
103
+ * Encourage your end users to use a virtual environment.
104
+ This makes the user's environment more predictable (possibly resulting
105
+ in fewer issues), and helps avoid disrupting their system.
106
+ * For scripts that are only expected to be run in an activated virtual
107
+ environment, shebang lines can be written as ``#!/usr/bin/env python``,
108
+ as this instructs the script to respect the active virtual environment.
109
+ * In cases where the script is expected to be executed outside virtual
110
+ environments, developers will need to be aware of the following
111
+ discrepancies across platforms and installation methods:
112
+
113
+ * Older Linux distributions will provide a ``python`` command that
114
+ refers to Python 2, and will likely not provide a ``python2`` command.
115
+ * Some newer Linux distributions will provide a ``python`` command that
116
+ refers to Python 3.
117
+ * Some Linux distributions will not provide a ``python`` command at
118
+ all by default, but will provide a ``python3`` command by default.
119
+
120
+ * When potentially targeting these environments, developers may either
121
+ use a Python package installation tool that rewrites shebang lines for
122
+ the installed environment, provide instructions on updating shebang lines
123
+ interactively, or else use more specific shebang lines that are
124
+ tailored to the target environment.
125
+ * Scripts targeting both “*old systems*” and systems without the default
126
+ ``python`` command need to make a compromise and document this situation.
127
+ Avoiding shebangs (via the console_scripts Entry Points ([9]_) or similar
128
+ means) is the recommended workaround for this problem.
129
+ * Applications designed exclusively for a specific environment (such as
130
+ a container or virtual environment) may continue to use the ``python``
131
+ command name.
132
+
133
+ For end users of Python
134
+ -----------------------
135
+
136
+ * While far from being universally available, ``python`` remains the
137
+ preferred spelling for explicitly invoking Python, as this is the
138
+ spelling that virtual environments make consistently available
139
+ across different platforms and Python installations.
140
+ * For software that is not distributed with (or developed for) your system,
141
+ we recommend using a virtual environment, possibly with an environment
142
+ manager like ``conda`` or ``pipenv``, to help avoid disrupting your system
143
+ Python installation.
144
+
86
145
87
146
These recommendations are the outcome of the relevant python-dev discussions
88
147
in March and July 2011 ([1]_, [2]_), February 2012 ([4]_),
89
- September 2014 ([6]_), and discussion on GitHub in April 2018 ([7]_).
148
+ September 2014 ([6]_), discussion on GitHub in April 2018 ([7]_),
149
+ on python-dev in February 2019 ([8]_), and during the PEP update review
150
+ in May/June 2019 ([10]_).
90
151
91
152
92
- Rationale
93
- =========
153
+ History of this PEP
154
+ ===================
94
155
95
- This recommendation is needed as, even though the majority of distributions
96
- still alias the ``python`` command to Python 2, some now alias it to
156
+ In 2011, the majority of distributions
157
+ aliased the ``python`` command to Python 2, but some started switching it to
97
158
Python 3 ([5]_). As some of the former distributions did not provide a
98
159
``python2`` command by default, there was previously no way for Python 2 code
99
160
(or any code that invokes the Python 2 interpreter directly rather than via
100
161
``sys.executable``) to reliably run on all Unix-like systems without
101
162
modification, as the ``python`` command would invoke the wrong interpreter
102
163
version on some systems, and the ``python2`` command would fail completely
103
- on others. The recommendations in this PEP provide a very simple mechanism
164
+ on others. This PEP originally provided a very simple mechanism
104
165
to restore cross-platform support, with minimal additional work required
105
- on the part of distribution maintainers.
166
+ on the part of distribution maintainers. Simplified, the recommendation was:
167
+
168
+ 1. The ``python`` command was preferred for code compatible with both
169
+ Python 2 and 3 (since it was available on all systems, even those that
170
+ already aliased it to Python 3).
171
+ 2. The ``python`` command should always invoke Python 2 (to prevent
172
+ hard-to-diagnose errors when Python 2 code is run on Python 3).
173
+ 3. The ``python2`` and ``python3`` commands should be available to specify
174
+ the version explicitly.
175
+
176
+ However, these recommendations implicitly assumed that Python 2 would always be
177
+ available. As Python 2 is nearing its end of life in 2020 (PEP 373, PEP 404),
178
+ distributions are making Python 2 optional or removing it entirely.
179
+ This means either removing the ``python`` command or switching it to invoke
180
+ Python 3. Some distributors also decided that their users were better served by
181
+ ignoring the PEP's original recommendations, and provided system
182
+ administrators with the freedom to configure their systems based on
183
+ the needs of their particular environment.
184
+
185
+
186
+ .. _rationale:
187
+
188
+ Current Rationale
189
+ =================
190
+
191
+ As of 2019, activating a Python virtual environment (or its functional
192
+ equivalent) prior to script execution is one way to obtain a consistent
193
+ cross-platform and cross-distribution experience.
194
+
195
+ Accordingly, publishers can expect users of the software to provide a suitable
196
+ execution environment.
106
197
107
198
108
199
Future Changes to this Recommendation
@@ -111,7 +202,7 @@ Future Changes to this Recommendation
111
202
This recommendation will be periodically reviewed over the next few years,
112
203
and updated when the core development team judges it appropriate. As a
113
204
point of reference, regular maintenance releases for the Python 2.7 series
114
- will continue until at least 2020.
205
+ will continue until January 2020.
115
206
116
207
117
208
Migration Notes
@@ -129,27 +220,25 @@ making such a change.
129
220
and other users. Updating the ``python`` command to invoke ``python3``
130
221
by default indicates that a distribution is willing to break such scripts
131
222
with errors that are potentially quite confusing for users that aren't
132
- yet familiar with the backwards incompatible changes in Python 3. For
223
+ familiar with the backwards incompatible changes in Python 3. For
133
224
example, while the change of ``print`` from a statement to a builtin
134
225
function is relatively simple for automated converters to handle, the
135
- SyntaxError from attempting to use the Python 2 notation in versions of
136
- Python 3 prior to 3.4.2 is thoroughly confusing if you aren't already
137
- aware of the change::
226
+ SyntaxError from attempting to use the Python 2 notation in Python 3
227
+ may be confusing for users that are not aware of the change::
138
228
139
229
$ python3 -c 'print "Hello, world!"'
140
230
File "<string>", line 1
141
231
print "Hello, world!"
142
- ^
143
- SyntaxError: invalid syntax
144
-
145
- (In Python 3.4.2+, that generic error message has been replaced with the
146
- 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.
152
- * The ``pythonX.X`` (e.g. ``python2.6``) commands exist on some systems, on
232
+ ^
233
+ SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello, world!")?
234
+
235
+ While this might be obvious for experienced Pythonistas, such scripts
236
+ might even be run by people who are not familiar with Python at all.
237
+ Avoiding breakage of such third party scripts was the key reason this
238
+ PEP used to recommend that ``python`` continue to refer to ``python2``.
239
+ * The error message ``python: command not found`` tends to be surprisingly
240
+ actionable, even for people unfamiliar with Python.
241
+ * The ``pythonX.X`` (e.g. ``python3.6``) commands exist on modern systems, on
153
242
which they invoke specific minor versions of the Python interpreter. It
154
243
can be useful for distribution-specific packages to take advantage of these
155
244
utilities if they exist, since it will prevent code breakage if the default
@@ -162,8 +251,8 @@ making such a change.
162
251
* When the ``pythonX.X`` binaries are provided by a distribution, the
163
252
``python2`` and ``python3`` commands should refer to one of those files
164
253
rather than being provided as a separate binary file.
165
- * It is strongly encouraged that distribution-specific packages use ``python2 ``
166
- or ``python3`` rather than ``python``, even in code that is not intended to
254
+ * It is strongly encouraged that distribution-specific packages use ``python3 ``
255
+ ( or ``python2``) rather than ``python``, even in code that is not intended to
167
256
operate on other distributions. This will reduce problems if the
168
257
distribution later decides to change the version of the Python interpreter
169
258
that the ``python`` command invokes, or if a sysadmin installs a custom
@@ -175,14 +264,12 @@ making such a change.
175
264
versa. That way, if a sysadmin does decide to replace the installed
176
265
``python`` file, they can do so without inadvertently deleting the
177
266
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.
267
+ * Even as the Python 2 interpreter becomes less common, it remains reasonable
268
+ for scripts to continue to use the ``python3`` convention, rather than just
269
+ ``python``.
182
270
* If these conventions are adhered to, it will become the case that the
183
271
``python`` command is only executed in an interactive manner as a user
184
- convenience, or to run scripts that are source compatible with both Python
185
- 2 and Python 3.
272
+ convenience, or else when using a virtual environment or similar mechanism.
186
273
187
274
188
275
Backwards Compatibility
@@ -194,7 +281,7 @@ these commands. This is mostly a non-issue, since the sysadmin can simply
194
281
create these symbolic links and avoid further problems. It is a significantly
195
282
more obvious breakage than the sometimes cryptic errors that can arise when
196
283
attempting to execute a script containing Python 2 specific syntax with a
197
- Python 3 interpreter.
284
+ Python 3 interpreter or vice versa .
198
285
199
286
200
287
Application to the CPython Reference Interpreter
@@ -209,7 +296,7 @@ items are relative symbolic links)::
209
296
python -> python2 -> python2.7
210
297
python-config -> python2-config -> python2.7-config
211
298
212
- Similar adjustments were made to the Mac OS X binary installer.
299
+ Similar adjustments were made to the macOS binary installer.
213
300
214
301
This feature first appeared in the default installation process in
215
302
CPython 2.7.3.
@@ -252,8 +339,7 @@ Exclusion of MS Windows
252
339
This PEP deliberately excludes any proposals relating to Microsoft Windows, as
253
340
devising an equivalent solution for Windows was deemed too complex to handle
254
341
here. PEP 397 and the related discussion on the python-dev mailing list
255
- address this issue (like this PEP, the PEP 397 launcher invokes Python 2 by
256
- default if versions of both Python 2 and 3 are installed on the system).
342
+ address this issue.
257
343
258
344
259
345
References
@@ -281,6 +367,16 @@ References
281
367
minor edits
282
368
(https://github.com/python/peps/pull/630)
283
369
370
+ .. [8] Another update for PEP 394 -- The "python" Command on Unix-Like Systems
371
+ (https://mail.python.org/pipermail/python-dev/2019-February/156272.html)
372
+
373
+ .. [9] The console_scripts Entry Point
374
+ (https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point)
375
+
376
+ .. [10] May 2019 PEP update review
377
+ (https://github.com/python/peps/pull/989)
378
+
379
+
284
380
Copyright
285
381
===========
286
382
This document has been placed in the public domain.
0 commit comments