@@ -203,13 +203,19 @@ The end result of all this is:
203203Executing scripts
204204-----------------
205205
206- Python scripts (files with the extension ``.py ``) will be executed by
207- :program: `python.exe ` by default. This executable opens a terminal, which stays
208- open even if the program uses a GUI. If you do not want this to happen, use the
209- extension ``.pyw `` which will cause the script to be executed by
210- :program: `pythonw.exe ` by default (both executables are located in the top-level
211- of your Python installation directory). This suppresses the terminal window on
212- startup.
206+ As of Python 3.3, Python includes a launcher which facilitates running Python
207+ scripts. See :ref: `launcher ` for more information.
208+
209+ Executing scripts without the Python launcher
210+ ---------------------------------------------
211+
212+ Without the Python launcher installed, Python scripts (files with the extension
213+ ``.py ``) will be executed by :program: `python.exe ` by default. This executable
214+ opens a terminal, which stays open even if the program uses a GUI. If you do
215+ not want this to happen, use the extension ``.pyw `` which will cause the script
216+ to be executed by :program: `pythonw.exe ` by default (both executables are
217+ located in the top-level of your Python installation directory). This
218+ suppresses the terminal window on startup.
213219
214220You can also make all ``.py `` scripts execute with :program: `pythonw.exe `,
215221setting this through the usual facilities, for example (might require
@@ -225,6 +231,250 @@ administrative rights):
225231 ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
226232
227233
234+ .. _launcher :
235+
236+ Python Launcher for Windows
237+ ===========================
238+
239+ .. versionadded :: 3.3
240+
241+ The Python launcher for Windows is a utility which aids in the location and
242+ execution of different Python versions. It allows scripts (or the
243+ command-line) to indicate a preference for a specific Python version, and
244+ will locate and execute that version.
245+
246+ Getting started
247+ ---------------
248+
249+ From the command-line
250+ ^^^^^^^^^^^^^^^^^^^^^
251+
252+ You should ensure the launcher is on your PATH - depending on how it was
253+ installed it may already be there, but check just in case it is not.
254+
255+ From a command-prompt, execute the following command:
256+
257+ ::
258+
259+ py
260+
261+ You should find that the latest version of Python 2.x you have installed is
262+ started - it can be exited as normal, and any additional command-line
263+ arguments specified will be sent directly to Python.
264+
265+ If you have multiple versions of Python 2.x installed (e.g., 2.6 and 2.7) you
266+ will have noticed that Python 2.7 was started - to launch Python 2.6, try the
267+ command:
268+
269+ ::
270+
271+ py -2.6
272+
273+ If you have a Python 3.x installed, try the command:
274+
275+ ::
276+
277+ py -3
278+
279+ You should find the latest version of Python 3.x starts.
280+
281+ From a script
282+ ^^^^^^^^^^^^^
283+
284+ Let's create a test Python script - create a file called ``hello.py `` with the
285+ following contents
286+
287+ ::
288+
289+ #! python
290+ import sys
291+ sys.stdout.write("hello from Python %s\n" % (sys.version,))
292+
293+ From the directory in which hello.py lives, execute the command:
294+
295+ ::
296+
297+ py hello.py
298+
299+ You should notice the version number of your latest Python 2.x installation
300+ is printed. Now try changing the first line to be:
301+
302+ ::
303+
304+ #! python3
305+
306+ Re-executing the command should now print the latest Python 3.x information.
307+ As with the above command-line examples, you can specify a more explicit
308+ version qualifier. Assuming you have Python 2.6 installed, try changing the
309+ first line to ``#! python2.6 `` and you should find the 2.6 version
310+ information printed.
311+
312+ From file associations
313+ ^^^^^^^^^^^^^^^^^^^^^^
314+
315+ The launcher should have been associated with Python files (i.e. ``.py ``,
316+ ``.pyw ``, ``.pyc ``, ``.pyo `` files) when it was installed. This means that
317+ when you double-click on one of these files from Windows explorer the launcher
318+ will be used, and therefore you can use the same facilities described above to
319+ have the script specify the version which should be used.
320+
321+ The key benefit of this is that a single launcher can support multiple Python
322+ versions at the same time depending on the contents of the first line.
323+
324+ Shebang Lines
325+ -------------
326+
327+ If the first line of a script file starts with ``#! ``, it is known as a
328+ "shebang" line. Linux and other Unix like operating systems have native
329+ support for such lines and are commonly used on such systems to indicate how
330+ a script should be executed. This launcher allows the same facilities to be
331+ using with Python scripts on Windows and the examples above demonstrate their
332+ use.
333+
334+ To allow shebang lines in Python scripts to be portable between Unix and
335+ Windows, this launcher supports a number of 'virtual' commands to specify
336+ which interpreter to use. The supported virtual commands are:
337+
338+ * ``/usr/bin/env python ``
339+ * ``/usr/bin/python ``
340+ * ``/usr/local/bin/python ``
341+ * ``python ``
342+
343+ For example, if the first line of your script starts with
344+
345+ ::
346+
347+ #! /usr/bin/python
348+
349+ The default Python will be located and used. As many Python scripts written
350+ to work on Unix will already have this line, you should find these scripts can
351+ be used by the launcher without modification. If you are writing a new script
352+ on Windows which you hope will be useful on Unix, you should use one of the
353+ shebang lines starting with ``/usr ``.
354+
355+ Arguments in shebang lines
356+ --------------------------
357+
358+ The shebang lines can also specify additional options to be passed to the
359+ Python interpreter. For example, if you have a shebang line:
360+
361+ ::
362+
363+ #! /usr/bin/python -v
364+
365+ Then Python will be started with the ``-v `` option
366+
367+ Customization
368+ -------------
369+
370+ Customization via INI files
371+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
372+
373+ Two .ini files will be searched by the launcher - ``py.ini `` in the
374+ current user's "application data" directory (i.e. the directory returned
375+ by calling the Windows function SHGetFolderPath with CSIDL_LOCAL_APPDATA)
376+ and ``py.ini `` in the same directory as the launcher. The same .ini
377+ files are used for both the 'console' version of the launcher (i.e.
378+ py.exe) and for the 'windows' version (i.e. pyw.exe)
379+
380+ Customization specified in the "application directory" will have
381+ precedence over the one next to the executable, so a user, who may not
382+ have write access to the .ini file next to the launcher, can override
383+ commands in that global .ini file)
384+
385+ Customizing default Python versions
386+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
387+
388+ In some cases, a version qualifier can be included in a command to dictate
389+ which version of Python will be used by the command. A version qualifier
390+ starts with a major version number and can optionally be followed by a period
391+ ('.') and a minor version specifier. If the minor qualifier is specified, it
392+ may optionally be followed by "-32" to indicate the 32-bit implementation of
393+ that version be used.
394+
395+ For example, a shebang line of ``#!python `` has no version qualifier, while
396+ ``#!python3 `` has a version qualifier which specifies only a major version.
397+
398+ If no version qualifiers are found in a command, the environment variable
399+ ``PY_PYTHON `` can be set to specify the default version qualifier - the default
400+ value is "2". Note this value could specify just a major version (e.g. "2") or
401+ a major.minor qualifier (e.g. "2.6"), or even major.minor-32.
402+
403+ If no minor version qualifiers are found, the environment variable
404+ ``PY_PYTHON{major} `` (where ``{major} `` is the current major version qualifier
405+ as determined above) can be set to specify the full version. If no such option
406+ is found, the launcher will enumerate the installed Python versions and use
407+ the latest minor release found for the major version, which is likely,
408+ although not guaranteed, to be the most recently installed version in that
409+ family.
410+
411+ On 64-bit Windows with both 32-bit and 64-bit implementations of the same
412+ (major.minor) Python version installed, the 64-bit version will always be
413+ preferred. This will be true for both 32-bit and 64-bit implementations of the
414+ launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation
415+ of the specified version if available. This is so the behavior of the launcher
416+ can be predicted knowing only what versions are installed on the PC and
417+ without regard to the order in which they were installed (i.e., without knowing
418+ whether a 32 or 64-bit version of Python and corresponding launcher was
419+ installed last). As noted above, an optional "-32" suffix can be used on a
420+ version specifier to change this behaviour.
421+
422+ Examples:
423+
424+ * If no relevant options are set, the commands ``python `` and
425+ ``python2 `` will use the latest Python 2.x version installed and
426+ the command ``python3 `` will use the latest Python 3.x installed.
427+
428+ * The commands ``python3.1 `` and ``python2.7 `` will not consult any
429+ options at all as the versions are fully specified.
430+
431+ * If ``PY_PYTHON=3 ``, the commands ``python `` and ``python3 `` will both use
432+ the latest installed Python 3 version.
433+
434+ * If ``PY_PYTHON=3.1-32 ``, the command ``python `` will use the 32-bit
435+ implementation of 3.1 whereas the command ``python3 `` will use the latest
436+ installed Python (PY_PYTHON was not considered at all as a major
437+ version was specified.)
438+
439+ * If ``PY_PYTHON=3 `` and ``PY_PYTHON3=3.1 ``, the commands
440+ ``python `` and ``python3 `` will both use specifically 3.1
441+
442+ In addition to environment variables, the same settings can be configured
443+ in the .INI file used by the launcher. The section in the INI file is
444+ called ``[defaults] `` and the key name will be the same as the
445+ environment variables without the leading ``PY\_ `` prefix (and note that
446+ the key names in the INI file are case insensitive.) The contents of
447+ an environment variable will override things specified in the INI file.
448+
449+ For example:
450+
451+ * Setting ``PY_PYTHON=3.1 `` is equivalent to the INI file containing:
452+
453+ ::
454+
455+ [defaults]
456+ python=3.1
457+
458+ * Setting ``PY_PYTHON=3 `` and ``PY_PYTHON3=3.1 `` is equivalent to the INI file
459+ containing:
460+
461+ ::
462+
463+ [defaults]
464+ python=3
465+ python3=3.1
466+
467+ Diagnostics
468+ -----------
469+
470+ If an environment variable ``PYLAUNCH_DEBUG `` is set (to any value), the
471+ launcher will print diagnostic information to stderr (i.e. to the console).
472+ While this information manages to be simultaneously verbose *and * terse, it
473+ should allow you to see what versions of Python were located, why a
474+ particular version was chosen and the exact command-line used to execute the
475+ target Python.
476+
477+
228478Additional modules
229479==================
230480
@@ -341,3 +591,7 @@ Other resources
341591 `A Python for Windows Tutorial <http://www.imladris.com/Scripts/PythonForWindows.html >`_
342592 by Amanda Birmingham, 2004
343593
594+ :pep: `397 ` - Python launcher for Windows
595+ The proposal for the launcher to be included in the Python distribution.
596+
597+
0 commit comments