xref: /third_party/python/Doc/using/windows.rst (revision 7db96d56)
17db96d56Sopenharmony_ci.. highlight:: none
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. _using-on-windows:
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ci*************************
67db96d56Sopenharmony_ci Using Python on Windows
77db96d56Sopenharmony_ci*************************
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci.. sectionauthor:: Robert Lehmann <lehmannro@gmail.com>
107db96d56Sopenharmony_ci.. sectionauthor:: Steve Dower <steve.dower@microsoft.com>
117db96d56Sopenharmony_ci
127db96d56Sopenharmony_ciThis document aims to give an overview of Windows-specific behaviour you should
137db96d56Sopenharmony_ciknow about when using Python on Microsoft Windows.
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ciUnlike most Unix systems and services, Windows does not include a system
167db96d56Sopenharmony_cisupported installation of Python. To make Python available, the CPython team
177db96d56Sopenharmony_cihas compiled Windows installers (MSI packages) with every `release
187db96d56Sopenharmony_ci<https://www.python.org/download/releases/>`_ for many years. These installers
197db96d56Sopenharmony_ciare primarily intended to add a per-user installation of Python, with the
207db96d56Sopenharmony_cicore interpreter and library being used by a single user. The installer is also
217db96d56Sopenharmony_ciable to install for all users of a single machine, and a separate ZIP file is
227db96d56Sopenharmony_ciavailable for application-local distributions.
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ciAs specified in :pep:`11`, a Python release only supports a Windows platform
257db96d56Sopenharmony_ciwhile Microsoft considers the platform under extended support. This means that
267db96d56Sopenharmony_ciPython |version| supports Windows 8.1 and newer. If you require Windows 7
277db96d56Sopenharmony_cisupport, please install Python 3.8.
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ciThere are a number of different installers available for Windows, each with
307db96d56Sopenharmony_cicertain benefits and downsides.
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ci:ref:`windows-full` contains all components and is the best option for
337db96d56Sopenharmony_cidevelopers using Python for any kind of project.
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ci:ref:`windows-store` is a simple installation of Python that is suitable for
367db96d56Sopenharmony_cirunning scripts and packages, and using IDLE or other development environments.
377db96d56Sopenharmony_ciIt requires Windows 10 and above, but can be safely installed without corrupting other
387db96d56Sopenharmony_ciprograms. It also provides many convenient commands for launching Python and
397db96d56Sopenharmony_ciits tools.
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci:ref:`windows-nuget` are lightweight installations intended for continuous
427db96d56Sopenharmony_ciintegration systems. It can be used to build Python packages or run scripts,
437db96d56Sopenharmony_cibut is not updateable and has no user interface tools.
447db96d56Sopenharmony_ci
457db96d56Sopenharmony_ci:ref:`windows-embeddable` is a minimal package of Python suitable for
467db96d56Sopenharmony_ciembedding into a larger application.
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ci
497db96d56Sopenharmony_ci.. _windows-full:
507db96d56Sopenharmony_ci
517db96d56Sopenharmony_ciThe full installer
527db96d56Sopenharmony_ci==================
537db96d56Sopenharmony_ci
547db96d56Sopenharmony_ciInstallation steps
557db96d56Sopenharmony_ci------------------
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ciFour Python |version| installers are available for download - two each for the
587db96d56Sopenharmony_ci32-bit and 64-bit versions of the interpreter. The *web installer* is a small
597db96d56Sopenharmony_ciinitial download, and it will automatically download the required components as
607db96d56Sopenharmony_cinecessary. The *offline installer* includes the components necessary for a
617db96d56Sopenharmony_cidefault installation and only requires an internet connection for optional
627db96d56Sopenharmony_cifeatures. See :ref:`install-layout-option` for other ways to avoid downloading
637db96d56Sopenharmony_ciduring installation.
647db96d56Sopenharmony_ci
657db96d56Sopenharmony_ciAfter starting the installer, one of two options may be selected:
667db96d56Sopenharmony_ci
677db96d56Sopenharmony_ci.. image:: win_installer.png
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ciIf you select "Install Now":
707db96d56Sopenharmony_ci
717db96d56Sopenharmony_ci* You will *not* need to be an administrator (unless a system update for the
727db96d56Sopenharmony_ci  C Runtime Library is required or you install the :ref:`launcher` for all
737db96d56Sopenharmony_ci  users)
747db96d56Sopenharmony_ci* Python will be installed into your user directory
757db96d56Sopenharmony_ci* The :ref:`launcher` will be installed according to the option at the bottom
767db96d56Sopenharmony_ci  of the first page
777db96d56Sopenharmony_ci* The standard library, test suite, launcher and pip will be installed
787db96d56Sopenharmony_ci* If selected, the install directory will be added to your :envvar:`PATH`
797db96d56Sopenharmony_ci* Shortcuts will only be visible for the current user
807db96d56Sopenharmony_ci
817db96d56Sopenharmony_ciSelecting "Customize installation" will allow you to select the features to
827db96d56Sopenharmony_ciinstall, the installation location and other options or post-install actions.
837db96d56Sopenharmony_ciTo install debugging symbols or binaries, you will need to use this option.
847db96d56Sopenharmony_ci
857db96d56Sopenharmony_ciTo perform an all-users installation, you should select "Customize
867db96d56Sopenharmony_ciinstallation". In this case:
877db96d56Sopenharmony_ci
887db96d56Sopenharmony_ci* You may be required to provide administrative credentials or approval
897db96d56Sopenharmony_ci* Python will be installed into the Program Files directory
907db96d56Sopenharmony_ci* The :ref:`launcher` will be installed into the Windows directory
917db96d56Sopenharmony_ci* Optional features may be selected during installation
927db96d56Sopenharmony_ci* The standard library can be pre-compiled to bytecode
937db96d56Sopenharmony_ci* If selected, the install directory will be added to the system :envvar:`PATH`
947db96d56Sopenharmony_ci* Shortcuts are available for all users
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci.. _max-path:
977db96d56Sopenharmony_ci
987db96d56Sopenharmony_ciRemoving the MAX_PATH Limitation
997db96d56Sopenharmony_ci--------------------------------
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ciWindows historically has limited path lengths to 260 characters. This meant that
1027db96d56Sopenharmony_cipaths longer than this would not resolve and errors would result.
1037db96d56Sopenharmony_ci
1047db96d56Sopenharmony_ciIn the latest versions of Windows, this limitation can be expanded to
1057db96d56Sopenharmony_ciapproximately 32,000 characters. Your administrator will need to activate the
1067db96d56Sopenharmony_ci"Enable Win32 long paths" group policy, or set ``LongPathsEnabled`` to ``1``
1077db96d56Sopenharmony_ciin the registry key
1087db96d56Sopenharmony_ci``HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem``.
1097db96d56Sopenharmony_ci
1107db96d56Sopenharmony_ciThis allows the :func:`open` function, the :mod:`os` module and most other
1117db96d56Sopenharmony_cipath functionality to accept and return paths longer than 260 characters.
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ciAfter changing the above option, no further configuration is required.
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ci.. versionchanged:: 3.6
1167db96d56Sopenharmony_ci
1177db96d56Sopenharmony_ci   Support for long paths was enabled in Python.
1187db96d56Sopenharmony_ci
1197db96d56Sopenharmony_ci.. _install-quiet-option:
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ciInstalling Without UI
1227db96d56Sopenharmony_ci---------------------
1237db96d56Sopenharmony_ci
1247db96d56Sopenharmony_ciAll of the options available in the installer UI can also be specified from the
1257db96d56Sopenharmony_cicommand line, allowing scripted installers to replicate an installation on many
1267db96d56Sopenharmony_cimachines without user interaction.  These options may also be set without
1277db96d56Sopenharmony_cisuppressing the UI in order to change some of the defaults.
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ciTo completely hide the installer UI and install Python silently, pass the
1307db96d56Sopenharmony_ci``/quiet`` option. To skip past the user interaction but still display
1317db96d56Sopenharmony_ciprogress and errors, pass the ``/passive`` option. The ``/uninstall``
1327db96d56Sopenharmony_cioption may be passed to immediately begin removing Python - no confirmation
1337db96d56Sopenharmony_ciprompt will be displayed.
1347db96d56Sopenharmony_ci
1357db96d56Sopenharmony_ciAll other options are passed as ``name=value``, where the value is usually
1367db96d56Sopenharmony_ci``0`` to disable a feature, ``1`` to enable a feature, or a path. The full list
1377db96d56Sopenharmony_ciof available options is shown below.
1387db96d56Sopenharmony_ci
1397db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1407db96d56Sopenharmony_ci| Name                      | Description                          | Default                  |
1417db96d56Sopenharmony_ci+===========================+======================================+==========================+
1427db96d56Sopenharmony_ci| InstallAllUsers           | Perform a system-wide installation.  | 0                        |
1437db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1447db96d56Sopenharmony_ci| TargetDir                 | The installation directory           | Selected based on        |
1457db96d56Sopenharmony_ci|                           |                                      | InstallAllUsers          |
1467db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1477db96d56Sopenharmony_ci| DefaultAllUsersTargetDir  | The default installation directory   | :file:`%ProgramFiles%\\\ |
1487db96d56Sopenharmony_ci|                           | for all-user installs                | Python X.Y` or :file:`\  |
1497db96d56Sopenharmony_ci|                           |                                      | %ProgramFiles(x86)%\\\   |
1507db96d56Sopenharmony_ci|                           |                                      | Python X.Y`              |
1517db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1527db96d56Sopenharmony_ci| DefaultJustForMeTargetDir | The default install directory for    | :file:`%LocalAppData%\\\ |
1537db96d56Sopenharmony_ci|                           | just-for-me installs                 | Programs\\Python\\\      |
1547db96d56Sopenharmony_ci|                           |                                      | PythonXY` or             |
1557db96d56Sopenharmony_ci|                           |                                      | :file:`%LocalAppData%\\\ |
1567db96d56Sopenharmony_ci|                           |                                      | Programs\\Python\\\      |
1577db96d56Sopenharmony_ci|                           |                                      | PythonXY-32` or          |
1587db96d56Sopenharmony_ci|                           |                                      | :file:`%LocalAppData%\\\ |
1597db96d56Sopenharmony_ci|                           |                                      | Programs\\Python\\\      |
1607db96d56Sopenharmony_ci|                           |                                      | PythonXY-64`             |
1617db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1627db96d56Sopenharmony_ci| DefaultCustomTargetDir    | The default custom install directory | (empty)                  |
1637db96d56Sopenharmony_ci|                           | displayed in the UI                  |                          |
1647db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1657db96d56Sopenharmony_ci| AssociateFiles            | Create file associations if the      | 1                        |
1667db96d56Sopenharmony_ci|                           | launcher is also installed.          |                          |
1677db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1687db96d56Sopenharmony_ci| CompileAll                | Compile all ``.py`` files to         | 0                        |
1697db96d56Sopenharmony_ci|                           | ``.pyc``.                            |                          |
1707db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1717db96d56Sopenharmony_ci| PrependPath               | Prepend install and Scripts          | 0                        |
1727db96d56Sopenharmony_ci|                           | directories  to :envvar:`PATH` and   |                          |
1737db96d56Sopenharmony_ci|                           | add ``.PY`` to :envvar:`PATHEXT`     |                          |
1747db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1757db96d56Sopenharmony_ci| AppendPath                | Append install and Scripts           | 0                        |
1767db96d56Sopenharmony_ci|                           | directories  to :envvar:`PATH` and   |                          |
1777db96d56Sopenharmony_ci|                           | add ``.PY`` to :envvar:`PATHEXT`     |                          |
1787db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1797db96d56Sopenharmony_ci| Shortcuts                 | Create shortcuts for the interpreter,| 1                        |
1807db96d56Sopenharmony_ci|                           | documentation and IDLE if installed. |                          |
1817db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1827db96d56Sopenharmony_ci| Include_doc               | Install Python manual                | 1                        |
1837db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1847db96d56Sopenharmony_ci| Include_debug             | Install debug binaries               | 0                        |
1857db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1867db96d56Sopenharmony_ci| Include_dev               | Install developer headers and        | 1                        |
1877db96d56Sopenharmony_ci|                           | libraries. Omitting this may lead to |                          |
1887db96d56Sopenharmony_ci|                           | an unusable installation.            |                          |
1897db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1907db96d56Sopenharmony_ci| Include_exe               | Install :file:`python.exe` and       | 1                        |
1917db96d56Sopenharmony_ci|                           | related files. Omitting this may     |                          |
1927db96d56Sopenharmony_ci|                           | lead to an unusable installation.    |                          |
1937db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1947db96d56Sopenharmony_ci| Include_launcher          | Install :ref:`launcher`.             | 1                        |
1957db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
1967db96d56Sopenharmony_ci| InstallLauncherAllUsers   | Installs the launcher for all        | 1                        |
1977db96d56Sopenharmony_ci|                           | users. Also requires                 |                          |
1987db96d56Sopenharmony_ci|                           | ``Include_launcher`` to be set to 1  |                          |
1997db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2007db96d56Sopenharmony_ci| Include_lib               | Install standard library and         | 1                        |
2017db96d56Sopenharmony_ci|                           | extension modules. Omitting this may |                          |
2027db96d56Sopenharmony_ci|                           | lead to an unusable installation.    |                          |
2037db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2047db96d56Sopenharmony_ci| Include_pip               | Install bundled pip and setuptools   | 1                        |
2057db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2067db96d56Sopenharmony_ci| Include_symbols           | Install debugging symbols (``*.pdb``)| 0                        |
2077db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2087db96d56Sopenharmony_ci| Include_tcltk             | Install Tcl/Tk support and IDLE      | 1                        |
2097db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2107db96d56Sopenharmony_ci| Include_test              | Install standard library test suite  | 1                        |
2117db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2127db96d56Sopenharmony_ci| Include_tools             | Install utility scripts              | 1                        |
2137db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2147db96d56Sopenharmony_ci| LauncherOnly              | Only installs the launcher. This     | 0                        |
2157db96d56Sopenharmony_ci|                           | will override most other options.    |                          |
2167db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2177db96d56Sopenharmony_ci| SimpleInstall             | Disable most install UI              | 0                        |
2187db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2197db96d56Sopenharmony_ci| SimpleInstallDescription  | A custom message to display when the | (empty)                  |
2207db96d56Sopenharmony_ci|                           | simplified install UI is used.       |                          |
2217db96d56Sopenharmony_ci+---------------------------+--------------------------------------+--------------------------+
2227db96d56Sopenharmony_ci
2237db96d56Sopenharmony_ciFor example, to silently install a default, system-wide Python installation,
2247db96d56Sopenharmony_ciyou could use the following command (from an elevated command prompt)::
2257db96d56Sopenharmony_ci
2267db96d56Sopenharmony_ci    python-3.9.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
2277db96d56Sopenharmony_ci
2287db96d56Sopenharmony_ciTo allow users to easily install a personal copy of Python without the test
2297db96d56Sopenharmony_cisuite, you could provide a shortcut with the following command. This will
2307db96d56Sopenharmony_cidisplay a simplified initial page and disallow customization::
2317db96d56Sopenharmony_ci
2327db96d56Sopenharmony_ci    python-3.9.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
2337db96d56Sopenharmony_ci        SimpleInstall=1 SimpleInstallDescription="Just for me, no test suite."
2347db96d56Sopenharmony_ci
2357db96d56Sopenharmony_ci(Note that omitting the launcher also omits file associations, and is only
2367db96d56Sopenharmony_cirecommended for per-user installs when there is also a system-wide installation
2377db96d56Sopenharmony_cithat included the launcher.)
2387db96d56Sopenharmony_ci
2397db96d56Sopenharmony_ciThe options listed above can also be provided in a file named ``unattend.xml``
2407db96d56Sopenharmony_cialongside the executable. This file specifies a list of options and values.
2417db96d56Sopenharmony_ciWhen a value is provided as an attribute, it will be converted to a number if
2427db96d56Sopenharmony_cipossible. Values provided as element text are always left as strings. This
2437db96d56Sopenharmony_ciexample file sets the same options as the previous example:
2447db96d56Sopenharmony_ci
2457db96d56Sopenharmony_ci.. code-block:: xml
2467db96d56Sopenharmony_ci
2477db96d56Sopenharmony_ci    <Options>
2487db96d56Sopenharmony_ci        <Option Name="InstallAllUsers" Value="no" />
2497db96d56Sopenharmony_ci        <Option Name="Include_launcher" Value="0" />
2507db96d56Sopenharmony_ci        <Option Name="Include_test" Value="no" />
2517db96d56Sopenharmony_ci        <Option Name="SimpleInstall" Value="yes" />
2527db96d56Sopenharmony_ci        <Option Name="SimpleInstallDescription">Just for me, no test suite</Option>
2537db96d56Sopenharmony_ci    </Options>
2547db96d56Sopenharmony_ci
2557db96d56Sopenharmony_ci.. _install-layout-option:
2567db96d56Sopenharmony_ci
2577db96d56Sopenharmony_ciInstalling Without Downloading
2587db96d56Sopenharmony_ci------------------------------
2597db96d56Sopenharmony_ci
2607db96d56Sopenharmony_ciAs some features of Python are not included in the initial installer download,
2617db96d56Sopenharmony_ciselecting those features may require an internet connection.  To avoid this
2627db96d56Sopenharmony_cineed, all possible components may be downloaded on-demand to create a complete
2637db96d56Sopenharmony_ci*layout* that will no longer require an internet connection regardless of the
2647db96d56Sopenharmony_ciselected features. Note that this download may be bigger than required, but
2657db96d56Sopenharmony_ciwhere a large number of installations are going to be performed it is very
2667db96d56Sopenharmony_ciuseful to have a locally cached copy.
2677db96d56Sopenharmony_ci
2687db96d56Sopenharmony_ciExecute the following command from Command Prompt to download all possible
2697db96d56Sopenharmony_cirequired files.  Remember to substitute ``python-3.9.0.exe`` for the actual
2707db96d56Sopenharmony_ciname of your installer, and to create layouts in their own directories to
2717db96d56Sopenharmony_ciavoid collisions between files with the same name.
2727db96d56Sopenharmony_ci
2737db96d56Sopenharmony_ci::
2747db96d56Sopenharmony_ci
2757db96d56Sopenharmony_ci    python-3.9.0.exe /layout [optional target directory]
2767db96d56Sopenharmony_ci
2777db96d56Sopenharmony_ciYou may also specify the ``/quiet`` option to hide the progress display.
2787db96d56Sopenharmony_ci
2797db96d56Sopenharmony_ciModifying an install
2807db96d56Sopenharmony_ci--------------------
2817db96d56Sopenharmony_ci
2827db96d56Sopenharmony_ciOnce Python has been installed, you can add or remove features through the
2837db96d56Sopenharmony_ciPrograms and Features tool that is part of Windows. Select the Python entry and
2847db96d56Sopenharmony_cichoose "Uninstall/Change" to open the installer in maintenance mode.
2857db96d56Sopenharmony_ci
2867db96d56Sopenharmony_ci"Modify" allows you to add or remove features by modifying the checkboxes -
2877db96d56Sopenharmony_ciunchanged checkboxes will not install or remove anything. Some options cannot be
2887db96d56Sopenharmony_cichanged in this mode, such as the install directory; to modify these, you will
2897db96d56Sopenharmony_cineed to remove and then reinstall Python completely.
2907db96d56Sopenharmony_ci
2917db96d56Sopenharmony_ci"Repair" will verify all the files that should be installed using the current
2927db96d56Sopenharmony_cisettings and replace any that have been removed or modified.
2937db96d56Sopenharmony_ci
2947db96d56Sopenharmony_ci"Uninstall" will remove Python entirely, with the exception of the
2957db96d56Sopenharmony_ci:ref:`launcher`, which has its own entry in Programs and Features.
2967db96d56Sopenharmony_ci
2977db96d56Sopenharmony_ci
2987db96d56Sopenharmony_ci.. _windows-store:
2997db96d56Sopenharmony_ci
3007db96d56Sopenharmony_ciThe Microsoft Store package
3017db96d56Sopenharmony_ci===========================
3027db96d56Sopenharmony_ci
3037db96d56Sopenharmony_ci.. versionadded:: 3.7.2
3047db96d56Sopenharmony_ci
3057db96d56Sopenharmony_ciThe Microsoft Store package is an easily installable Python interpreter that
3067db96d56Sopenharmony_ciis intended mainly for interactive use, for example, by students.
3077db96d56Sopenharmony_ci
3087db96d56Sopenharmony_ciTo install the package, ensure you have the latest Windows 10 updates and
3097db96d56Sopenharmony_cisearch the Microsoft Store app for "Python |version|". Ensure that the app
3107db96d56Sopenharmony_ciyou select is published by the Python Software Foundation, and install it.
3117db96d56Sopenharmony_ci
3127db96d56Sopenharmony_ci.. warning::
3137db96d56Sopenharmony_ci   Python will always be available for free on the Microsoft Store. If you
3147db96d56Sopenharmony_ci   are asked to pay for it, you have not selected the correct package.
3157db96d56Sopenharmony_ci
3167db96d56Sopenharmony_ciAfter installation, Python may be launched by finding it in Start.
3177db96d56Sopenharmony_ciAlternatively, it will be available from any Command Prompt or PowerShell
3187db96d56Sopenharmony_cisession by typing ``python``. Further, pip and IDLE may be used by typing
3197db96d56Sopenharmony_ci``pip`` or ``idle``. IDLE can also be found in Start.
3207db96d56Sopenharmony_ci
3217db96d56Sopenharmony_ciAll three commands are also available with version number suffixes, for
3227db96d56Sopenharmony_ciexample, as ``python3.exe`` and ``python3.x.exe`` as well as
3237db96d56Sopenharmony_ci``python.exe`` (where ``3.x`` is the specific version you want to launch,
3247db96d56Sopenharmony_cisuch as |version|). Open "Manage App Execution Aliases" through Start to
3257db96d56Sopenharmony_ciselect which version of Python is associated with each command. It is
3267db96d56Sopenharmony_cirecommended to make sure that ``pip`` and ``idle`` are consistent with
3277db96d56Sopenharmony_ciwhichever version of ``python`` is selected.
3287db96d56Sopenharmony_ci
3297db96d56Sopenharmony_ciVirtual environments can be created with ``python -m venv`` and activated
3307db96d56Sopenharmony_ciand used as normal.
3317db96d56Sopenharmony_ci
3327db96d56Sopenharmony_ciIf you have installed another version of Python and added it to your
3337db96d56Sopenharmony_ci``PATH`` variable, it will be available as ``python.exe`` rather than the
3347db96d56Sopenharmony_cione from the Microsoft Store. To access the new installation, use
3357db96d56Sopenharmony_ci``python3.exe`` or ``python3.x.exe``.
3367db96d56Sopenharmony_ci
3377db96d56Sopenharmony_ciThe ``py.exe`` launcher will detect this Python installation, but will prefer
3387db96d56Sopenharmony_ciinstallations from the traditional installer.
3397db96d56Sopenharmony_ci
3407db96d56Sopenharmony_ciTo remove Python, open Settings and use Apps and Features, or else find
3417db96d56Sopenharmony_ciPython in Start and right-click to select Uninstall. Uninstalling will
3427db96d56Sopenharmony_ciremove all packages you installed directly into this Python installation, but
3437db96d56Sopenharmony_ciwill not remove any virtual environments
3447db96d56Sopenharmony_ci
3457db96d56Sopenharmony_ciKnown issues
3467db96d56Sopenharmony_ci------------
3477db96d56Sopenharmony_ci
3487db96d56Sopenharmony_ciRedirection of local data, registry, and temporary paths
3497db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3507db96d56Sopenharmony_ci
3517db96d56Sopenharmony_ciBecause of restrictions on Microsoft Store apps, Python scripts may not have
3527db96d56Sopenharmony_cifull write access to shared locations such as :envvar:`TEMP` and the registry.
3537db96d56Sopenharmony_ciInstead, it will write to a private copy. If your scripts must modify the
3547db96d56Sopenharmony_cishared locations, you will need to install the full installer.
3557db96d56Sopenharmony_ci
3567db96d56Sopenharmony_ciAt runtime, Python will use a private copy of well-known Windows folders and the registry.
3577db96d56Sopenharmony_ciFor example, if the environment variable :envvar:`%APPDATA%` is :file:`c:\\Users\\<user>\\AppData\\`,
3587db96d56Sopenharmony_cithen when writing to :file:`C:\\Users\\<user>\\AppData\\Local` will write to
3597db96d56Sopenharmony_ci:file:`C:\\Users\\<user>\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\\LocalCache\\Local\\`.
3607db96d56Sopenharmony_ci
3617db96d56Sopenharmony_ciWhen reading files, Windows will return the file from the private folder, or if that does not exist, the
3627db96d56Sopenharmony_cireal Windows directory. For example reading :file:`C:\\Windows\\System32` returns the contents of :file:`C:\\Windows\\System32`
3637db96d56Sopenharmony_ciplus the contents of :file:`C:\\Program Files\\WindowsApps\\package_name\\VFS\\SystemX86`.
3647db96d56Sopenharmony_ci
3657db96d56Sopenharmony_ciYou can find the real path of any existing file using :func:`os.path.realpath`:
3667db96d56Sopenharmony_ci
3677db96d56Sopenharmony_ci.. code-block:: python
3687db96d56Sopenharmony_ci
3697db96d56Sopenharmony_ci  >>> import os
3707db96d56Sopenharmony_ci  >>> test_file = 'C:\\Users\\example\\AppData\\Local\\test.txt'
3717db96d56Sopenharmony_ci  >>> os.path.realpath(test_file)
3727db96d56Sopenharmony_ci  'C:\\Users\\example\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\\LocalCache\\Local\\test.txt'
3737db96d56Sopenharmony_ci
3747db96d56Sopenharmony_ciWhen writing to the Windows Registry, the following behaviors exist:
3757db96d56Sopenharmony_ci
3767db96d56Sopenharmony_ci* Reading from ``HKLM\\Software`` is allowed and results are merged with the :file:`registry.dat` file in the package.
3777db96d56Sopenharmony_ci* Writing to ``HKLM\\Software`` is not allowed if the corresponding key/value exists, i.e. modifying existing keys.
3787db96d56Sopenharmony_ci* Writing to ``HKLM\\Software`` is allowed as long as a corresponding key/value does not exist in the package
3797db96d56Sopenharmony_ci  and the user has the correct access permissions.
3807db96d56Sopenharmony_ci
3817db96d56Sopenharmony_ciFor more detail on the technical basis for these limitations, please consult
3827db96d56Sopenharmony_ciMicrosoft's documentation on packaged full-trust apps, currently available at
3837db96d56Sopenharmony_ci`docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-behind-the-scenes
3847db96d56Sopenharmony_ci<https://docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-behind-the-scenes>`_
3857db96d56Sopenharmony_ci
3867db96d56Sopenharmony_ci
3877db96d56Sopenharmony_ci.. _windows-nuget:
3887db96d56Sopenharmony_ci
3897db96d56Sopenharmony_ciThe nuget.org packages
3907db96d56Sopenharmony_ci======================
3917db96d56Sopenharmony_ci
3927db96d56Sopenharmony_ci.. versionadded:: 3.5.2
3937db96d56Sopenharmony_ci
3947db96d56Sopenharmony_ciThe nuget.org package is a reduced size Python environment intended for use on
3957db96d56Sopenharmony_cicontinuous integration and build systems that do not have a system-wide
3967db96d56Sopenharmony_ciinstall of Python. While nuget is "the package manager for .NET", it also works
3977db96d56Sopenharmony_ciperfectly fine for packages containing build-time tools.
3987db96d56Sopenharmony_ci
3997db96d56Sopenharmony_ciVisit `nuget.org <https://www.nuget.org/>`_ for the most up-to-date information
4007db96d56Sopenharmony_cion using nuget. What follows is a summary that is sufficient for Python
4017db96d56Sopenharmony_cidevelopers.
4027db96d56Sopenharmony_ci
4037db96d56Sopenharmony_ciThe ``nuget.exe`` command line tool may be downloaded directly from
4047db96d56Sopenharmony_ci``https://aka.ms/nugetclidl``, for example, using curl or PowerShell. With the
4057db96d56Sopenharmony_citool, the latest version of Python for 64-bit or 32-bit machines is installed
4067db96d56Sopenharmony_ciusing::
4077db96d56Sopenharmony_ci
4087db96d56Sopenharmony_ci   nuget.exe install python -ExcludeVersion -OutputDirectory .
4097db96d56Sopenharmony_ci   nuget.exe install pythonx86 -ExcludeVersion -OutputDirectory .
4107db96d56Sopenharmony_ci
4117db96d56Sopenharmony_ciTo select a particular version, add a ``-Version 3.x.y``. The output directory
4127db96d56Sopenharmony_cimay be changed from ``.``, and the package will be installed into a
4137db96d56Sopenharmony_cisubdirectory. By default, the subdirectory is named the same as the package,
4147db96d56Sopenharmony_ciand without the ``-ExcludeVersion`` option this name will include the specific
4157db96d56Sopenharmony_civersion installed. Inside the subdirectory is a ``tools`` directory that
4167db96d56Sopenharmony_cicontains the Python installation:
4177db96d56Sopenharmony_ci
4187db96d56Sopenharmony_ci.. code-block:: doscon
4197db96d56Sopenharmony_ci
4207db96d56Sopenharmony_ci   # Without -ExcludeVersion
4217db96d56Sopenharmony_ci   > .\python.3.5.2\tools\python.exe -V
4227db96d56Sopenharmony_ci   Python 3.5.2
4237db96d56Sopenharmony_ci
4247db96d56Sopenharmony_ci   # With -ExcludeVersion
4257db96d56Sopenharmony_ci   > .\python\tools\python.exe -V
4267db96d56Sopenharmony_ci   Python 3.5.2
4277db96d56Sopenharmony_ci
4287db96d56Sopenharmony_ciIn general, nuget packages are not upgradeable, and newer versions should be
4297db96d56Sopenharmony_ciinstalled side-by-side and referenced using the full path. Alternatively,
4307db96d56Sopenharmony_cidelete the package directory manually and install it again. Many CI systems
4317db96d56Sopenharmony_ciwill do this automatically if they do not preserve files between builds.
4327db96d56Sopenharmony_ci
4337db96d56Sopenharmony_ciAlongside the ``tools`` directory is a ``build\native`` directory. This
4347db96d56Sopenharmony_cicontains a MSBuild properties file ``python.props`` that can be used in a
4357db96d56Sopenharmony_ciC++ project to reference the Python install. Including the settings will
4367db96d56Sopenharmony_ciautomatically use the headers and import libraries in your build.
4377db96d56Sopenharmony_ci
4387db96d56Sopenharmony_ciThe package information pages on nuget.org are
4397db96d56Sopenharmony_ci`www.nuget.org/packages/python <https://www.nuget.org/packages/python>`_
4407db96d56Sopenharmony_cifor the 64-bit version and `www.nuget.org/packages/pythonx86
4417db96d56Sopenharmony_ci<https://www.nuget.org/packages/pythonx86>`_ for the 32-bit version.
4427db96d56Sopenharmony_ci
4437db96d56Sopenharmony_ci
4447db96d56Sopenharmony_ci.. _windows-embeddable:
4457db96d56Sopenharmony_ci
4467db96d56Sopenharmony_ciThe embeddable package
4477db96d56Sopenharmony_ci======================
4487db96d56Sopenharmony_ci
4497db96d56Sopenharmony_ci.. versionadded:: 3.5
4507db96d56Sopenharmony_ci
4517db96d56Sopenharmony_ciThe embedded distribution is a ZIP file containing a minimal Python environment.
4527db96d56Sopenharmony_ciIt is intended for acting as part of another application, rather than being
4537db96d56Sopenharmony_cidirectly accessed by end-users.
4547db96d56Sopenharmony_ci
4557db96d56Sopenharmony_ciWhen extracted, the embedded distribution is (almost) fully isolated from the
4567db96d56Sopenharmony_ciuser's system, including environment variables, system registry settings, and
4577db96d56Sopenharmony_ciinstalled packages. The standard library is included as pre-compiled and
4587db96d56Sopenharmony_cioptimized ``.pyc`` files in a ZIP, and ``python3.dll``, ``python37.dll``,
4597db96d56Sopenharmony_ci``python.exe`` and ``pythonw.exe`` are all provided. Tcl/tk (including all
4607db96d56Sopenharmony_cidependents, such as Idle), pip and the Python documentation are not included.
4617db96d56Sopenharmony_ci
4627db96d56Sopenharmony_ci.. note::
4637db96d56Sopenharmony_ci
4647db96d56Sopenharmony_ci    The embedded distribution does not include the `Microsoft C Runtime
4657db96d56Sopenharmony_ci    <https://docs.microsoft.com/en-US/cpp/windows/latest-supported-vc-redist#visual-studio-2015-2017-2019-and-2022>`_ and it is
4667db96d56Sopenharmony_ci    the responsibility of the application installer to provide this. The
4677db96d56Sopenharmony_ci    runtime may have already been installed on a user's system previously or
4687db96d56Sopenharmony_ci    automatically via Windows Update, and can be detected by finding
4697db96d56Sopenharmony_ci    ``ucrtbase.dll`` in the system directory.
4707db96d56Sopenharmony_ci
4717db96d56Sopenharmony_ciThird-party packages should be installed by the application installer alongside
4727db96d56Sopenharmony_cithe embedded distribution. Using pip to manage dependencies as for a regular
4737db96d56Sopenharmony_ciPython installation is not supported with this distribution, though with some
4747db96d56Sopenharmony_cicare it may be possible to include and use pip for automatic updates. In
4757db96d56Sopenharmony_cigeneral, third-party packages should be treated as part of the application
4767db96d56Sopenharmony_ci("vendoring") so that the developer can ensure compatibility with newer
4777db96d56Sopenharmony_civersions before providing updates to users.
4787db96d56Sopenharmony_ci
4797db96d56Sopenharmony_ciThe two recommended use cases for this distribution are described below.
4807db96d56Sopenharmony_ci
4817db96d56Sopenharmony_ciPython Application
4827db96d56Sopenharmony_ci------------------
4837db96d56Sopenharmony_ci
4847db96d56Sopenharmony_ciAn application written in Python does not necessarily require users to be aware
4857db96d56Sopenharmony_ciof that fact. The embedded distribution may be used in this case to include a
4867db96d56Sopenharmony_ciprivate version of Python in an install package. Depending on how transparent it
4877db96d56Sopenharmony_cishould be (or conversely, how professional it should appear), there are two
4887db96d56Sopenharmony_cioptions.
4897db96d56Sopenharmony_ci
4907db96d56Sopenharmony_ciUsing a specialized executable as a launcher requires some coding, but provides
4917db96d56Sopenharmony_cithe most transparent experience for users. With a customized launcher, there are
4927db96d56Sopenharmony_cino obvious indications that the program is running on Python: icons can be
4937db96d56Sopenharmony_cicustomized, company and version information can be specified, and file
4947db96d56Sopenharmony_ciassociations behave properly. In most cases, a custom launcher should simply be
4957db96d56Sopenharmony_ciable to call ``Py_Main`` with a hard-coded command line.
4967db96d56Sopenharmony_ci
4977db96d56Sopenharmony_ciThe simpler approach is to provide a batch file or generated shortcut that
4987db96d56Sopenharmony_cidirectly calls the ``python.exe`` or ``pythonw.exe`` with the required
4997db96d56Sopenharmony_cicommand-line arguments. In this case, the application will appear to be Python
5007db96d56Sopenharmony_ciand not its actual name, and users may have trouble distinguishing it from other
5017db96d56Sopenharmony_cirunning Python processes or file associations.
5027db96d56Sopenharmony_ci
5037db96d56Sopenharmony_ciWith the latter approach, packages should be installed as directories alongside
5047db96d56Sopenharmony_cithe Python executable to ensure they are available on the path. With the
5057db96d56Sopenharmony_cispecialized launcher, packages can be located in other locations as there is an
5067db96d56Sopenharmony_ciopportunity to specify the search path before launching the application.
5077db96d56Sopenharmony_ci
5087db96d56Sopenharmony_ciEmbedding Python
5097db96d56Sopenharmony_ci----------------
5107db96d56Sopenharmony_ci
5117db96d56Sopenharmony_ciApplications written in native code often require some form of scripting
5127db96d56Sopenharmony_cilanguage, and the embedded Python distribution can be used for this purpose. In
5137db96d56Sopenharmony_cigeneral, the majority of the application is in native code, and some part will
5147db96d56Sopenharmony_cieither invoke ``python.exe`` or directly use ``python3.dll``. For either case,
5157db96d56Sopenharmony_ciextracting the embedded distribution to a subdirectory of the application
5167db96d56Sopenharmony_ciinstallation is sufficient to provide a loadable Python interpreter.
5177db96d56Sopenharmony_ci
5187db96d56Sopenharmony_ciAs with the application use, packages can be installed to any location as there
5197db96d56Sopenharmony_ciis an opportunity to specify search paths before initializing the interpreter.
5207db96d56Sopenharmony_ciOtherwise, there is no fundamental differences between using the embedded
5217db96d56Sopenharmony_cidistribution and a regular installation.
5227db96d56Sopenharmony_ci
5237db96d56Sopenharmony_ci
5247db96d56Sopenharmony_ciAlternative bundles
5257db96d56Sopenharmony_ci===================
5267db96d56Sopenharmony_ci
5277db96d56Sopenharmony_ciBesides the standard CPython distribution, there are modified packages including
5287db96d56Sopenharmony_ciadditional functionality.  The following is a list of popular versions and their
5297db96d56Sopenharmony_cikey features:
5307db96d56Sopenharmony_ci
5317db96d56Sopenharmony_ci`ActivePython <https://www.activestate.com/products/python/>`_
5327db96d56Sopenharmony_ci    Installer with multi-platform compatibility, documentation, PyWin32
5337db96d56Sopenharmony_ci
5347db96d56Sopenharmony_ci`Anaconda <https://www.anaconda.com/download/>`_
5357db96d56Sopenharmony_ci    Popular scientific modules (such as numpy, scipy and pandas) and the
5367db96d56Sopenharmony_ci    ``conda`` package manager.
5377db96d56Sopenharmony_ci
5387db96d56Sopenharmony_ci`Enthought Deployment Manager <https://www.enthought.com/edm/>`_
5397db96d56Sopenharmony_ci    "The Next Generation Python Environment and Package Manager".
5407db96d56Sopenharmony_ci
5417db96d56Sopenharmony_ci    Previously Enthought provided Canopy, but it `reached end of life in 2016
5427db96d56Sopenharmony_ci    <https://support.enthought.com/hc/en-us/articles/360038600051-Canopy-GUI-end-of-life-transition-to-the-Enthought-Deployment-Manager-EDM-and-Visual-Studio-Code>`_.
5437db96d56Sopenharmony_ci
5447db96d56Sopenharmony_ci`WinPython <https://winpython.github.io/>`_
5457db96d56Sopenharmony_ci    Windows-specific distribution with prebuilt scientific packages and
5467db96d56Sopenharmony_ci    tools for building packages.
5477db96d56Sopenharmony_ci
5487db96d56Sopenharmony_ciNote that these packages may not include the latest versions of Python or
5497db96d56Sopenharmony_ciother libraries, and are not maintained or supported by the core Python team.
5507db96d56Sopenharmony_ci
5517db96d56Sopenharmony_ci
5527db96d56Sopenharmony_ci
5537db96d56Sopenharmony_ciConfiguring Python
5547db96d56Sopenharmony_ci==================
5557db96d56Sopenharmony_ci
5567db96d56Sopenharmony_ciTo run Python conveniently from a command prompt, you might consider changing
5577db96d56Sopenharmony_cisome default environment variables in Windows.  While the installer provides an
5587db96d56Sopenharmony_cioption to configure the PATH and PATHEXT variables for you, this is only
5597db96d56Sopenharmony_cireliable for a single, system-wide installation.  If you regularly use multiple
5607db96d56Sopenharmony_civersions of Python, consider using the :ref:`launcher`.
5617db96d56Sopenharmony_ci
5627db96d56Sopenharmony_ci
5637db96d56Sopenharmony_ci.. _setting-envvars:
5647db96d56Sopenharmony_ci
5657db96d56Sopenharmony_ciExcursus: Setting environment variables
5667db96d56Sopenharmony_ci---------------------------------------
5677db96d56Sopenharmony_ci
5687db96d56Sopenharmony_ciWindows allows environment variables to be configured permanently at both the
5697db96d56Sopenharmony_ciUser level and the System level, or temporarily in a command prompt.
5707db96d56Sopenharmony_ci
5717db96d56Sopenharmony_ciTo temporarily set environment variables, open Command Prompt and use the
5727db96d56Sopenharmony_ci:command:`set` command:
5737db96d56Sopenharmony_ci
5747db96d56Sopenharmony_ci.. code-block:: doscon
5757db96d56Sopenharmony_ci
5767db96d56Sopenharmony_ci    C:\>set PATH=C:\Program Files\Python 3.9;%PATH%
5777db96d56Sopenharmony_ci    C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
5787db96d56Sopenharmony_ci    C:\>python
5797db96d56Sopenharmony_ci
5807db96d56Sopenharmony_ciThese changes will apply to any further commands executed in that console, and
5817db96d56Sopenharmony_ciwill be inherited by any applications started from the console.
5827db96d56Sopenharmony_ci
5837db96d56Sopenharmony_ciIncluding the variable name within percent signs will expand to the existing
5847db96d56Sopenharmony_civalue, allowing you to add your new value at either the start or the end.
5857db96d56Sopenharmony_ciModifying :envvar:`PATH` by adding the directory containing
5867db96d56Sopenharmony_ci:program:`python.exe` to the start is a common way to ensure the correct version
5877db96d56Sopenharmony_ciof Python is launched.
5887db96d56Sopenharmony_ci
5897db96d56Sopenharmony_ciTo permanently modify the default environment variables, click Start and search
5907db96d56Sopenharmony_cifor 'edit environment variables', or open System properties, :guilabel:`Advanced
5917db96d56Sopenharmony_cisystem settings` and click the :guilabel:`Environment Variables` button.
5927db96d56Sopenharmony_ciIn this dialog, you can add or modify User and System variables. To change
5937db96d56Sopenharmony_ciSystem variables, you need non-restricted access to your machine
5947db96d56Sopenharmony_ci(i.e. Administrator rights).
5957db96d56Sopenharmony_ci
5967db96d56Sopenharmony_ci.. note::
5977db96d56Sopenharmony_ci
5987db96d56Sopenharmony_ci    Windows will concatenate User variables *after* System variables, which may
5997db96d56Sopenharmony_ci    cause unexpected results when modifying :envvar:`PATH`.
6007db96d56Sopenharmony_ci
6017db96d56Sopenharmony_ci    The :envvar:`PYTHONPATH` variable is used by all versions of Python,
6027db96d56Sopenharmony_ci    so you should not permanently configure it unless the listed paths
6037db96d56Sopenharmony_ci    only include code that is compatible with all of your installed Python
6047db96d56Sopenharmony_ci    versions.
6057db96d56Sopenharmony_ci
6067db96d56Sopenharmony_ci.. seealso::
6077db96d56Sopenharmony_ci
6087db96d56Sopenharmony_ci    https://docs.microsoft.com/en-us/windows/win32/procthread/environment-variables
6097db96d56Sopenharmony_ci      Overview of environment variables on Windows
6107db96d56Sopenharmony_ci
6117db96d56Sopenharmony_ci    https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1
6127db96d56Sopenharmony_ci      The ``set`` command, for temporarily modifying environment variables
6137db96d56Sopenharmony_ci
6147db96d56Sopenharmony_ci    https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/setx
6157db96d56Sopenharmony_ci      The ``setx`` command, for permanently modifying environment variables
6167db96d56Sopenharmony_ci
6177db96d56Sopenharmony_ci
6187db96d56Sopenharmony_ci.. _windows-path-mod:
6197db96d56Sopenharmony_ci
6207db96d56Sopenharmony_ciFinding the Python executable
6217db96d56Sopenharmony_ci-----------------------------
6227db96d56Sopenharmony_ci
6237db96d56Sopenharmony_ci.. versionchanged:: 3.5
6247db96d56Sopenharmony_ci
6257db96d56Sopenharmony_ciBesides using the automatically created start menu entry for the Python
6267db96d56Sopenharmony_ciinterpreter, you might want to start Python in the command prompt. The
6277db96d56Sopenharmony_ciinstaller has an option to set that up for you.
6287db96d56Sopenharmony_ci
6297db96d56Sopenharmony_ciOn the first page of the installer, an option labelled "Add Python to PATH"
6307db96d56Sopenharmony_cimay be selected to have the installer add the install location into the
6317db96d56Sopenharmony_ci:envvar:`PATH`.  The location of the :file:`Scripts\\` folder is also added.
6327db96d56Sopenharmony_ciThis allows you to type :command:`python` to run the interpreter, and
6337db96d56Sopenharmony_ci:command:`pip` for the package installer. Thus, you can also execute your
6347db96d56Sopenharmony_ciscripts with command line options, see :ref:`using-on-cmdline` documentation.
6357db96d56Sopenharmony_ci
6367db96d56Sopenharmony_ciIf you don't enable this option at install time, you can always re-run the
6377db96d56Sopenharmony_ciinstaller, select Modify, and enable it.  Alternatively, you can manually
6387db96d56Sopenharmony_cimodify the :envvar:`PATH` using the directions in :ref:`setting-envvars`.  You
6397db96d56Sopenharmony_cineed to set your :envvar:`PATH` environment variable to include the directory
6407db96d56Sopenharmony_ciof your Python installation, delimited by a semicolon from other entries.  An
6417db96d56Sopenharmony_ciexample variable could look like this (assuming the first two entries already
6427db96d56Sopenharmony_ciexisted)::
6437db96d56Sopenharmony_ci
6447db96d56Sopenharmony_ci    C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.9
6457db96d56Sopenharmony_ci
6467db96d56Sopenharmony_ci.. _win-utf8-mode:
6477db96d56Sopenharmony_ci
6487db96d56Sopenharmony_ciUTF-8 mode
6497db96d56Sopenharmony_ci==========
6507db96d56Sopenharmony_ci
6517db96d56Sopenharmony_ci.. versionadded:: 3.7
6527db96d56Sopenharmony_ci
6537db96d56Sopenharmony_ciWindows still uses legacy encodings for the system encoding (the ANSI Code
6547db96d56Sopenharmony_ciPage).  Python uses it for the default encoding of text files (e.g.
6557db96d56Sopenharmony_ci:func:`locale.getencoding`).
6567db96d56Sopenharmony_ci
6577db96d56Sopenharmony_ciThis may cause issues because UTF-8 is widely used on the internet
6587db96d56Sopenharmony_ciand most Unix systems, including WSL (Windows Subsystem for Linux).
6597db96d56Sopenharmony_ci
6607db96d56Sopenharmony_ciYou can use the :ref:`Python UTF-8 Mode <utf8-mode>` to change the default text
6617db96d56Sopenharmony_ciencoding to UTF-8. You can enable the :ref:`Python UTF-8 Mode <utf8-mode>` via
6627db96d56Sopenharmony_cithe ``-X utf8`` command line option, or the ``PYTHONUTF8=1`` environment
6637db96d56Sopenharmony_civariable.  See :envvar:`PYTHONUTF8` for enabling UTF-8 mode, and
6647db96d56Sopenharmony_ci:ref:`setting-envvars` for how to modify environment variables.
6657db96d56Sopenharmony_ci
6667db96d56Sopenharmony_ciWhen the :ref:`Python UTF-8 Mode <utf8-mode>` is enabled, you can still use the
6677db96d56Sopenharmony_cisystem encoding (the ANSI Code Page) via the "mbcs" codec.
6687db96d56Sopenharmony_ci
6697db96d56Sopenharmony_ciNote that adding ``PYTHONUTF8=1`` to the default environment variables
6707db96d56Sopenharmony_ciwill affect all Python 3.7+ applications on your system.
6717db96d56Sopenharmony_ciIf you have any Python 3.7+ applications which rely on the legacy
6727db96d56Sopenharmony_cisystem encoding, it is recommended to set the environment variable
6737db96d56Sopenharmony_citemporarily or use the ``-X utf8`` command line option.
6747db96d56Sopenharmony_ci
6757db96d56Sopenharmony_ci.. note::
6767db96d56Sopenharmony_ci   Even when UTF-8 mode is disabled, Python uses UTF-8 by default
6777db96d56Sopenharmony_ci   on Windows for:
6787db96d56Sopenharmony_ci
6797db96d56Sopenharmony_ci   * Console I/O including standard I/O (see :pep:`528` for details).
6807db96d56Sopenharmony_ci   * The :term:`filesystem encoding <filesystem encoding and error handler>`
6817db96d56Sopenharmony_ci     (see :pep:`529` for details).
6827db96d56Sopenharmony_ci
6837db96d56Sopenharmony_ci
6847db96d56Sopenharmony_ci.. _launcher:
6857db96d56Sopenharmony_ci
6867db96d56Sopenharmony_ciPython Launcher for Windows
6877db96d56Sopenharmony_ci===========================
6887db96d56Sopenharmony_ci
6897db96d56Sopenharmony_ci.. versionadded:: 3.3
6907db96d56Sopenharmony_ci
6917db96d56Sopenharmony_ciThe Python launcher for Windows is a utility which aids in locating and
6927db96d56Sopenharmony_ciexecuting of different Python versions.  It allows scripts (or the
6937db96d56Sopenharmony_cicommand-line) to indicate a preference for a specific Python version, and
6947db96d56Sopenharmony_ciwill locate and execute that version.
6957db96d56Sopenharmony_ci
6967db96d56Sopenharmony_ciUnlike the :envvar:`PATH` variable, the launcher will correctly select the most
6977db96d56Sopenharmony_ciappropriate version of Python. It will prefer per-user installations over
6987db96d56Sopenharmony_cisystem-wide ones, and orders by language version rather than using the most
6997db96d56Sopenharmony_cirecently installed version.
7007db96d56Sopenharmony_ci
7017db96d56Sopenharmony_ciThe launcher was originally specified in :pep:`397`.
7027db96d56Sopenharmony_ci
7037db96d56Sopenharmony_ciGetting started
7047db96d56Sopenharmony_ci---------------
7057db96d56Sopenharmony_ci
7067db96d56Sopenharmony_ciFrom the command-line
7077db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^
7087db96d56Sopenharmony_ci
7097db96d56Sopenharmony_ci.. versionchanged:: 3.6
7107db96d56Sopenharmony_ci
7117db96d56Sopenharmony_ciSystem-wide installations of Python 3.3 and later will put the launcher on your
7127db96d56Sopenharmony_ci:envvar:`PATH`. The launcher is compatible with all available versions of
7137db96d56Sopenharmony_ciPython, so it does not matter which version is installed. To check that the
7147db96d56Sopenharmony_cilauncher is available, execute the following command in Command Prompt::
7157db96d56Sopenharmony_ci
7167db96d56Sopenharmony_ci  py
7177db96d56Sopenharmony_ci
7187db96d56Sopenharmony_ciYou should find that the latest version of Python you have installed is
7197db96d56Sopenharmony_cistarted - it can be exited as normal, and any additional command-line
7207db96d56Sopenharmony_ciarguments specified will be sent directly to Python.
7217db96d56Sopenharmony_ci
7227db96d56Sopenharmony_ciIf you have multiple versions of Python installed (e.g., 3.7 and |version|) you
7237db96d56Sopenharmony_ciwill have noticed that Python |version| was started - to launch Python 3.7, try
7247db96d56Sopenharmony_cithe command::
7257db96d56Sopenharmony_ci
7267db96d56Sopenharmony_ci  py -3.7
7277db96d56Sopenharmony_ci
7287db96d56Sopenharmony_ciIf you want the latest version of Python 2 you have installed, try the
7297db96d56Sopenharmony_cicommand::
7307db96d56Sopenharmony_ci
7317db96d56Sopenharmony_ci  py -2
7327db96d56Sopenharmony_ci
7337db96d56Sopenharmony_ciIf you see the following error, you do not have the launcher installed::
7347db96d56Sopenharmony_ci
7357db96d56Sopenharmony_ci  'py' is not recognized as an internal or external command,
7367db96d56Sopenharmony_ci  operable program or batch file.
7377db96d56Sopenharmony_ci
7387db96d56Sopenharmony_ciThe command::
7397db96d56Sopenharmony_ci
7407db96d56Sopenharmony_ci  py --list
7417db96d56Sopenharmony_ci
7427db96d56Sopenharmony_cidisplays the currently installed version(s) of Python.
7437db96d56Sopenharmony_ci
7447db96d56Sopenharmony_ciThe ``-x.y`` argument is the short form of the ``-V:Company/Tag`` argument,
7457db96d56Sopenharmony_ciwhich allows selecting a specific Python runtime, including those that may have
7467db96d56Sopenharmony_cicome from somewhere other than python.org. Any runtime registered by following
7477db96d56Sopenharmony_ci:pep:`514` will be discoverable. The ``--list`` command lists all available
7487db96d56Sopenharmony_ciruntimes using the ``-V:`` format.
7497db96d56Sopenharmony_ci
7507db96d56Sopenharmony_ciWhen using the ``-V:`` argument, specifying the Company will limit selection to
7517db96d56Sopenharmony_ciruntimes from that provider, while specifying only the Tag will select from all
7527db96d56Sopenharmony_ciproviders. Note that omitting the slash implies a tag::
7537db96d56Sopenharmony_ci
7547db96d56Sopenharmony_ci  # Select any '3.*' tagged runtime
7557db96d56Sopenharmony_ci  py -V:3
7567db96d56Sopenharmony_ci
7577db96d56Sopenharmony_ci  # Select any 'PythonCore' released runtime
7587db96d56Sopenharmony_ci  py -V:PythonCore/
7597db96d56Sopenharmony_ci
7607db96d56Sopenharmony_ci  # Select PythonCore's latest Python 3 runtime
7617db96d56Sopenharmony_ci  py -V:PythonCore/3
7627db96d56Sopenharmony_ci
7637db96d56Sopenharmony_ciThe short form of the argument (``-3``) only ever selects from core Python
7647db96d56Sopenharmony_cireleases, and not other distributions. However, the longer form (``-V:3``) will
7657db96d56Sopenharmony_ciselect from any.
7667db96d56Sopenharmony_ci
7677db96d56Sopenharmony_ciThe Company is matched on the full string, case-insenitive. The Tag is matched
7687db96d56Sopenharmony_cioneither the full string, or a prefix, provided the next character is a dot or a
7697db96d56Sopenharmony_cihyphen. This allows ``-V:3.1`` to match ``3.1-32``, but not ``3.10``. Tags are
7707db96d56Sopenharmony_cisorted using numerical ordering (``3.10`` is newer than ``3.1``), but are
7717db96d56Sopenharmony_cicompared using text (``-V:3.01`` does not match ``3.1``).
7727db96d56Sopenharmony_ci
7737db96d56Sopenharmony_ci
7747db96d56Sopenharmony_ciVirtual environments
7757db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^
7767db96d56Sopenharmony_ci
7777db96d56Sopenharmony_ci.. versionadded:: 3.5
7787db96d56Sopenharmony_ci
7797db96d56Sopenharmony_ciIf the launcher is run with no explicit Python version specification, and a
7807db96d56Sopenharmony_civirtual environment (created with the standard library :mod:`venv` module or
7817db96d56Sopenharmony_cithe external ``virtualenv`` tool) active, the launcher will run the virtual
7827db96d56Sopenharmony_cienvironment's interpreter rather than the global one.  To run the global
7837db96d56Sopenharmony_ciinterpreter, either deactivate the virtual environment, or explicitly specify
7847db96d56Sopenharmony_cithe global Python version.
7857db96d56Sopenharmony_ci
7867db96d56Sopenharmony_ciFrom a script
7877db96d56Sopenharmony_ci^^^^^^^^^^^^^
7887db96d56Sopenharmony_ci
7897db96d56Sopenharmony_ciLet's create a test Python script - create a file called ``hello.py`` with the
7907db96d56Sopenharmony_cifollowing contents
7917db96d56Sopenharmony_ci
7927db96d56Sopenharmony_ci.. code-block:: python
7937db96d56Sopenharmony_ci
7947db96d56Sopenharmony_ci    #! python
7957db96d56Sopenharmony_ci    import sys
7967db96d56Sopenharmony_ci    sys.stdout.write("hello from Python %s\n" % (sys.version,))
7977db96d56Sopenharmony_ci
7987db96d56Sopenharmony_ciFrom the directory in which hello.py lives, execute the command::
7997db96d56Sopenharmony_ci
8007db96d56Sopenharmony_ci   py hello.py
8017db96d56Sopenharmony_ci
8027db96d56Sopenharmony_ciYou should notice the version number of your latest Python 2.x installation
8037db96d56Sopenharmony_ciis printed.  Now try changing the first line to be:
8047db96d56Sopenharmony_ci
8057db96d56Sopenharmony_ci.. code-block:: python
8067db96d56Sopenharmony_ci
8077db96d56Sopenharmony_ci    #! python3
8087db96d56Sopenharmony_ci
8097db96d56Sopenharmony_ciRe-executing the command should now print the latest Python 3.x information.
8107db96d56Sopenharmony_ciAs with the above command-line examples, you can specify a more explicit
8117db96d56Sopenharmony_civersion qualifier.  Assuming you have Python 3.7 installed, try changing
8127db96d56Sopenharmony_cithe first line to ``#! python3.7`` and you should find the 3.7
8137db96d56Sopenharmony_civersion information printed.
8147db96d56Sopenharmony_ci
8157db96d56Sopenharmony_ciNote that unlike interactive use, a bare "python" will use the latest
8167db96d56Sopenharmony_civersion of Python 2.x that you have installed.  This is for backward
8177db96d56Sopenharmony_cicompatibility and for compatibility with Unix, where the command ``python``
8187db96d56Sopenharmony_citypically refers to Python 2.
8197db96d56Sopenharmony_ci
8207db96d56Sopenharmony_ciFrom file associations
8217db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^
8227db96d56Sopenharmony_ci
8237db96d56Sopenharmony_ciThe launcher should have been associated with Python files (i.e. ``.py``,
8247db96d56Sopenharmony_ci``.pyw``, ``.pyc`` files) when it was installed.  This means that
8257db96d56Sopenharmony_ciwhen you double-click on one of these files from Windows explorer the launcher
8267db96d56Sopenharmony_ciwill be used, and therefore you can use the same facilities described above to
8277db96d56Sopenharmony_cihave the script specify the version which should be used.
8287db96d56Sopenharmony_ci
8297db96d56Sopenharmony_ciThe key benefit of this is that a single launcher can support multiple Python
8307db96d56Sopenharmony_civersions at the same time depending on the contents of the first line.
8317db96d56Sopenharmony_ci
8327db96d56Sopenharmony_ciShebang Lines
8337db96d56Sopenharmony_ci-------------
8347db96d56Sopenharmony_ci
8357db96d56Sopenharmony_ciIf the first line of a script file starts with ``#!``, it is known as a
8367db96d56Sopenharmony_ci"shebang" line.  Linux and other Unix like operating systems have native
8377db96d56Sopenharmony_cisupport for such lines and they are commonly used on such systems to indicate
8387db96d56Sopenharmony_cihow a script should be executed.  This launcher allows the same facilities to
8397db96d56Sopenharmony_cibe used with Python scripts on Windows and the examples above demonstrate their
8407db96d56Sopenharmony_ciuse.
8417db96d56Sopenharmony_ci
8427db96d56Sopenharmony_ciTo allow shebang lines in Python scripts to be portable between Unix and
8437db96d56Sopenharmony_ciWindows, this launcher supports a number of 'virtual' commands to specify
8447db96d56Sopenharmony_ciwhich interpreter to use.  The supported virtual commands are:
8457db96d56Sopenharmony_ci
8467db96d56Sopenharmony_ci* ``/usr/bin/env``
8477db96d56Sopenharmony_ci* ``/usr/bin/python``
8487db96d56Sopenharmony_ci* ``/usr/local/bin/python``
8497db96d56Sopenharmony_ci* ``python``
8507db96d56Sopenharmony_ci
8517db96d56Sopenharmony_ciFor example, if the first line of your script starts with
8527db96d56Sopenharmony_ci
8537db96d56Sopenharmony_ci.. code-block:: sh
8547db96d56Sopenharmony_ci
8557db96d56Sopenharmony_ci  #! /usr/bin/python
8567db96d56Sopenharmony_ci
8577db96d56Sopenharmony_ciThe default Python will be located and used.  As many Python scripts written
8587db96d56Sopenharmony_cito work on Unix will already have this line, you should find these scripts can
8597db96d56Sopenharmony_cibe used by the launcher without modification.  If you are writing a new script
8607db96d56Sopenharmony_cion Windows which you hope will be useful on Unix, you should use one of the
8617db96d56Sopenharmony_cishebang lines starting with ``/usr``.
8627db96d56Sopenharmony_ci
8637db96d56Sopenharmony_ciAny of the above virtual commands can be suffixed with an explicit version
8647db96d56Sopenharmony_ci(either just the major version, or the major and minor version).
8657db96d56Sopenharmony_ciFurthermore the 32-bit version can be requested by adding "-32" after the
8667db96d56Sopenharmony_ciminor version. I.e. ``/usr/bin/python3.7-32`` will request usage of the
8677db96d56Sopenharmony_ci32-bit python 3.7.
8687db96d56Sopenharmony_ci
8697db96d56Sopenharmony_ci.. versionadded:: 3.7
8707db96d56Sopenharmony_ci
8717db96d56Sopenharmony_ci   Beginning with python launcher 3.7 it is possible to request 64-bit version
8727db96d56Sopenharmony_ci   by the "-64" suffix. Furthermore it is possible to specify a major and
8737db96d56Sopenharmony_ci   architecture without minor (i.e. ``/usr/bin/python3-64``).
8747db96d56Sopenharmony_ci
8757db96d56Sopenharmony_ci.. versionchanged:: 3.11
8767db96d56Sopenharmony_ci
8777db96d56Sopenharmony_ci   The "-64" suffix is deprecated, and now implies "any architecture that is
8787db96d56Sopenharmony_ci   not provably i386/32-bit". To request a specific environment, use the new
8797db96d56Sopenharmony_ci   ``-V:<TAG>`` argument with the complete tag.
8807db96d56Sopenharmony_ci
8817db96d56Sopenharmony_ciThe ``/usr/bin/env`` form of shebang line has one further special property.
8827db96d56Sopenharmony_ciBefore looking for installed Python interpreters, this form will search the
8837db96d56Sopenharmony_ciexecutable :envvar:`PATH` for a Python executable matching the name provided
8847db96d56Sopenharmony_cias the first argument. This corresponds to the behaviour of the Unix ``env``
8857db96d56Sopenharmony_ciprogram, which performs a :envvar:`PATH` search.
8867db96d56Sopenharmony_ciIf an executable matching the first argument after the ``env`` command cannot
8877db96d56Sopenharmony_cibe found, but the argument starts with ``python``, it will be handled as
8887db96d56Sopenharmony_cidescribed for the other virtual commands.
8897db96d56Sopenharmony_ciThe environment variable :envvar:`PYLAUNCHER_NO_SEARCH_PATH` may be set
8907db96d56Sopenharmony_ci(to any value) to skip this search of :envvar:`PATH`.
8917db96d56Sopenharmony_ci
8927db96d56Sopenharmony_ciShebang lines that do not match any of these patterns are looked up in the
8937db96d56Sopenharmony_ci``[commands]`` section of the launcher's :ref:`.INI file <launcher-ini>`.
8947db96d56Sopenharmony_ciThis may be used to handle certain commands in a way that makes sense for your
8957db96d56Sopenharmony_cisystem. The name of the command must be a single argument (no spaces in the
8967db96d56Sopenharmony_cishebang executable), and the value substituted is the full path to the
8977db96d56Sopenharmony_ciexecutable (additional arguments specified in the .INI will be quoted as part
8987db96d56Sopenharmony_ciof the filename).
8997db96d56Sopenharmony_ci
9007db96d56Sopenharmony_ci.. code-block:: ini
9017db96d56Sopenharmony_ci
9027db96d56Sopenharmony_ci   [commands]
9037db96d56Sopenharmony_ci   /bin/xpython=C:\Program Files\XPython\python.exe
9047db96d56Sopenharmony_ci
9057db96d56Sopenharmony_ciAny commands not found in the .INI file are treated as **Windows** executable
9067db96d56Sopenharmony_cipaths that are absolute or relative to the directory containing the script file.
9077db96d56Sopenharmony_ciThis is a convenience for Windows-only scripts, such as those generated by an
9087db96d56Sopenharmony_ciinstaller, since the behavior is not compatible with Unix-style shells.
9097db96d56Sopenharmony_ciThese paths may be quoted, and may include multiple arguments, after which the
9107db96d56Sopenharmony_cipath to the script and any additional arguments will be appended.
9117db96d56Sopenharmony_ci
9127db96d56Sopenharmony_ci
9137db96d56Sopenharmony_ciArguments in shebang lines
9147db96d56Sopenharmony_ci--------------------------
9157db96d56Sopenharmony_ci
9167db96d56Sopenharmony_ciThe shebang lines can also specify additional options to be passed to the
9177db96d56Sopenharmony_ciPython interpreter.  For example, if you have a shebang line:
9187db96d56Sopenharmony_ci
9197db96d56Sopenharmony_ci.. code-block:: sh
9207db96d56Sopenharmony_ci
9217db96d56Sopenharmony_ci  #! /usr/bin/python -v
9227db96d56Sopenharmony_ci
9237db96d56Sopenharmony_ciThen Python will be started with the ``-v`` option
9247db96d56Sopenharmony_ci
9257db96d56Sopenharmony_ciCustomization
9267db96d56Sopenharmony_ci-------------
9277db96d56Sopenharmony_ci
9287db96d56Sopenharmony_ci.. _launcher-ini:
9297db96d56Sopenharmony_ci
9307db96d56Sopenharmony_ciCustomization via INI files
9317db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^
9327db96d56Sopenharmony_ci
9337db96d56Sopenharmony_ciTwo .ini files will be searched by the launcher - ``py.ini`` in the current
9347db96d56Sopenharmony_ciuser's application data directory (``%LOCALAPPDATA%`` or ``$env:LocalAppData``)
9357db96d56Sopenharmony_ciand ``py.ini`` in the same directory as the launcher. The same .ini files are
9367db96d56Sopenharmony_ciused for both the 'console' version of the launcher (i.e. py.exe) and for the
9377db96d56Sopenharmony_ci'windows' version (i.e. pyw.exe).
9387db96d56Sopenharmony_ci
9397db96d56Sopenharmony_ciCustomization specified in the "application directory" will have precedence over
9407db96d56Sopenharmony_cithe one next to the executable, so a user, who may not have write access to the
9417db96d56Sopenharmony_ci.ini file next to the launcher, can override commands in that global .ini file.
9427db96d56Sopenharmony_ci
9437db96d56Sopenharmony_ciCustomizing default Python versions
9447db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9457db96d56Sopenharmony_ci
9467db96d56Sopenharmony_ciIn some cases, a version qualifier can be included in a command to dictate
9477db96d56Sopenharmony_ciwhich version of Python will be used by the command. A version qualifier
9487db96d56Sopenharmony_cistarts with a major version number and can optionally be followed by a period
9497db96d56Sopenharmony_ci('.') and a minor version specifier. Furthermore it is possible to specify
9507db96d56Sopenharmony_ciif a 32 or 64 bit implementation shall be requested by adding "-32" or "-64".
9517db96d56Sopenharmony_ci
9527db96d56Sopenharmony_ciFor example, a shebang line of ``#!python`` has no version qualifier, while
9537db96d56Sopenharmony_ci``#!python3`` has a version qualifier which specifies only a major version.
9547db96d56Sopenharmony_ci
9557db96d56Sopenharmony_ciIf no version qualifiers are found in a command, the environment
9567db96d56Sopenharmony_civariable :envvar:`PY_PYTHON` can be set to specify the default version
9577db96d56Sopenharmony_ciqualifier. If it is not set, the default is "3". The variable can
9587db96d56Sopenharmony_cispecify any value that may be passed on the command line, such as "3",
9597db96d56Sopenharmony_ci"3.7", "3.7-32" or "3.7-64". (Note that the "-64" option is only
9607db96d56Sopenharmony_ciavailable with the launcher included with Python 3.7 or newer.)
9617db96d56Sopenharmony_ci
9627db96d56Sopenharmony_ciIf no minor version qualifiers are found, the environment variable
9637db96d56Sopenharmony_ci``PY_PYTHON{major}`` (where ``{major}`` is the current major version qualifier
9647db96d56Sopenharmony_cias determined above) can be set to specify the full version. If no such option
9657db96d56Sopenharmony_ciis found, the launcher will enumerate the installed Python versions and use
9667db96d56Sopenharmony_cithe latest minor release found for the major version, which is likely,
9677db96d56Sopenharmony_cialthough not guaranteed, to be the most recently installed version in that
9687db96d56Sopenharmony_cifamily.
9697db96d56Sopenharmony_ci
9707db96d56Sopenharmony_ciOn 64-bit Windows with both 32-bit and 64-bit implementations of the same
9717db96d56Sopenharmony_ci(major.minor) Python version installed, the 64-bit version will always be
9727db96d56Sopenharmony_cipreferred. This will be true for both 32-bit and 64-bit implementations of the
9737db96d56Sopenharmony_cilauncher - a 32-bit launcher will prefer to execute a 64-bit Python installation
9747db96d56Sopenharmony_ciof the specified version if available. This is so the behavior of the launcher
9757db96d56Sopenharmony_cican be predicted knowing only what versions are installed on the PC and
9767db96d56Sopenharmony_ciwithout regard to the order in which they were installed (i.e., without knowing
9777db96d56Sopenharmony_ciwhether a 32 or 64-bit version of Python and corresponding launcher was
9787db96d56Sopenharmony_ciinstalled last). As noted above, an optional "-32" or "-64" suffix can be
9797db96d56Sopenharmony_ciused on a version specifier to change this behaviour.
9807db96d56Sopenharmony_ci
9817db96d56Sopenharmony_ciExamples:
9827db96d56Sopenharmony_ci
9837db96d56Sopenharmony_ci* If no relevant options are set, the commands ``python`` and
9847db96d56Sopenharmony_ci  ``python2`` will use the latest Python 2.x version installed and
9857db96d56Sopenharmony_ci  the command ``python3`` will use the latest Python 3.x installed.
9867db96d56Sopenharmony_ci
9877db96d56Sopenharmony_ci* The command ``python3.7`` will not consult any
9887db96d56Sopenharmony_ci  options at all as the versions are fully specified.
9897db96d56Sopenharmony_ci
9907db96d56Sopenharmony_ci* If ``PY_PYTHON=3``, the commands ``python`` and ``python3`` will both use
9917db96d56Sopenharmony_ci  the latest installed Python 3 version.
9927db96d56Sopenharmony_ci
9937db96d56Sopenharmony_ci* If ``PY_PYTHON=3.7-32``, the command ``python`` will use the 32-bit
9947db96d56Sopenharmony_ci  implementation of 3.7 whereas the command ``python3`` will use the latest
9957db96d56Sopenharmony_ci  installed Python (PY_PYTHON was not considered at all as a major
9967db96d56Sopenharmony_ci  version was specified.)
9977db96d56Sopenharmony_ci
9987db96d56Sopenharmony_ci* If ``PY_PYTHON=3`` and ``PY_PYTHON3=3.7``, the commands
9997db96d56Sopenharmony_ci  ``python`` and ``python3`` will both use specifically 3.7
10007db96d56Sopenharmony_ci
10017db96d56Sopenharmony_ciIn addition to environment variables, the same settings can be configured
10027db96d56Sopenharmony_ciin the .INI file used by the launcher.  The section in the INI file is
10037db96d56Sopenharmony_cicalled ``[defaults]`` and the key name will be the same as the
10047db96d56Sopenharmony_cienvironment variables without the leading ``PY_`` prefix (and note that
10057db96d56Sopenharmony_cithe key names in the INI file are case insensitive.)  The contents of
10067db96d56Sopenharmony_cian environment variable will override things specified in the INI file.
10077db96d56Sopenharmony_ci
10087db96d56Sopenharmony_ciFor example:
10097db96d56Sopenharmony_ci
10107db96d56Sopenharmony_ci* Setting ``PY_PYTHON=3.7`` is equivalent to the INI file containing:
10117db96d56Sopenharmony_ci
10127db96d56Sopenharmony_ci.. code-block:: ini
10137db96d56Sopenharmony_ci
10147db96d56Sopenharmony_ci  [defaults]
10157db96d56Sopenharmony_ci  python=3.7
10167db96d56Sopenharmony_ci
10177db96d56Sopenharmony_ci* Setting ``PY_PYTHON=3`` and ``PY_PYTHON3=3.7`` is equivalent to the INI file
10187db96d56Sopenharmony_ci  containing:
10197db96d56Sopenharmony_ci
10207db96d56Sopenharmony_ci.. code-block:: ini
10217db96d56Sopenharmony_ci
10227db96d56Sopenharmony_ci  [defaults]
10237db96d56Sopenharmony_ci  python=3
10247db96d56Sopenharmony_ci  python3=3.7
10257db96d56Sopenharmony_ci
10267db96d56Sopenharmony_ciDiagnostics
10277db96d56Sopenharmony_ci-----------
10287db96d56Sopenharmony_ci
10297db96d56Sopenharmony_ciIf an environment variable :envvar:`PYLAUNCHER_DEBUG` is set (to any value), the
10307db96d56Sopenharmony_cilauncher will print diagnostic information to stderr (i.e. to the console).
10317db96d56Sopenharmony_ciWhile this information manages to be simultaneously verbose *and* terse, it
10327db96d56Sopenharmony_cishould allow you to see what versions of Python were located, why a
10337db96d56Sopenharmony_ciparticular version was chosen and the exact command-line used to execute the
10347db96d56Sopenharmony_citarget Python. It is primarily intended for testing and debugging.
10357db96d56Sopenharmony_ci
10367db96d56Sopenharmony_ciDry Run
10377db96d56Sopenharmony_ci-------
10387db96d56Sopenharmony_ci
10397db96d56Sopenharmony_ciIf an environment variable :envvar:`PYLAUNCHER_DRYRUN` is set (to any value),
10407db96d56Sopenharmony_cithe launcher will output the command it would have run, but will not actually
10417db96d56Sopenharmony_cilaunch Python. This may be useful for tools that want to use the launcher to
10427db96d56Sopenharmony_cidetect and then launch Python directly. Note that the command written to
10437db96d56Sopenharmony_cistandard output is always encoded using UTF-8, and may not render correctly in
10447db96d56Sopenharmony_cithe console.
10457db96d56Sopenharmony_ci
10467db96d56Sopenharmony_ciInstall on demand
10477db96d56Sopenharmony_ci-----------------
10487db96d56Sopenharmony_ci
10497db96d56Sopenharmony_ciIf an environment variable :envvar:`PYLAUNCHER_ALLOW_INSTALL` is set (to any
10507db96d56Sopenharmony_civalue), and the requested Python version is not installed but is available on
10517db96d56Sopenharmony_cithe Microsoft Store, the launcher will attempt to install it. This may require
10527db96d56Sopenharmony_ciuser interaction to complete, and you may need to run the command again.
10537db96d56Sopenharmony_ci
10547db96d56Sopenharmony_ciAn additional :envvar:`PYLAUNCHER_ALWAYS_INSTALL` variable causes the launcher
10557db96d56Sopenharmony_cito always try to install Python, even if it is detected. This is mainly intended
10567db96d56Sopenharmony_cifor testing (and should be used with :envvar:`PYLAUNCHER_DRYRUN`).
10577db96d56Sopenharmony_ci
10587db96d56Sopenharmony_ciReturn codes
10597db96d56Sopenharmony_ci------------
10607db96d56Sopenharmony_ci
10617db96d56Sopenharmony_ciThe following exit codes may be returned by the Python launcher. Unfortunately,
10627db96d56Sopenharmony_cithere is no way to distinguish these from the exit code of Python itself.
10637db96d56Sopenharmony_ci
10647db96d56Sopenharmony_ciThe names of codes are as used in the sources, and are only for reference. There
10657db96d56Sopenharmony_ciis no way to access or resolve them apart from reading this page. Entries are
10667db96d56Sopenharmony_cilisted in alphabetical order of names.
10677db96d56Sopenharmony_ci
10687db96d56Sopenharmony_ci+-------------------+-------+-----------------------------------------------+
10697db96d56Sopenharmony_ci| Name              | Value | Description                                   |
10707db96d56Sopenharmony_ci+===================+=======+===============================================+
10717db96d56Sopenharmony_ci| RC_BAD_VENV_CFG   | 107   | A :file:`pyvenv.cfg` was found but is corrupt.|
10727db96d56Sopenharmony_ci+-------------------+-------+-----------------------------------------------+
10737db96d56Sopenharmony_ci| RC_CREATE_PROCESS | 101   | Failed to launch Python.                      |
10747db96d56Sopenharmony_ci+-------------------+-------+-----------------------------------------------+
10757db96d56Sopenharmony_ci| RC_INSTALLING     | 111   | An install was started, but the command will  |
10767db96d56Sopenharmony_ci|                   |       | need to be re-run after it completes.         |
10777db96d56Sopenharmony_ci+-------------------+-------+-----------------------------------------------+
10787db96d56Sopenharmony_ci| RC_INTERNAL_ERROR | 109   | Unexpected error. Please report a bug.        |
10797db96d56Sopenharmony_ci+-------------------+-------+-----------------------------------------------+
10807db96d56Sopenharmony_ci| RC_NO_COMMANDLINE | 108   | Unable to obtain command line from the        |
10817db96d56Sopenharmony_ci|                   |       | operating system.                             |
10827db96d56Sopenharmony_ci+-------------------+-------+-----------------------------------------------+
10837db96d56Sopenharmony_ci| RC_NO_PYTHON      | 103   | Unable to locate the requested version.       |
10847db96d56Sopenharmony_ci+-------------------+-------+-----------------------------------------------+
10857db96d56Sopenharmony_ci| RC_NO_VENV_CFG    | 106   | A :file:`pyvenv.cfg` was required but not     |
10867db96d56Sopenharmony_ci|                   |       | found.                                        |
10877db96d56Sopenharmony_ci+-------------------+-------+-----------------------------------------------+
10887db96d56Sopenharmony_ci
10897db96d56Sopenharmony_ci
10907db96d56Sopenharmony_ci.. _windows_finding_modules:
10917db96d56Sopenharmony_ci
10927db96d56Sopenharmony_ciFinding modules
10937db96d56Sopenharmony_ci===============
10947db96d56Sopenharmony_ci
10957db96d56Sopenharmony_ciThese notes supplement the description at :ref:`sys-path-init` with
10967db96d56Sopenharmony_cidetailed Windows notes.
10977db96d56Sopenharmony_ci
10987db96d56Sopenharmony_ciWhen no ``._pth`` file is found, this is how :data:`sys.path` is populated on
10997db96d56Sopenharmony_ciWindows:
11007db96d56Sopenharmony_ci
11017db96d56Sopenharmony_ci* An empty entry is added at the start, which corresponds to the current
11027db96d56Sopenharmony_ci  directory.
11037db96d56Sopenharmony_ci
11047db96d56Sopenharmony_ci* If the environment variable :envvar:`PYTHONPATH` exists, as described in
11057db96d56Sopenharmony_ci  :ref:`using-on-envvars`, its entries are added next.  Note that on Windows,
11067db96d56Sopenharmony_ci  paths in this variable must be separated by semicolons, to distinguish them
11077db96d56Sopenharmony_ci  from the colon used in drive identifiers (``C:\`` etc.).
11087db96d56Sopenharmony_ci
11097db96d56Sopenharmony_ci* Additional "application paths" can be added in the registry as subkeys of
11107db96d56Sopenharmony_ci  :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the
11117db96d56Sopenharmony_ci  ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives.  Subkeys which have
11127db96d56Sopenharmony_ci  semicolon-delimited path strings as their default value will cause each path
11137db96d56Sopenharmony_ci  to be added to :data:`sys.path`.  (Note that all known installers only use
11147db96d56Sopenharmony_ci  HKLM, so HKCU is typically empty.)
11157db96d56Sopenharmony_ci
11167db96d56Sopenharmony_ci* If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as
11177db96d56Sopenharmony_ci  "Python Home".  Otherwise, the path of the main Python executable is used to
11187db96d56Sopenharmony_ci  locate a "landmark file" (either ``Lib\os.py`` or ``pythonXY.zip``) to deduce
11197db96d56Sopenharmony_ci  the "Python Home".  If a Python home is found, the relevant sub-directories
11207db96d56Sopenharmony_ci  added to :data:`sys.path` (``Lib``, ``plat-win``, etc) are based on that
11217db96d56Sopenharmony_ci  folder.  Otherwise, the core Python path is constructed from the PythonPath
11227db96d56Sopenharmony_ci  stored in the registry.
11237db96d56Sopenharmony_ci
11247db96d56Sopenharmony_ci* If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in
11257db96d56Sopenharmony_ci  the environment, and no registry entries can be found, a default path with
11267db96d56Sopenharmony_ci  relative entries is used (e.g. ``.\Lib;.\plat-win``, etc).
11277db96d56Sopenharmony_ci
11287db96d56Sopenharmony_ciIf a ``pyvenv.cfg`` file is found alongside the main executable or in the
11297db96d56Sopenharmony_cidirectory one level above the executable, the following variations apply:
11307db96d56Sopenharmony_ci
11317db96d56Sopenharmony_ci* If ``home`` is an absolute path and :envvar:`PYTHONHOME` is not set, this
11327db96d56Sopenharmony_ci  path is used instead of the path to the main executable when deducing the
11337db96d56Sopenharmony_ci  home location.
11347db96d56Sopenharmony_ci
11357db96d56Sopenharmony_ciThe end result of all this is:
11367db96d56Sopenharmony_ci
11377db96d56Sopenharmony_ci* When running :file:`python.exe`, or any other .exe in the main Python
11387db96d56Sopenharmony_ci  directory (either an installed version, or directly from the PCbuild
11397db96d56Sopenharmony_ci  directory), the core path is deduced, and the core paths in the registry are
11407db96d56Sopenharmony_ci  ignored.  Other "application paths" in the registry are always read.
11417db96d56Sopenharmony_ci
11427db96d56Sopenharmony_ci* When Python is hosted in another .exe (different directory, embedded via COM,
11437db96d56Sopenharmony_ci  etc), the "Python Home" will not be deduced, so the core path from the
11447db96d56Sopenharmony_ci  registry is used.  Other "application paths" in the registry are always read.
11457db96d56Sopenharmony_ci
11467db96d56Sopenharmony_ci* If Python can't find its home and there are no registry value (frozen .exe,
11477db96d56Sopenharmony_ci  some very strange installation setup) you get a path with some default, but
11487db96d56Sopenharmony_ci  relative, paths.
11497db96d56Sopenharmony_ci
11507db96d56Sopenharmony_ciFor those who want to bundle Python into their application or distribution, the
11517db96d56Sopenharmony_cifollowing advice will prevent conflicts with other installations:
11527db96d56Sopenharmony_ci
11537db96d56Sopenharmony_ci* Include a ``._pth`` file alongside your executable containing the
11547db96d56Sopenharmony_ci  directories to include. This will ignore paths listed in the registry and
11557db96d56Sopenharmony_ci  environment variables, and also ignore :mod:`site` unless ``import site`` is
11567db96d56Sopenharmony_ci  listed.
11577db96d56Sopenharmony_ci
11587db96d56Sopenharmony_ci* If you are loading :file:`python3.dll` or :file:`python37.dll` in your own
11597db96d56Sopenharmony_ci  executable, explicitly call :c:func:`Py_SetPath` or (at least)
11607db96d56Sopenharmony_ci  :c:func:`Py_SetProgramName` before :c:func:`Py_Initialize`.
11617db96d56Sopenharmony_ci
11627db96d56Sopenharmony_ci* Clear and/or overwrite :envvar:`PYTHONPATH` and set :envvar:`PYTHONHOME`
11637db96d56Sopenharmony_ci  before launching :file:`python.exe` from your application.
11647db96d56Sopenharmony_ci
11657db96d56Sopenharmony_ci* If you cannot use the previous suggestions (for example, you are a
11667db96d56Sopenharmony_ci  distribution that allows people to run :file:`python.exe` directly), ensure
11677db96d56Sopenharmony_ci  that the landmark file (:file:`Lib\\os.py`) exists in your install directory.
11687db96d56Sopenharmony_ci  (Note that it will not be detected inside a ZIP file, but a correctly named
11697db96d56Sopenharmony_ci  ZIP file will be detected instead.)
11707db96d56Sopenharmony_ci
11717db96d56Sopenharmony_ciThese will ensure that the files in a system-wide installation will not take
11727db96d56Sopenharmony_ciprecedence over the copy of the standard library bundled with your application.
11737db96d56Sopenharmony_ciOtherwise, your users may experience problems using your application. Note that
11747db96d56Sopenharmony_cithe first suggestion is the best, as the others may still be susceptible to
11757db96d56Sopenharmony_cinon-standard paths in the registry and user site-packages.
11767db96d56Sopenharmony_ci
11777db96d56Sopenharmony_ci.. versionchanged::
11787db96d56Sopenharmony_ci   3.6
11797db96d56Sopenharmony_ci
11807db96d56Sopenharmony_ci      * Adds ``._pth`` file support and removes ``applocal`` option from
11817db96d56Sopenharmony_ci        ``pyvenv.cfg``.
11827db96d56Sopenharmony_ci      * Adds ``pythonXX.zip`` as a potential landmark when directly adjacent
11837db96d56Sopenharmony_ci        to the executable.
11847db96d56Sopenharmony_ci
11857db96d56Sopenharmony_ci.. deprecated::
11867db96d56Sopenharmony_ci   3.6
11877db96d56Sopenharmony_ci
11887db96d56Sopenharmony_ci      Modules specified in the registry under ``Modules`` (not ``PythonPath``)
11897db96d56Sopenharmony_ci      may be imported by :class:`importlib.machinery.WindowsRegistryFinder`.
11907db96d56Sopenharmony_ci      This finder is enabled on Windows in 3.6.0 and earlier, but may need to
11917db96d56Sopenharmony_ci      be explicitly added to :attr:`sys.meta_path` in the future.
11927db96d56Sopenharmony_ci
11937db96d56Sopenharmony_ciAdditional modules
11947db96d56Sopenharmony_ci==================
11957db96d56Sopenharmony_ci
11967db96d56Sopenharmony_ciEven though Python aims to be portable among all platforms, there are features
11977db96d56Sopenharmony_cithat are unique to Windows.  A couple of modules, both in the standard library
11987db96d56Sopenharmony_ciand external, and snippets exist to use these features.
11997db96d56Sopenharmony_ci
12007db96d56Sopenharmony_ciThe Windows-specific standard modules are documented in
12017db96d56Sopenharmony_ci:ref:`mswin-specific-services`.
12027db96d56Sopenharmony_ci
12037db96d56Sopenharmony_ciPyWin32
12047db96d56Sopenharmony_ci-------
12057db96d56Sopenharmony_ci
12067db96d56Sopenharmony_ciThe `PyWin32 <https://pypi.org/project/pywin32>`_ module by Mark Hammond
12077db96d56Sopenharmony_ciis a collection of modules for advanced Windows-specific support.  This includes
12087db96d56Sopenharmony_ciutilities for:
12097db96d56Sopenharmony_ci
12107db96d56Sopenharmony_ci* `Component Object Model
12117db96d56Sopenharmony_ci  <https://docs.microsoft.com/en-us/windows/win32/com/component-object-model--com--portal>`_
12127db96d56Sopenharmony_ci  (COM)
12137db96d56Sopenharmony_ci* Win32 API calls
12147db96d56Sopenharmony_ci* Registry
12157db96d56Sopenharmony_ci* Event log
12167db96d56Sopenharmony_ci* `Microsoft Foundation Classes
12177db96d56Sopenharmony_ci  <https://docs.microsoft.com/en-us/cpp/mfc/mfc-desktop-applications>`_
12187db96d56Sopenharmony_ci  (MFC) user interfaces
12197db96d56Sopenharmony_ci
12207db96d56Sopenharmony_ci`PythonWin <https://web.archive.org/web/20060524042422/
12217db96d56Sopenharmony_cihttps://www.python.org/windows/pythonwin/>`_ is a sample MFC application
12227db96d56Sopenharmony_cishipped with PyWin32.  It is an embeddable IDE with a built-in debugger.
12237db96d56Sopenharmony_ci
12247db96d56Sopenharmony_ci.. seealso::
12257db96d56Sopenharmony_ci
12267db96d56Sopenharmony_ci   `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_
12277db96d56Sopenharmony_ci      by Tim Golden
12287db96d56Sopenharmony_ci
12297db96d56Sopenharmony_ci   `Python and COM <https://www.boddie.org.uk/python/COM.html>`_
12307db96d56Sopenharmony_ci      by David and Paul Boddie
12317db96d56Sopenharmony_ci
12327db96d56Sopenharmony_ci
12337db96d56Sopenharmony_cicx_Freeze
12347db96d56Sopenharmony_ci---------
12357db96d56Sopenharmony_ci
12367db96d56Sopenharmony_ci`cx_Freeze <https://cx-freeze.readthedocs.io/en/latest/>`_ is a :mod:`distutils`
12377db96d56Sopenharmony_ciextension (see :ref:`extending-distutils`) which wraps Python scripts into
12387db96d56Sopenharmony_ciexecutable Windows programs (:file:`{*}.exe` files).  When you have done this,
12397db96d56Sopenharmony_ciyou can distribute your application without requiring your users to install
12407db96d56Sopenharmony_ciPython.
12417db96d56Sopenharmony_ci
12427db96d56Sopenharmony_ci
12437db96d56Sopenharmony_ciCompiling Python on Windows
12447db96d56Sopenharmony_ci===========================
12457db96d56Sopenharmony_ci
12467db96d56Sopenharmony_ciIf you want to compile CPython yourself, first thing you should do is get the
12477db96d56Sopenharmony_ci`source <https://www.python.org/downloads/source/>`_. You can download either the
12487db96d56Sopenharmony_cilatest release's source or just grab a fresh `checkout
12497db96d56Sopenharmony_ci<https://devguide.python.org/setup/#get-the-source-code>`_.
12507db96d56Sopenharmony_ci
12517db96d56Sopenharmony_ciThe source tree contains a build solution and project files for Microsoft
12527db96d56Sopenharmony_ciVisual Studio, which is the compiler used to build the official Python
12537db96d56Sopenharmony_cireleases. These files are in the :file:`PCbuild` directory.
12547db96d56Sopenharmony_ci
12557db96d56Sopenharmony_ciCheck :file:`PCbuild/readme.txt` for general information on the build process.
12567db96d56Sopenharmony_ci
12577db96d56Sopenharmony_ciFor extension modules, consult :ref:`building-on-windows`.
12587db96d56Sopenharmony_ci
12597db96d56Sopenharmony_ci
12607db96d56Sopenharmony_ciOther Platforms
12617db96d56Sopenharmony_ci===============
12627db96d56Sopenharmony_ci
12637db96d56Sopenharmony_ciWith ongoing development of Python, some platforms that used to be supported
12647db96d56Sopenharmony_ciearlier are no longer supported (due to the lack of users or developers).
12657db96d56Sopenharmony_ciCheck :pep:`11` for details on all unsupported platforms.
12667db96d56Sopenharmony_ci
12677db96d56Sopenharmony_ci* `Windows CE <https://pythonce.sourceforge.net/>`_ is
12687db96d56Sopenharmony_ci  `no longer supported <https://github.com/python/cpython/issues/71542>`__
12697db96d56Sopenharmony_ci  since Python 3 (if it ever was).
12707db96d56Sopenharmony_ci* The `Cygwin <https://cygwin.com/>`_ installer offers to install the
12717db96d56Sopenharmony_ci  `Python interpreter <https://cygwin.com/packages/summary/python3.html>`__
12727db96d56Sopenharmony_ci  as well
12737db96d56Sopenharmony_ci
12747db96d56Sopenharmony_ciSee `Python for Windows <https://www.python.org/downloads/windows/>`_
12757db96d56Sopenharmony_cifor detailed information about platforms with pre-compiled installers.
1276