blob: 97e9cdfeb09396c79a10b485b76ec842365dae1f [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: none
Christian Heimesd8654cf2007-12-02 15:22:16 +00002
3.. _using-on-windows:
4
5*************************
6 Using Python on Windows
7*************************
8
9.. sectionauthor:: Robert Lehmann <lehmannro@gmail.com>
Steve Dowerbb240872015-02-05 22:08:48 -080010.. sectionauthor:: Steve Dower <steve.dower@microsoft.com>
Christian Heimesd8654cf2007-12-02 15:22:16 +000011
12This document aims to give an overview of Windows-specific behaviour you should
13know about when using Python on Microsoft Windows.
14
Steve Dowerbb240872015-02-05 22:08:48 -080015Unlike most Unix systems and services, Windows does not include a system
16supported installation of Python. To make Python available, the CPython team
Christian Heimesd8654cf2007-12-02 15:22:16 +000017has compiled Windows installers (MSI packages) with every `release
Steve Dowerbb240872015-02-05 22:08:48 -080018<https://www.python.org/download/releases/>`_ for many years. These installers
Steve Dower9a578d92015-05-02 22:28:58 -070019are primarily intended to add a per-user installation of Python, with the
20core interpreter and library being used by a single user. The installer is also
21able to install for all users of a single machine, and a separate ZIP file is
22available for application-local distributions.
Steve Dowerbb240872015-02-05 22:08:48 -080023
Brett Cannon85622e42015-12-27 12:08:37 -080024As specified in :pep:`11`, a Python release only supports a Windows platform
25while Microsoft considers the platform under extended support. This means that
Steve Dowerf333fd82016-09-09 09:40:06 -070026Python |version| supports Windows Vista and newer. If you require Windows XP
27support then please install Python 3.4.
Brett Cannon85622e42015-12-27 12:08:37 -080028
Steve Dower0cd63912018-12-10 18:52:57 -080029There are a number of different installers available for Windows, each with
30certain benefits and downsides.
31
32:ref:`windows-full` contains all components and is the best option for
33developers using Python for any kind of project.
34
35:ref:`windows-store` is a simple installation of Python that is suitable for
36running scripts and packages, and using IDLE or other development environments.
37It requires Windows 10, but can be safely installed without corrupting other
38programs. It also provides many convenient commands for launching Python and
39its tools.
40
41:ref:`windows-nuget` are lightweight installations intended for continuous
42integration systems. It can be used to build Python packages or run scripts,
43but is not updateable and has no user interface tools.
44
45:ref:`windows-embeddable` is a minimal package of Python suitable for
46embedding into a larger application.
47
48
49.. _windows-full:
50
51The full installer
52==================
53
54Installation steps
Steve Dowerbb240872015-02-05 22:08:48 -080055------------------
56
Steve Dowerf333fd82016-09-09 09:40:06 -070057Four Python |version| installers are available for download - two each for the
5832-bit and 64-bit versions of the interpreter. The *web installer* is a small
59initial download, and it will automatically download the required components as
Steve Dowerbb240872015-02-05 22:08:48 -080060necessary. The *offline installer* includes the components necessary for a
61default installation and only requires an internet connection for optional
62features. See :ref:`install-layout-option` for other ways to avoid downloading
63during installation.
64
Steve Dower9a578d92015-05-02 22:28:58 -070065After starting the installer, one of two options may be selected:
Steve Dowerbb240872015-02-05 22:08:48 -080066
67.. image:: win_installer.png
68
Steve Dower9a578d92015-05-02 22:28:58 -070069If you select "Install Now":
Steve Dowerbb240872015-02-05 22:08:48 -080070
Steve Dower9a578d92015-05-02 22:28:58 -070071* You will *not* need to be an administrator (unless a system update for the
Steve Dowereb3c16d2015-08-08 09:09:01 -070072 C Runtime Library is required or you install the :ref:`launcher` for all
73 users)
Steve Dowerbb240872015-02-05 22:08:48 -080074* Python will be installed into your user directory
Steve Dowereb3c16d2015-08-08 09:09:01 -070075* The :ref:`launcher` will be installed according to the option at the bottom
Zachary Ware87ec85f2016-07-28 19:00:53 -050076 of the first page
Steve Dowerbb240872015-02-05 22:08:48 -080077* The standard library, test suite, launcher and pip will be installed
Steve Dower9a578d92015-05-02 22:28:58 -070078* If selected, the install directory will be added to your :envvar:`PATH`
79* Shortcuts will only be visible for the current user
Steve Dowerbb240872015-02-05 22:08:48 -080080
81Selecting "Customize installation" will allow you to select the features to
82install, the installation location and other options or post-install actions.
83To install debugging symbols or binaries, you will need to use this option.
84
Steve Dower9a578d92015-05-02 22:28:58 -070085To perform an all-users installation, you should select "Customize
86installation". In this case:
87
88* You may be required to provide administrative credentials or approval
89* Python will be installed into the Program Files directory
90* The :ref:`launcher` will be installed into the Windows directory
91* Optional features may be selected during installation
Steve Dowereb3c16d2015-08-08 09:09:01 -070092* The standard library can be pre-compiled to bytecode
Steve Dower9a578d92015-05-02 22:28:58 -070093* If selected, the install directory will be added to the system :envvar:`PATH`
94* Shortcuts are available for all users
95
Steve Dower19ab0fd2016-09-06 20:40:11 -070096.. _max-path:
97
98Removing the MAX_PATH Limitation
99--------------------------------
100
101Windows historically has limited path lengths to 260 characters. This meant that
102paths longer than this would not resolve and errors would result.
103
104In the latest versions of Windows, this limitation can be expanded to
105approximately 32,000 characters. Your administrator will need to activate the
106"Enable Win32 long paths" group policy, or set the registry value
107``HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled``
108to ``1``.
109
110This allows the :func:`open` function, the :mod:`os` module and most other
111path functionality to accept and return paths longer than 260 characters when
112using strings. (Use of bytes as paths is deprecated on Windows, and this feature
113is not available when using bytes.)
114
115After changing the above option, no further configuration is required.
116
117.. versionchanged:: 3.6
118
119 Support for long paths was enabled in Python.
120
Steve Dowerbb240872015-02-05 22:08:48 -0800121.. _install-quiet-option:
122
123Installing Without UI
124---------------------
125
126All of the options available in the installer UI can also be specified from the
127command line, allowing scripted installers to replicate an installation on many
128machines without user interaction. These options may also be set without
129suppressing the UI in order to change some of the defaults.
130
131To completely hide the installer UI and install Python silently, pass the
Steve Dowercb0afc82015-09-11 08:47:42 -0700132``/quiet`` option. To skip past the user interaction but still display
133progress and errors, pass the ``/passive`` option. The ``/uninstall``
Steve Dowerbb240872015-02-05 22:08:48 -0800134option may be passed to immediately begin removing Python - no prompt will be
135displayed.
136
137All other options are passed as ``name=value``, where the value is usually
138``0`` to disable a feature, ``1`` to enable a feature, or a path. The full list
139of available options is shown below.
140
141+---------------------------+--------------------------------------+--------------------------+
142| Name | Description | Default |
143+===========================+======================================+==========================+
Steve Dower9b608e52015-09-13 14:39:26 -0700144| InstallAllUsers | Perform a system-wide installation. | 0 |
Steve Dowerbb240872015-02-05 22:08:48 -0800145+---------------------------+--------------------------------------+--------------------------+
146| TargetDir | The installation directory | Selected based on |
147| | | InstallAllUsers |
148+---------------------------+--------------------------------------+--------------------------+
149| DefaultAllUsersTargetDir | The default installation directory | :file:`%ProgramFiles%\\\ |
150| | for all-user installs | Python X.Y` or :file:`\ |
151| | | %ProgramFiles(x86)%\\\ |
152| | | Python X.Y` |
153+---------------------------+--------------------------------------+--------------------------+
154| DefaultJustForMeTargetDir | The default install directory for | :file:`%LocalAppData%\\\ |
155| | just-for-me installs | Programs\\PythonXY` or |
156| | | :file:`%LocalAppData%\\\ |
mrh19971e2ad6c2019-04-13 00:26:47 +0200157| | | Programs\\PythonXY-32` or|
158| | | :file:`%LocalAppData%\\\ |
159| | | Programs\\PythonXY-64` |
Steve Dowerbb240872015-02-05 22:08:48 -0800160+---------------------------+--------------------------------------+--------------------------+
161| DefaultCustomTargetDir | The default custom install directory | (empty) |
162| | displayed in the UI | |
163+---------------------------+--------------------------------------+--------------------------+
164| AssociateFiles | Create file associations if the | 1 |
165| | launcher is also installed. | |
166+---------------------------+--------------------------------------+--------------------------+
167| CompileAll | Compile all ``.py`` files to | 0 |
Brett Cannonf299abd2015-04-13 14:21:02 -0400168| | ``.pyc``. | |
Steve Dowerbb240872015-02-05 22:08:48 -0800169+---------------------------+--------------------------------------+--------------------------+
170| PrependPath | Add install and Scripts directories | 0 |
Segev Finer8e7e8bd2018-07-30 20:11:30 +0300171| | to :envvar:`PATH` and ``.PY`` to | |
Steve Dowerbb240872015-02-05 22:08:48 -0800172| | :envvar:`PATHEXT` | |
173+---------------------------+--------------------------------------+--------------------------+
Steve Dower1d4880d2015-05-03 14:54:32 -0700174| Shortcuts | Create shortcuts for the interpreter,| 1 |
175| | documentation and IDLE if installed. | |
176+---------------------------+--------------------------------------+--------------------------+
Steve Dowerbb240872015-02-05 22:08:48 -0800177| Include_doc | Install Python manual | 1 |
178+---------------------------+--------------------------------------+--------------------------+
179| Include_debug | Install debug binaries | 0 |
180+---------------------------+--------------------------------------+--------------------------+
181| Include_dev | Install developer headers and | 1 |
182| | libraries | |
183+---------------------------+--------------------------------------+--------------------------+
184| Include_exe | Install :file:`python.exe` and | 1 |
185| | related files | |
186+---------------------------+--------------------------------------+--------------------------+
187| Include_launcher | Install :ref:`launcher`. | 1 |
188+---------------------------+--------------------------------------+--------------------------+
Steve Dower2237bdc2015-07-16 16:33:55 -0700189| InstallLauncherAllUsers | Installs :ref:`launcher` for all | 1 |
190| | users. | |
191+---------------------------+--------------------------------------+--------------------------+
Steve Dowerbb240872015-02-05 22:08:48 -0800192| Include_lib | Install standard library and | 1 |
193| | extension modules | |
194+---------------------------+--------------------------------------+--------------------------+
195| Include_pip | Install bundled pip and setuptools | 1 |
196+---------------------------+--------------------------------------+--------------------------+
197| Include_symbols | Install debugging symbols (`*`.pdb) | 0 |
198+---------------------------+--------------------------------------+--------------------------+
199| Include_tcltk | Install Tcl/Tk support and IDLE | 1 |
200+---------------------------+--------------------------------------+--------------------------+
201| Include_test | Install standard library test suite | 1 |
202+---------------------------+--------------------------------------+--------------------------+
203| Include_tools | Install utility scripts | 1 |
204+---------------------------+--------------------------------------+--------------------------+
Steve Dowera3d03ec2015-07-18 09:27:52 -0700205| LauncherOnly | Only installs the launcher. This | 0 |
206| | will override most other options. | |
207+---------------------------+--------------------------------------+--------------------------+
Steve Dowerbb240872015-02-05 22:08:48 -0800208| SimpleInstall | Disable most install UI | 0 |
209+---------------------------+--------------------------------------+--------------------------+
Steve Dower2237bdc2015-07-16 16:33:55 -0700210| SimpleInstallDescription | A custom message to display when the | (empty) |
211| | simplified install UI is used. | |
212+---------------------------+--------------------------------------+--------------------------+
Steve Dowerbb240872015-02-05 22:08:48 -0800213
214For example, to silently install a default, system-wide Python installation,
215you could use the following command (from an elevated command prompt)::
216
Steve Dower7a177c02019-06-26 08:55:57 -0700217 python-3.9.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
Steve Dowerbb240872015-02-05 22:08:48 -0800218
219To allow users to easily install a personal copy of Python without the test
Steve Dower2237bdc2015-07-16 16:33:55 -0700220suite, you could provide a shortcut with the following command. This will
221display a simplified initial page and disallow customization::
Steve Dowerbb240872015-02-05 22:08:48 -0800222
Steve Dower7a177c02019-06-26 08:55:57 -0700223 python-3.9.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
Steve Dower2237bdc2015-07-16 16:33:55 -0700224 SimpleInstall=1 SimpleInstallDescription="Just for me, no test suite."
Steve Dowerbb240872015-02-05 22:08:48 -0800225
226(Note that omitting the launcher also omits file associations, and is only
227recommended for per-user installs when there is also a system-wide installation
228that included the launcher.)
229
Steve Dower2434aa22015-07-18 09:28:19 -0700230The options listed above can also be provided in a file named ``unattend.xml``
231alongside the executable. This file specifies a list of options and values.
232When a value is provided as an attribute, it will be converted to a number if
233possible. Values provided as element text are always left as strings. This
Segev Finer8e7e8bd2018-07-30 20:11:30 +0300234example file sets the same options as the previous example:
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300235
236.. code-block:: xml
Steve Dower2434aa22015-07-18 09:28:19 -0700237
238 <Options>
239 <Option Name="InstallAllUsers" Value="no" />
240 <Option Name="Include_launcher" Value="0" />
241 <Option Name="Include_test" Value="no" />
242 <Option Name="SimpleInstall" Value="yes" />
243 <Option Name="SimpleInstallDescription">Just for me, no test suite</Option>
244 </Options>
245
Steve Dowerbb240872015-02-05 22:08:48 -0800246.. _install-layout-option:
247
248Installing Without Downloading
249------------------------------
250
251As some features of Python are not included in the initial installer download,
252selecting those features may require an internet connection. To avoid this
253need, all possible components may be downloaded on-demand to create a complete
254*layout* that will no longer require an internet connection regardless of the
255selected features. Note that this download may be bigger than required, but
256where a large number of installations are going to be performed it is very
257useful to have a locally cached copy.
258
259Execute the following command from Command Prompt to download all possible
Steve Dower7a177c02019-06-26 08:55:57 -0700260required files. Remember to substitute ``python-3.9.0.exe`` for the actual
Steve Dowerbb240872015-02-05 22:08:48 -0800261name of your installer, and to create layouts in their own directories to
262avoid collisions between files with the same name.
263
264::
265
Steve Dower7a177c02019-06-26 08:55:57 -0700266 python-3.9.0.exe /layout [optional target directory]
Steve Dowerbb240872015-02-05 22:08:48 -0800267
268You may also specify the ``/quiet`` option to hide the progress display.
269
Steve Dowere248f682016-01-16 11:58:30 -0800270Modifying an install
271--------------------
272
273Once Python has been installed, you can add or remove features through the
274Programs and Features tool that is part of Windows. Select the Python entry and
275choose "Uninstall/Change" to open the installer in maintenance mode.
276
277"Modify" allows you to add or remove features by modifying the checkboxes -
278unchanged checkboxes will not install or remove anything. Some options cannot be
279changed in this mode, such as the install directory; to modify these, you will
280need to remove and then reinstall Python completely.
281
282"Repair" will verify all the files that should be installed using the current
283settings and replace any that have been removed or modified.
284
285"Uninstall" will remove Python entirely, with the exception of the
286:ref:`launcher`, which has its own entry in Programs and Features.
Steve Dowerbb240872015-02-05 22:08:48 -0800287
Christian Heimesd8654cf2007-12-02 15:22:16 +0000288
Steve Dower0cd63912018-12-10 18:52:57 -0800289.. _windows-store:
Christian Heimesd8654cf2007-12-02 15:22:16 +0000290
Steve Dower0cd63912018-12-10 18:52:57 -0800291The Microsoft Store package
292===========================
Christian Heimesd8654cf2007-12-02 15:22:16 +0000293
Steve Dower0cd63912018-12-10 18:52:57 -0800294.. versionadded:: 3.7.2
Christian Heimesd8654cf2007-12-02 15:22:16 +0000295
Steve Dower0cd63912018-12-10 18:52:57 -0800296The Microsoft Store package is an easily installable Python interpreter that
297is intended mainly for interactive use, for example, by students.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000298
Steve Dower0cd63912018-12-10 18:52:57 -0800299To install the package, ensure you have the latest Windows 10 updates and
300search the Microsoft Store app for "Python |version|". Ensure that the app
301you select is published by the Python Software Foundation, and install it.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000302
Steve Dower0cd63912018-12-10 18:52:57 -0800303.. warning::
304 Python will always be available for free on the Microsoft Store. If you
305 are asked to pay for it, you have not selected the correct package.
306
307After installation, Python may be launched by finding it in Start.
308Alternatively, it will be available from any Command Prompt or PowerShell
309session by typing ``python``. Further, pip and IDLE may be used by typing
310``pip`` or ``idle``. IDLE can also be found in Start.
311
312All three commands are also available with version number suffixes, for
313example, as ``python3.exe`` and ``python3.x.exe`` as well as
314``python.exe`` (where ``3.x`` is the specific version you want to launch,
Steve Dowercf9360e2019-08-19 10:07:25 -0700315such as |version|). Open "Manage App Execution Aliases" through Start to
316select which version of Python is associated with each command. It is
317recommended to make sure that ``pip`` and ``idle`` are consistent with
318whichever version of ``python`` is selected.
Steve Dower0cd63912018-12-10 18:52:57 -0800319
320Virtual environments can be created with ``python -m venv`` and activated
321and used as normal.
322
323If you have installed another version of Python and added it to your
324``PATH`` variable, it will be available as ``python.exe`` rather than the
325one from the Microsoft Store. To access the new installation, use
326``python3.exe`` or ``python3.x.exe``.
327
Steve Dower4d202282019-10-14 15:45:43 -0700328The ``py.exe`` launcher will detect this Python installation, but will prefer
329installations from the traditional installer.
330
Steve Dower0cd63912018-12-10 18:52:57 -0800331To remove Python, open Settings and use Apps and Features, or else find
332Python in Start and right-click to select Uninstall. Uninstalling will
333remove all packages you installed directly into this Python installation, but
334will not remove any virtual environments
335
336Known Issues
337------------
338
Steve Dower0cd63912018-12-10 18:52:57 -0800339Because of restrictions on Microsoft Store apps, Python scripts may not have
340full write access to shared locations such as ``TEMP`` and the registry.
341Instead, it will write to a private copy. If your scripts must modify the
342shared locations, you will need to install the full installer.
343
344
345.. _windows-nuget:
346
347The nuget.org packages
348======================
349
350.. versionadded:: 3.5.2
351
352The nuget.org package is a reduced size Python environment intended for use on
353continuous integration and build systems that do not have a system-wide
354install of Python. While nuget is "the package manager for .NET", it also works
355perfectly fine for packages containing build-time tools.
356
357Visit `nuget.org <https://www.nuget.org/>`_ for the most up-to-date information
358on using nuget. What follows is a summary that is sufficient for Python
359developers.
360
361The ``nuget.exe`` command line tool may be downloaded directly from
362``https://aka.ms/nugetclidl``, for example, using curl or PowerShell. With the
363tool, the latest version of Python for 64-bit or 32-bit machines is installed
364using::
365
366 nuget.exe install python -ExcludeVersion -OutputDirectory .
367 nuget.exe install pythonx86 -ExcludeVersion -OutputDirectory .
368
369To select a particular version, add a ``-Version 3.x.y``. The output directory
370may be changed from ``.``, and the package will be installed into a
371subdirectory. By default, the subdirectory is named the same as the package,
372and without the ``-ExcludeVersion`` option this name will include the specific
373version installed. Inside the subdirectory is a ``tools`` directory that
374contains the Python installation::
375
376 # Without -ExcludeVersion
377 > .\python.3.5.2\tools\python.exe -V
378 Python 3.5.2
379
380 # With -ExcludeVersion
381 > .\python\tools\python.exe -V
382 Python 3.5.2
383
384In general, nuget packages are not upgradeable, and newer versions should be
385installed side-by-side and referenced using the full path. Alternatively,
386delete the package directory manually and install it again. Many CI systems
387will do this automatically if they do not preserve files between builds.
388
389Alongside the ``tools`` directory is a ``build\native`` directory. This
390contains a MSBuild properties file ``python.props`` that can be used in a
391C++ project to reference the Python install. Including the settings will
392automatically use the headers and import libraries in your build.
393
394The package information pages on nuget.org are
395`www.nuget.org/packages/python <https://www.nuget.org/packages/python>`_
396for the 64-bit version and `www.nuget.org/packages/pythonx86
397<https://www.nuget.org/packages/pythonx86>`_ for the 32-bit version.
398
399
400.. _windows-embeddable:
401
402The embeddable package
403======================
404
405.. versionadded:: 3.5
406
407The embedded distribution is a ZIP file containing a minimal Python environment.
408It is intended for acting as part of another application, rather than being
409directly accessed by end-users.
410
411When extracted, the embedded distribution is (almost) fully isolated from the
412user's system, including environment variables, system registry settings, and
413installed packages. The standard library is included as pre-compiled and
414optimized ``.pyc`` files in a ZIP, and ``python3.dll``, ``python37.dll``,
415``python.exe`` and ``pythonw.exe`` are all provided. Tcl/tk (including all
416dependants, such as Idle), pip and the Python documentation are not included.
417
418.. note::
419
420 The embedded distribution does not include the `Microsoft C Runtime
421 <https://www.microsoft.com/en-us/download/details.aspx?id=48145>`_ and it is
422 the responsibility of the application installer to provide this. The
423 runtime may have already been installed on a user's system previously or
424 automatically via Windows Update, and can be detected by finding
425 ``ucrtbase.dll`` in the system directory.
426
427Third-party packages should be installed by the application installer alongside
428the embedded distribution. Using pip to manage dependencies as for a regular
429Python installation is not supported with this distribution, though with some
430care it may be possible to include and use pip for automatic updates. In
431general, third-party packages should be treated as part of the application
432("vendoring") so that the developer can ensure compatibility with newer
433versions before providing updates to users.
434
435The two recommended use cases for this distribution are described below.
436
437Python Application
438------------------
439
440An application written in Python does not necessarily require users to be aware
441of that fact. The embedded distribution may be used in this case to include a
442private version of Python in an install package. Depending on how transparent it
443should be (or conversely, how professional it should appear), there are two
444options.
445
446Using a specialized executable as a launcher requires some coding, but provides
447the most transparent experience for users. With a customized launcher, there are
448no obvious indications that the program is running on Python: icons can be
449customized, company and version information can be specified, and file
450associations behave properly. In most cases, a custom launcher should simply be
451able to call ``Py_Main`` with a hard-coded command line.
452
453The simpler approach is to provide a batch file or generated shortcut that
454directly calls the ``python.exe`` or ``pythonw.exe`` with the required
455command-line arguments. In this case, the application will appear to be Python
456and not its actual name, and users may have trouble distinguishing it from other
457running Python processes or file associations.
458
459With the latter approach, packages should be installed as directories alongside
460the Python executable to ensure they are available on the path. With the
461specialized launcher, packages can be located in other locations as there is an
462opportunity to specify the search path before launching the application.
463
464Embedding Python
465----------------
466
467Applications written in native code often require some form of scripting
468language, and the embedded Python distribution can be used for this purpose. In
469general, the majority of the application is in native code, and some part will
470either invoke ``python.exe`` or directly use ``python3.dll``. For either case,
471extracting the embedded distribution to a subdirectory of the application
472installation is sufficient to provide a loadable Python interpreter.
473
474As with the application use, packages can be installed to any location as there
475is an opportunity to specify search paths before initializing the interpreter.
476Otherwise, there is no fundamental differences between using the embedded
477distribution and a regular installation.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000478
479
480Alternative bundles
481===================
482
483Besides the standard CPython distribution, there are modified packages including
484additional functionality. The following is a list of popular versions and their
485key features:
486
Georg Brandl5d941342016-02-26 19:37:12 +0100487`ActivePython <https://www.activestate.com/activepython/>`_
Christian Heimesd8654cf2007-12-02 15:22:16 +0000488 Installer with multi-platform compatibility, documentation, PyWin32
489
Sanyam Khurana338cd832018-01-20 05:55:37 +0530490`Anaconda <https://www.anaconda.com/download/>`_
Steve Dowerbb240872015-02-05 22:08:48 -0800491 Popular scientific modules (such as numpy, scipy and pandas) and the
492 ``conda`` package manager.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000493
Sanyam Khurana338cd832018-01-20 05:55:37 +0530494`Canopy <https://www.enthought.com/product/canopy/>`_
Steve Dowerbb240872015-02-05 22:08:48 -0800495 A "comprehensive Python analysis environment" with editors and other
496 development tools.
497
498`WinPython <https://winpython.github.io/>`_
499 Windows-specific distribution with prebuilt scientific packages and
500 tools for building packages.
501
502Note that these packages may not include the latest versions of Python or
503other libraries, and are not maintained or supported by the core Python team.
Christian Heimes255f53b2007-12-08 15:33:56 +0000504
Christian Heimesd8654cf2007-12-02 15:22:16 +0000505
506
507Configuring Python
508==================
509
Steve Dowerbb240872015-02-05 22:08:48 -0800510To run Python conveniently from a command prompt, you might consider changing
511some default environment variables in Windows. While the installer provides an
512option to configure the PATH and PATHEXT variables for you, this is only
513reliable for a single, system-wide installation. If you regularly use multiple
514versions of Python, consider using the :ref:`launcher`.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000515
516
Brian Curtina86f1852012-08-19 11:22:20 -0500517.. _setting-envvars:
518
Christian Heimesd8654cf2007-12-02 15:22:16 +0000519Excursus: Setting environment variables
520---------------------------------------
521
Steve Dowerbb240872015-02-05 22:08:48 -0800522Windows allows environment variables to be configured permanently at both the
523User level and the System level, or temporarily in a command prompt.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000524
Steve Dowerbb240872015-02-05 22:08:48 -0800525To temporarily set environment variables, open Command Prompt and use the
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300526:command:`set` command:
527
528.. code-block:: doscon
Christian Heimesd8654cf2007-12-02 15:22:16 +0000529
Steve Dower7a177c02019-06-26 08:55:57 -0700530 C:\>set PATH=C:\Program Files\Python 3.9;%PATH%
Steve Dowerbb240872015-02-05 22:08:48 -0800531 C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
532 C:\>python
Christian Heimesd8654cf2007-12-02 15:22:16 +0000533
Steve Dowerbb240872015-02-05 22:08:48 -0800534These changes will apply to any further commands executed in that console, and
535will be inherited by any applications started from the console.
536
537Including the variable name within percent signs will expand to the existing
538value, allowing you to add your new value at either the start or the end.
539Modifying :envvar:`PATH` by adding the directory containing
540:program:`python.exe` to the start is a common way to ensure the correct version
541of Python is launched.
542
543To permanently modify the default environment variables, click Start and search
544for 'edit environment variables', or open System properties, :guilabel:`Advanced
545system settings` and click the :guilabel:`Environment Variables` button.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000546In this dialog, you can add or modify User and System variables. To change
547System variables, you need non-restricted access to your machine
548(i.e. Administrator rights).
549
Steve Dowerbb240872015-02-05 22:08:48 -0800550.. note::
Christian Heimesd8654cf2007-12-02 15:22:16 +0000551
Steve Dowerbb240872015-02-05 22:08:48 -0800552 Windows will concatenate User variables *after* System variables, which may
553 cause unexpected results when modifying :envvar:`PATH`.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000554
Steve Dowerbb240872015-02-05 22:08:48 -0800555 The :envvar:`PYTHONPATH` variable is used by all versions of Python 2 and
556 Python 3, so you should not permanently configure this variable unless it
Steve Dower9a578d92015-05-02 22:28:58 -0700557 only includes code that is compatible with all of your installed Python
Steve Dowerbb240872015-02-05 22:08:48 -0800558 versions.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000559
560.. seealso::
561
HiyashiChuka46ebe612018-08-03 11:44:06 +0900562 https://www.microsoft.com/en-us/wdsi/help/folder-variables
Christian Heimesd8654cf2007-12-02 15:22:16 +0000563 Environment variables in Windows NT
564
Georg Brandl5d941342016-02-26 19:37:12 +0100565 https://technet.microsoft.com/en-us/library/cc754250.aspx
Steve Dowerbb240872015-02-05 22:08:48 -0800566 The SET command, for temporarily modifying environment variables
567
Georg Brandl5d941342016-02-26 19:37:12 +0100568 https://technet.microsoft.com/en-us/library/cc755104.aspx
Steve Dowerbb240872015-02-05 22:08:48 -0800569 The SETX command, for permanently modifying environment variables
570
Sanyam Khurana338cd832018-01-20 05:55:37 +0530571 https://support.microsoft.com/en-us/help/310519/how-to-manage-environment-variables-in-windows-xp
Christian Heimesd8654cf2007-12-02 15:22:16 +0000572 How To Manage Environment Variables in Windows XP
573
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300574 https://www.chem.gla.ac.uk/~louis/software/faq/q1.html
Christian Heimes255f53b2007-12-08 15:33:56 +0000575 Setting Environment variables, Louis J. Farrugia
576
Nick Coghlan349c8022012-09-30 13:00:43 +0530577.. _windows-path-mod:
578
Christian Heimesd8654cf2007-12-02 15:22:16 +0000579Finding the Python executable
580-----------------------------
581
Steve Dowerbb240872015-02-05 22:08:48 -0800582.. versionchanged:: 3.5
Brian Curtina86f1852012-08-19 11:22:20 -0500583
Christian Heimesd8654cf2007-12-02 15:22:16 +0000584Besides using the automatically created start menu entry for the Python
Steve Dowerbb240872015-02-05 22:08:48 -0800585interpreter, you might want to start Python in the command prompt. The
Steve Dowerf333fd82016-09-09 09:40:06 -0700586installer has an option to set that up for you.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000587
Steve Dower4db86bc2016-09-09 09:17:35 -0700588On the first page of the installer, an option labelled "Add Python to PATH"
589may be selected to have the installer add the install location into the
Steve Dowerbb240872015-02-05 22:08:48 -0800590:envvar:`PATH`. The location of the :file:`Scripts\\` folder is also added.
591This allows you to type :command:`python` to run the interpreter, and
Zachary Ware72e483a2016-08-04 12:13:47 -0500592:command:`pip` for the package installer. Thus, you can also execute your
Brian Curtina86f1852012-08-19 11:22:20 -0500593scripts with command line options, see :ref:`using-on-cmdline` documentation.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000594
Brian Curtina86f1852012-08-19 11:22:20 -0500595If you don't enable this option at install time, you can always re-run the
Steve Dowerbb240872015-02-05 22:08:48 -0800596installer, select Modify, and enable it. Alternatively, you can manually
597modify the :envvar:`PATH` using the directions in :ref:`setting-envvars`. You
598need to set your :envvar:`PATH` environment variable to include the directory
599of your Python installation, delimited by a semicolon from other entries. An
600example variable could look like this (assuming the first two entries already
601existed)::
Brian Curtina86f1852012-08-19 11:22:20 -0500602
Steve Dower7a177c02019-06-26 08:55:57 -0700603 C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.9
Christian Heimesd8654cf2007-12-02 15:22:16 +0000604
Inada Naoki148610d2020-01-28 19:12:31 +0900605.. _win-utf8-mode:
606
607UTF-8 mode
608==========
609
610.. versionadded:: 3.7
611
612Windows still uses legacy encodings for the system encoding (the ANSI Code
613Page). Python uses it for the default encoding of text files (e.g.
614:func:`locale.getpreferredencoding`).
615
616This may cause issues because UTF-8 is widely used on the internet
617and most Unix systems, including WSL (Windows Subsystem for Linux).
618
619You can use UTF-8 mode to change the default text encoding to UTF-8.
620You can enable UTF-8 mode via the ``-X utf8`` command line option, or
621the ``PYTHONUTF8=1`` environment variable. See :envvar:`PYTHONUTF8` for
622enabling UTF-8 mode, and :ref:`setting-envvars` for how to modify
623environment variables.
624
625When UTF-8 mode is enabled:
626
627* :func:`locale.getpreferredencoding` returns ``'UTF-8'`` instead of
628 the system encoding. This function is used for the default text
629 encoding in many places, including :func:`open`, :class:`Popen`,
630 :meth:`Path.read_text`, etc.
631* :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr`
632 all use UTF-8 as their text encoding.
633* You can still use the system encoding via the "mbcs" codec.
634
635Note that adding ``PYTHONUTF8=1`` to the default environment variables
636will affect all Python 3.7+ applications on your system.
637If you have any Python 3.7+ applications which rely on the legacy
638system encoding, it is recommended to set the environment variable
639temporarily or use the ``-X utf8`` command line option.
640
641.. note::
642 Even when UTF-8 mode is disabled, Python uses UTF-8 by default
643 on Windows for:
644
645 * Console I/O including standard I/O (see :pep:`528` for details).
646 * The filesystem encoding (see :pep:`529` for details).
647
648
Vinay Sajipd89dae12012-06-24 11:23:07 +0100649.. _launcher:
650
651Python Launcher for Windows
652===========================
653
654.. versionadded:: 3.3
655
Steve Dowerbb240872015-02-05 22:08:48 -0800656The Python launcher for Windows is a utility which aids in locating and
657executing of different Python versions. It allows scripts (or the
Vinay Sajipd89dae12012-06-24 11:23:07 +0100658command-line) to indicate a preference for a specific Python version, and
659will locate and execute that version.
660
Steve Dowerbb240872015-02-05 22:08:48 -0800661Unlike the :envvar:`PATH` variable, the launcher will correctly select the most
662appropriate version of Python. It will prefer per-user installations over
663system-wide ones, and orders by language version rather than using the most
664recently installed version.
665
Steve Dower0cd63912018-12-10 18:52:57 -0800666The launcher was originally specified in :pep:`397`.
667
Vinay Sajipd89dae12012-06-24 11:23:07 +0100668Getting started
669---------------
670
671From the command-line
672^^^^^^^^^^^^^^^^^^^^^
673
Paul Moore835416c2016-05-22 12:28:41 +0100674.. versionchanged:: 3.6
675
Steve Dowerbb240872015-02-05 22:08:48 -0800676System-wide installations of Python 3.3 and later will put the launcher on your
677:envvar:`PATH`. The launcher is compatible with all available versions of
678Python, so it does not matter which version is installed. To check that the
679launcher is available, execute the following command in Command Prompt:
Vinay Sajipd89dae12012-06-24 11:23:07 +0100680
681::
682
683 py
684
Paul Moore835416c2016-05-22 12:28:41 +0100685You should find that the latest version of Python you have installed is
Vinay Sajipd89dae12012-06-24 11:23:07 +0100686started - it can be exited as normal, and any additional command-line
687arguments specified will be sent directly to Python.
688
Steve Dowerf333fd82016-09-09 09:40:06 -0700689If you have multiple versions of Python installed (e.g., 2.7 and |version|) you
690will have noticed that Python |version| was started - to launch Python 2.7, try
691the command:
Vinay Sajipd89dae12012-06-24 11:23:07 +0100692
693::
694
Paul Moore835416c2016-05-22 12:28:41 +0100695 py -2.7
Vinay Sajipd89dae12012-06-24 11:23:07 +0100696
Paul Moore835416c2016-05-22 12:28:41 +0100697If you want the latest version of Python 2.x you have installed, try the
698command:
Vinay Sajipd89dae12012-06-24 11:23:07 +0100699
700::
701
Paul Moore835416c2016-05-22 12:28:41 +0100702 py -2
Vinay Sajipd89dae12012-06-24 11:23:07 +0100703
Paul Moore835416c2016-05-22 12:28:41 +0100704You should find the latest version of Python 2.x starts.
Vinay Sajipd89dae12012-06-24 11:23:07 +0100705
Steve Dowerbb240872015-02-05 22:08:48 -0800706If you see the following error, you do not have the launcher installed:
707
708::
709
710 'py' is not recognized as an internal or external command,
711 operable program or batch file.
712
713Per-user installations of Python do not add the launcher to :envvar:`PATH`
714unless the option was selected on installation.
715
Steve Dower76998fe2015-02-26 14:25:33 -0800716Virtual environments
717^^^^^^^^^^^^^^^^^^^^
718
Berker Peksagf2d4e572015-03-02 05:36:19 +0200719.. versionadded:: 3.5
720
Steve Dower76998fe2015-02-26 14:25:33 -0800721If the launcher is run with no explicit Python version specification, and a
722virtual environment (created with the standard library :mod:`venv` module or
723the external ``virtualenv`` tool) active, the launcher will run the virtual
724environment's interpreter rather than the global one. To run the global
725interpreter, either deactivate the virtual environment, or explicitly specify
726the global Python version.
727
Vinay Sajipd89dae12012-06-24 11:23:07 +0100728From a script
729^^^^^^^^^^^^^
730
731Let's create a test Python script - create a file called ``hello.py`` with the
732following contents
733
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300734.. code-block:: python
Vinay Sajipd89dae12012-06-24 11:23:07 +0100735
736 #! python
737 import sys
738 sys.stdout.write("hello from Python %s\n" % (sys.version,))
739
740From the directory in which hello.py lives, execute the command:
741
742::
743
744 py hello.py
745
746You should notice the version number of your latest Python 2.x installation
747is printed. Now try changing the first line to be:
748
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300749.. code-block:: python
Vinay Sajipd89dae12012-06-24 11:23:07 +0100750
751 #! python3
752
753Re-executing the command should now print the latest Python 3.x information.
754As with the above command-line examples, you can specify a more explicit
755version qualifier. Assuming you have Python 2.6 installed, try changing the
756first line to ``#! python2.6`` and you should find the 2.6 version
757information printed.
758
Paul Moore835416c2016-05-22 12:28:41 +0100759Note that unlike interactive use, a bare "python" will use the latest
760version of Python 2.x that you have installed. This is for backward
761compatibility and for compatibility with Unix, where the command ``python``
762typically refers to Python 2.
763
Vinay Sajipd89dae12012-06-24 11:23:07 +0100764From file associations
765^^^^^^^^^^^^^^^^^^^^^^
766
767The launcher should have been associated with Python files (i.e. ``.py``,
Brett Cannonf299abd2015-04-13 14:21:02 -0400768``.pyw``, ``.pyc`` files) when it was installed. This means that
Vinay Sajipd89dae12012-06-24 11:23:07 +0100769when you double-click on one of these files from Windows explorer the launcher
770will be used, and therefore you can use the same facilities described above to
771have the script specify the version which should be used.
772
773The key benefit of this is that a single launcher can support multiple Python
774versions at the same time depending on the contents of the first line.
775
776Shebang Lines
777-------------
778
779If the first line of a script file starts with ``#!``, it is known as a
780"shebang" line. Linux and other Unix like operating systems have native
Wieland Hoffmann80a3da42017-04-28 18:12:57 +0200781support for such lines and they are commonly used on such systems to indicate
782how a script should be executed. This launcher allows the same facilities to
783be used with Python scripts on Windows and the examples above demonstrate their
Vinay Sajipd89dae12012-06-24 11:23:07 +0100784use.
785
786To allow shebang lines in Python scripts to be portable between Unix and
787Windows, this launcher supports a number of 'virtual' commands to specify
788which interpreter to use. The supported virtual commands are:
789
790* ``/usr/bin/env python``
791* ``/usr/bin/python``
792* ``/usr/local/bin/python``
793* ``python``
794
795For example, if the first line of your script starts with
796
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300797.. code-block:: sh
Vinay Sajipd89dae12012-06-24 11:23:07 +0100798
799 #! /usr/bin/python
800
801The default Python will be located and used. As many Python scripts written
802to work on Unix will already have this line, you should find these scripts can
803be used by the launcher without modification. If you are writing a new script
804on Windows which you hope will be useful on Unix, you should use one of the
805shebang lines starting with ``/usr``.
806
Steve Dower76998fe2015-02-26 14:25:33 -0800807Any of the above virtual commands can be suffixed with an explicit version
mrh19971e2ad6c2019-04-13 00:26:47 +0200808(either just the major version, or the major and minor version).
809Furthermore the 32-bit version can be requested by adding "-32" after the
810minor version. I.e. ``/usr/bin/python2.7-32`` will request usage of the
81132-bit python 2.7.
812
813.. versionadded:: 3.7
814
815 Beginning with python launcher 3.7 it is possible to request 64-bit version
816 by the "-64" suffix. Furthermore it is possible to specify a major and
817 architecture without minor (i.e. ``/usr/bin/python3-64``).
Steve Dower76998fe2015-02-26 14:25:33 -0800818
819The ``/usr/bin/env`` form of shebang line has one further special property.
820Before looking for installed Python interpreters, this form will search the
821executable :envvar:`PATH` for a Python executable. This corresponds to the
822behaviour of the Unix ``env`` program, which performs a :envvar:`PATH` search.
823
Vinay Sajipd89dae12012-06-24 11:23:07 +0100824Arguments in shebang lines
825--------------------------
826
827The shebang lines can also specify additional options to be passed to the
828Python interpreter. For example, if you have a shebang line:
829
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300830.. code-block:: sh
Vinay Sajipd89dae12012-06-24 11:23:07 +0100831
832 #! /usr/bin/python -v
833
834Then Python will be started with the ``-v`` option
835
836Customization
837-------------
838
839Customization via INI files
840^^^^^^^^^^^^^^^^^^^^^^^^^^^
841
Steve Dowerbb240872015-02-05 22:08:48 -0800842Two .ini files will be searched by the launcher - ``py.ini`` in the current
843user's "application data" directory (i.e. the directory returned by calling the
Segev Finer8e7e8bd2018-07-30 20:11:30 +0300844Windows function ``SHGetFolderPath`` with ``CSIDL_LOCAL_APPDATA``) and ``py.ini`` in the
Steve Dowerbb240872015-02-05 22:08:48 -0800845same directory as the launcher. The same .ini files are used for both the
846'console' version of the launcher (i.e. py.exe) and for the 'windows' version
Andre Delfino55f41e42018-12-05 16:45:30 -0300847(i.e. pyw.exe).
Vinay Sajipd89dae12012-06-24 11:23:07 +0100848
Steve Dowerbb240872015-02-05 22:08:48 -0800849Customization specified in the "application directory" will have precedence over
850the one next to the executable, so a user, who may not have write access to the
Andre Delfino55f41e42018-12-05 16:45:30 -0300851.ini file next to the launcher, can override commands in that global .ini file.
Vinay Sajipd89dae12012-06-24 11:23:07 +0100852
853Customizing default Python versions
854^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
855
856In some cases, a version qualifier can be included in a command to dictate
857which version of Python will be used by the command. A version qualifier
858starts with a major version number and can optionally be followed by a period
Xtreak0d702272019-06-03 04:42:33 +0530859('.') and a minor version specifier. Furthermore it is possible to specify
mrh19971e2ad6c2019-04-13 00:26:47 +0200860if a 32 or 64 bit implementation shall be requested by adding "-32" or "-64".
Vinay Sajipd89dae12012-06-24 11:23:07 +0100861
862For example, a shebang line of ``#!python`` has no version qualifier, while
863``#!python3`` has a version qualifier which specifies only a major version.
864
mrh19971e2ad6c2019-04-13 00:26:47 +0200865If no version qualifiers are found in a command, the environment
866variable :envvar:`PY_PYTHON` can be set to specify the default version
867qualifier. If it is not set, the default is "3". The variable can
868specify any value that may be passed on the command line, such as "3",
869"3.7", "3.7-32" or "3.7-64". (Note that the "-64" option is only
870available with the launcher included with Python 3.7 or newer.)
Vinay Sajipd89dae12012-06-24 11:23:07 +0100871
872If no minor version qualifiers are found, the environment variable
873``PY_PYTHON{major}`` (where ``{major}`` is the current major version qualifier
874as determined above) can be set to specify the full version. If no such option
875is found, the launcher will enumerate the installed Python versions and use
876the latest minor release found for the major version, which is likely,
877although not guaranteed, to be the most recently installed version in that
878family.
879
880On 64-bit Windows with both 32-bit and 64-bit implementations of the same
881(major.minor) Python version installed, the 64-bit version will always be
882preferred. This will be true for both 32-bit and 64-bit implementations of the
883launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation
884of the specified version if available. This is so the behavior of the launcher
885can be predicted knowing only what versions are installed on the PC and
886without regard to the order in which they were installed (i.e., without knowing
887whether a 32 or 64-bit version of Python and corresponding launcher was
mrh19971e2ad6c2019-04-13 00:26:47 +0200888installed last). As noted above, an optional "-32" or "-64" suffix can be
889used on a version specifier to change this behaviour.
Vinay Sajipd89dae12012-06-24 11:23:07 +0100890
891Examples:
892
893* If no relevant options are set, the commands ``python`` and
894 ``python2`` will use the latest Python 2.x version installed and
895 the command ``python3`` will use the latest Python 3.x installed.
896
897* The commands ``python3.1`` and ``python2.7`` will not consult any
898 options at all as the versions are fully specified.
899
900* If ``PY_PYTHON=3``, the commands ``python`` and ``python3`` will both use
901 the latest installed Python 3 version.
902
903* If ``PY_PYTHON=3.1-32``, the command ``python`` will use the 32-bit
904 implementation of 3.1 whereas the command ``python3`` will use the latest
905 installed Python (PY_PYTHON was not considered at all as a major
906 version was specified.)
907
908* If ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1``, the commands
909 ``python`` and ``python3`` will both use specifically 3.1
910
911In addition to environment variables, the same settings can be configured
912in the .INI file used by the launcher. The section in the INI file is
913called ``[defaults]`` and the key name will be the same as the
Ezio Melotti5b69fbd2012-10-19 20:40:18 +0300914environment variables without the leading ``PY_`` prefix (and note that
Vinay Sajipd89dae12012-06-24 11:23:07 +0100915the key names in the INI file are case insensitive.) The contents of
916an environment variable will override things specified in the INI file.
917
918For example:
919
920* Setting ``PY_PYTHON=3.1`` is equivalent to the INI file containing:
921
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300922.. code-block:: ini
Vinay Sajipd89dae12012-06-24 11:23:07 +0100923
924 [defaults]
925 python=3.1
926
927* Setting ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1`` is equivalent to the INI file
928 containing:
929
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300930.. code-block:: ini
Vinay Sajipd89dae12012-06-24 11:23:07 +0100931
932 [defaults]
933 python=3
934 python3=3.1
935
936Diagnostics
937-----------
938
939If an environment variable ``PYLAUNCH_DEBUG`` is set (to any value), the
940launcher will print diagnostic information to stderr (i.e. to the console).
941While this information manages to be simultaneously verbose *and* terse, it
942should allow you to see what versions of Python were located, why a
943particular version was chosen and the exact command-line used to execute the
944target Python.
945
946
Steve Dowerbb240872015-02-05 22:08:48 -0800947
Steve Dower3cdd7f52016-09-09 15:22:13 -0700948.. _finding_modules:
Steve Dowereb3c16d2015-08-08 09:09:01 -0700949
Steve Dowerbb240872015-02-05 22:08:48 -0800950Finding modules
951===============
952
953Python usually stores its library (and thereby your site-packages folder) in the
954installation directory. So, if you had installed Python to
955:file:`C:\\Python\\`, the default library would reside in
956:file:`C:\\Python\\Lib\\` and third-party modules should be stored in
957:file:`C:\\Python\\Lib\\site-packages\\`.
958
Steve Dowered51b262016-09-17 12:54:06 -0700959To completely override :data:`sys.path`, create a ``._pth`` file with the same
Steve Dower15ea3a62018-01-29 10:34:09 +1100960name as the DLL (``python37._pth``) or the executable (``python._pth``) and
Steve Dowered51b262016-09-17 12:54:06 -0700961specify one line for each path to add to :data:`sys.path`. The file based on the
962DLL name overrides the one based on the executable, which allows paths to be
963restricted for any program loading the runtime if desired.
Steve Dower4db86bc2016-09-09 09:17:35 -0700964
Steve Dowered51b262016-09-17 12:54:06 -0700965When the file exists, all registry and environment variables are ignored,
966isolated mode is enabled, and :mod:`site` is not imported unless one line in the
967file specifies ``import site``. Blank paths and lines starting with ``#`` are
968ignored. Each path may be absolute or relative to the location of the file.
969Import statements other than to ``site`` are not permitted, and arbitrary code
970cannot be specified.
971
972Note that ``.pth`` files (without leading underscore) will be processed normally
Steve Dower15ea3a62018-01-29 10:34:09 +1100973by the :mod:`site` module when ``import site`` has been specified.
Steve Dowered51b262016-09-17 12:54:06 -0700974
975When no ``._pth`` file is found, this is how :data:`sys.path` is populated on
976Windows:
Steve Dowerbb240872015-02-05 22:08:48 -0800977
978* An empty entry is added at the start, which corresponds to the current
979 directory.
980
981* If the environment variable :envvar:`PYTHONPATH` exists, as described in
982 :ref:`using-on-envvars`, its entries are added next. Note that on Windows,
983 paths in this variable must be separated by semicolons, to distinguish them
984 from the colon used in drive identifiers (``C:\`` etc.).
985
986* Additional "application paths" can be added in the registry as subkeys of
987 :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the
988 ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have
989 semicolon-delimited path strings as their default value will cause each path
990 to be added to :data:`sys.path`. (Note that all known installers only use
991 HKLM, so HKCU is typically empty.)
992
993* If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as
994 "Python Home". Otherwise, the path of the main Python executable is used to
Steve Dower814f9ae2016-09-09 15:07:46 -0700995 locate a "landmark file" (either ``Lib\os.py`` or ``pythonXY.zip``) to deduce
996 the "Python Home". If a Python home is found, the relevant sub-directories
997 added to :data:`sys.path` (``Lib``, ``plat-win``, etc) are based on that
998 folder. Otherwise, the core Python path is constructed from the PythonPath
999 stored in the registry.
Steve Dowerbb240872015-02-05 22:08:48 -08001000
1001* If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in
1002 the environment, and no registry entries can be found, a default path with
1003 relative entries is used (e.g. ``.\Lib;.\plat-win``, etc).
1004
Steve Dower4a7fe7e2015-05-22 15:10:10 -07001005If a ``pyvenv.cfg`` file is found alongside the main executable or in the
1006directory one level above the executable, the following variations apply:
1007
1008* If ``home`` is an absolute path and :envvar:`PYTHONHOME` is not set, this
1009 path is used instead of the path to the main executable when deducing the
1010 home location.
1011
Steve Dowerbb240872015-02-05 22:08:48 -08001012The end result of all this is:
1013
1014* When running :file:`python.exe`, or any other .exe in the main Python
1015 directory (either an installed version, or directly from the PCbuild
1016 directory), the core path is deduced, and the core paths in the registry are
1017 ignored. Other "application paths" in the registry are always read.
1018
1019* When Python is hosted in another .exe (different directory, embedded via COM,
1020 etc), the "Python Home" will not be deduced, so the core path from the
1021 registry is used. Other "application paths" in the registry are always read.
1022
Steve Dower4a7fe7e2015-05-22 15:10:10 -07001023* If Python can't find its home and there are no registry value (frozen .exe,
1024 some very strange installation setup) you get a path with some default, but
Steve Dowerbb240872015-02-05 22:08:48 -08001025 relative, paths.
1026
1027For those who want to bundle Python into their application or distribution, the
1028following advice will prevent conflicts with other installations:
1029
Steve Dowered51b262016-09-17 12:54:06 -07001030* Include a ``._pth`` file alongside your executable containing the
1031 directories to include. This will ignore paths listed in the registry and
1032 environment variables, and also ignore :mod:`site` unless ``import site`` is
1033 listed.
Steve Dower4a7fe7e2015-05-22 15:10:10 -07001034
Steve Dower15ea3a62018-01-29 10:34:09 +11001035* If you are loading :file:`python3.dll` or :file:`python37.dll` in your own
Steve Dowerbb240872015-02-05 22:08:48 -08001036 executable, explicitly call :c:func:`Py_SetPath` or (at least)
1037 :c:func:`Py_SetProgramName` before :c:func:`Py_Initialize`.
1038
1039* Clear and/or overwrite :envvar:`PYTHONPATH` and set :envvar:`PYTHONHOME`
1040 before launching :file:`python.exe` from your application.
1041
1042* If you cannot use the previous suggestions (for example, you are a
1043 distribution that allows people to run :file:`python.exe` directly), ensure
Steve Dower4a7fe7e2015-05-22 15:10:10 -07001044 that the landmark file (:file:`Lib\\os.py`) exists in your install directory.
Steve Dower814f9ae2016-09-09 15:07:46 -07001045 (Note that it will not be detected inside a ZIP file, but a correctly named
1046 ZIP file will be detected instead.)
Steve Dowerbb240872015-02-05 22:08:48 -08001047
1048These will ensure that the files in a system-wide installation will not take
1049precedence over the copy of the standard library bundled with your application.
Steve Dowereb3c16d2015-08-08 09:09:01 -07001050Otherwise, your users may experience problems using your application. Note that
Segev Finer8e7e8bd2018-07-30 20:11:30 +03001051the first suggestion is the best, as the others may still be susceptible to
Steve Dowereb3c16d2015-08-08 09:09:01 -07001052non-standard paths in the registry and user site-packages.
Steve Dowerbb240872015-02-05 22:08:48 -08001053
Steve Dower814f9ae2016-09-09 15:07:46 -07001054.. versionchanged::
1055 3.6
Steve Dower4db86bc2016-09-09 09:17:35 -07001056
Victor Stinnerc6b1e152016-10-20 00:45:50 +02001057 * Adds ``._pth`` file support and removes ``applocal`` option from
1058 ``pyvenv.cfg``.
1059 * Adds ``pythonXX.zip`` as a potential landmark when directly adjacent
1060 to the executable.
Steve Dower4db86bc2016-09-09 09:17:35 -07001061
Steve Dower20367422016-12-07 13:02:27 -08001062.. deprecated::
1063 3.6
1064
1065 Modules specified in the registry under ``Modules`` (not ``PythonPath``)
1066 may be imported by :class:`importlib.machinery.WindowsRegistryFinder`.
1067 This finder is enabled on Windows in 3.6.0 and earlier, but may need to
1068 be explicitly added to :attr:`sys.meta_path` in the future.
1069
Christian Heimesd8654cf2007-12-02 15:22:16 +00001070Additional modules
1071==================
1072
1073Even though Python aims to be portable among all platforms, there are features
1074that are unique to Windows. A couple of modules, both in the standard library
1075and external, and snippets exist to use these features.
1076
1077The Windows-specific standard modules are documented in
1078:ref:`mswin-specific-services`.
1079
Christian Heimesd8654cf2007-12-02 15:22:16 +00001080PyWin32
1081-------
1082
Stéphane Wirtel19177fb2018-05-15 20:58:35 +02001083The `PyWin32 <https://pypi.org/project/pywin32>`_ module by Mark Hammond
Christian Heimesd8654cf2007-12-02 15:22:16 +00001084is a collection of modules for advanced Windows-specific support. This includes
Georg Brandl2ee470f2008-07-16 12:55:28 +00001085utilities for:
Christian Heimesd8654cf2007-12-02 15:22:16 +00001086
Julien Palard7114c652019-05-25 20:02:24 +02001087* `Component Object Model
1088 <https://docs.microsoft.com/en-us/windows/desktop/com/component-object-model--com--portal>`_
1089 (COM)
Christian Heimesd8654cf2007-12-02 15:22:16 +00001090* Win32 API calls
1091* Registry
1092* Event log
Georg Brandl5d941342016-02-26 19:37:12 +01001093* `Microsoft Foundation Classes <https://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx>`_ (MFC)
Christian Heimesd8654cf2007-12-02 15:22:16 +00001094 user interfaces
1095
Georg Brandl5d941342016-02-26 19:37:12 +01001096`PythonWin <https://web.archive.org/web/20060524042422/
Georg Brandle73778c2014-10-29 08:36:35 +01001097https://www.python.org/windows/pythonwin/>`_ is a sample MFC application
Christian Heimesd8654cf2007-12-02 15:22:16 +00001098shipped with PyWin32. It is an embeddable IDE with a built-in debugger.
1099
1100.. seealso::
1101
1102 `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_
1103 by Tim Golden
1104
1105 `Python and COM <http://www.boddie.org.uk/python/COM.html>`_
1106 by David and Paul Boddie
1107
1108
Zachary Warec3cf97b2014-01-17 15:23:42 -06001109cx_Freeze
1110---------
Christian Heimesd8654cf2007-12-02 15:22:16 +00001111
Sanyam Khurana1b4587a2017-12-06 22:09:33 +05301112`cx_Freeze <https://anthony-tuininga.github.io/cx_Freeze/>`_ is a :mod:`distutils`
Zachary Warec3cf97b2014-01-17 15:23:42 -06001113extension (see :ref:`extending-distutils`) which wraps Python scripts into
1114executable Windows programs (:file:`{*}.exe` files). When you have done this,
1115you can distribute your application without requiring your users to install
1116Python.
Christian Heimesd8654cf2007-12-02 15:22:16 +00001117
1118
1119WConio
1120------
1121
1122Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to
1123Unix-like systems, there is a library exclusive to Windows as well: Windows
1124Console I/O for Python.
1125
1126`WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for
1127Turbo-C's :file:`CONIO.H`, used to create text user interfaces.
1128
1129
1130
1131Compiling Python on Windows
1132===========================
1133
1134If you want to compile CPython yourself, first thing you should do is get the
Georg Brandl5d941342016-02-26 19:37:12 +01001135`source <https://www.python.org/downloads/source/>`_. You can download either the
Christian Heimesd8654cf2007-12-02 15:22:16 +00001136latest release's source or just grab a fresh `checkout
Lisa Hewus Fresh384899d2017-08-30 09:37:43 -07001137<https://devguide.python.org/setup/#getting-the-source-code>`_.
Christian Heimesd8654cf2007-12-02 15:22:16 +00001138
Zachary Waref8ceb042013-11-30 16:59:33 -06001139The source tree contains a build solution and project files for Microsoft
Steve Dowerbb240872015-02-05 22:08:48 -08001140Visual Studio 2015, which is the compiler used to build the official Python
1141releases. These files are in the :file:`PCbuild` directory.
Christian Heimesd8654cf2007-12-02 15:22:16 +00001142
Zachary Waref8ceb042013-11-30 16:59:33 -06001143Check :file:`PCbuild/readme.txt` for general information on the build process.
Christian Heimesd8654cf2007-12-02 15:22:16 +00001144
1145
1146For extension modules, consult :ref:`building-on-windows`.
1147
1148.. seealso::
1149
1150 `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_
1151 or "Creating Python extensions in C/C++ with SWIG and compiling them with
1152 MinGW gcc under Windows" or "Installing Python extension with distutils
1153 and without Microsoft Visual C++" by Sébastien Sauvage, 2003
1154
Julien Palard7114c652019-05-25 20:02:24 +02001155 `MingW -- Python extensions <http://www.mingw.org/wiki/FAQ#toc14>`_
Christian Heimesd8654cf2007-12-02 15:22:16 +00001156
1157
Steve Dower0cd63912018-12-10 18:52:57 -08001158Other Platforms
Christian Heimesd8654cf2007-12-02 15:22:16 +00001159===============
1160
Steve Dower0cd63912018-12-10 18:52:57 -08001161With ongoing development of Python, some platforms that used to be supported
1162earlier are no longer supported (due to the lack of users or developers).
1163Check :pep:`11` for details on all unsupported platforms.
Christian Heimesd8654cf2007-12-02 15:22:16 +00001164
Steve Dower0cd63912018-12-10 18:52:57 -08001165* `Windows CE <http://pythonce.sourceforge.net/>`_ is still supported.
1166* The `Cygwin <https://cygwin.com/>`_ installer offers to install the Python
1167 interpreter as well (cf. `Cygwin package source
1168 <ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/
1169 release/python>`_, `Maintainer releases
1170 <http://www.tishler.net/jason/software/python/>`_)
Christian Heimesd8654cf2007-12-02 15:22:16 +00001171
Steve Dower0cd63912018-12-10 18:52:57 -08001172See `Python for Windows <https://www.python.org/downloads/windows/>`_
1173for detailed information about platforms with pre-compiled installers.