blob: e93fa0a78d1fbd4a2997b19077cd90a913d544a9 [file] [log] [blame]
Christian Heimesd8654cf2007-12-02 15:22:16 +00001.. highlightlang:: none
2
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
Christian Heimesd8654cf2007-12-02 15:22:16 +000015Installing Python
16=================
17
Steve Dowerbb240872015-02-05 22:08:48 -080018Unlike most Unix systems and services, Windows does not include a system
19supported installation of Python. To make Python available, the CPython team
Christian Heimesd8654cf2007-12-02 15:22:16 +000020has compiled Windows installers (MSI packages) with every `release
Steve Dowerbb240872015-02-05 22:08:48 -080021<https://www.python.org/download/releases/>`_ for many years. These installers
Steve Dower9a578d92015-05-02 22:28:58 -070022are primarily intended to add a per-user installation of Python, with the
23core interpreter and library being used by a single user. The installer is also
24able to install for all users of a single machine, and a separate ZIP file is
25available for application-local distributions.
Steve Dowerbb240872015-02-05 22:08:48 -080026
Brett Cannon85622e42015-12-27 12:08:37 -080027Supported Versions
28------------------
29
30As specified in :pep:`11`, a Python release only supports a Windows platform
31while Microsoft considers the platform under extended support. This means that
Steve Dowerf333fd82016-09-09 09:40:06 -070032Python |version| supports Windows Vista and newer. If you require Windows XP
33support then please install Python 3.4.
Brett Cannon85622e42015-12-27 12:08:37 -080034
Steve Dowerbb240872015-02-05 22:08:48 -080035Installation Steps
36------------------
37
Steve Dowerf333fd82016-09-09 09:40:06 -070038Four Python |version| installers are available for download - two each for the
3932-bit and 64-bit versions of the interpreter. The *web installer* is a small
40initial download, and it will automatically download the required components as
Steve Dowerbb240872015-02-05 22:08:48 -080041necessary. The *offline installer* includes the components necessary for a
42default installation and only requires an internet connection for optional
43features. See :ref:`install-layout-option` for other ways to avoid downloading
44during installation.
45
Steve Dower9a578d92015-05-02 22:28:58 -070046After starting the installer, one of two options may be selected:
Steve Dowerbb240872015-02-05 22:08:48 -080047
48.. image:: win_installer.png
49
Steve Dower9a578d92015-05-02 22:28:58 -070050If you select "Install Now":
Steve Dowerbb240872015-02-05 22:08:48 -080051
Steve Dower9a578d92015-05-02 22:28:58 -070052* You will *not* need to be an administrator (unless a system update for the
Steve Dowereb3c16d2015-08-08 09:09:01 -070053 C Runtime Library is required or you install the :ref:`launcher` for all
54 users)
Steve Dowerbb240872015-02-05 22:08:48 -080055* Python will be installed into your user directory
Steve Dowereb3c16d2015-08-08 09:09:01 -070056* The :ref:`launcher` will be installed according to the option at the bottom
Zachary Ware87ec85f2016-07-28 19:00:53 -050057 of the first page
Steve Dowerbb240872015-02-05 22:08:48 -080058* The standard library, test suite, launcher and pip will be installed
Steve Dower9a578d92015-05-02 22:28:58 -070059* If selected, the install directory will be added to your :envvar:`PATH`
60* Shortcuts will only be visible for the current user
Steve Dowerbb240872015-02-05 22:08:48 -080061
62Selecting "Customize installation" will allow you to select the features to
63install, the installation location and other options or post-install actions.
64To install debugging symbols or binaries, you will need to use this option.
65
Steve Dower9a578d92015-05-02 22:28:58 -070066To perform an all-users installation, you should select "Customize
67installation". In this case:
68
69* You may be required to provide administrative credentials or approval
70* Python will be installed into the Program Files directory
71* The :ref:`launcher` will be installed into the Windows directory
72* Optional features may be selected during installation
Steve Dowereb3c16d2015-08-08 09:09:01 -070073* The standard library can be pre-compiled to bytecode
Steve Dower9a578d92015-05-02 22:28:58 -070074* If selected, the install directory will be added to the system :envvar:`PATH`
75* Shortcuts are available for all users
76
Steve Dower19ab0fd2016-09-06 20:40:11 -070077.. _max-path:
78
79Removing the MAX_PATH Limitation
80--------------------------------
81
82Windows historically has limited path lengths to 260 characters. This meant that
83paths longer than this would not resolve and errors would result.
84
85In the latest versions of Windows, this limitation can be expanded to
86approximately 32,000 characters. Your administrator will need to activate the
87"Enable Win32 long paths" group policy, or set the registry value
88``HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled``
89to ``1``.
90
91This allows the :func:`open` function, the :mod:`os` module and most other
92path functionality to accept and return paths longer than 260 characters when
93using strings. (Use of bytes as paths is deprecated on Windows, and this feature
94is not available when using bytes.)
95
96After changing the above option, no further configuration is required.
97
98.. versionchanged:: 3.6
99
100 Support for long paths was enabled in Python.
101
Steve Dowerbb240872015-02-05 22:08:48 -0800102.. _install-quiet-option:
103
104Installing Without UI
105---------------------
106
107All of the options available in the installer UI can also be specified from the
108command line, allowing scripted installers to replicate an installation on many
109machines without user interaction. These options may also be set without
110suppressing the UI in order to change some of the defaults.
111
112To completely hide the installer UI and install Python silently, pass the
Steve Dowercb0afc82015-09-11 08:47:42 -0700113``/quiet`` option. To skip past the user interaction but still display
114progress and errors, pass the ``/passive`` option. The ``/uninstall``
Steve Dowerbb240872015-02-05 22:08:48 -0800115option may be passed to immediately begin removing Python - no prompt will be
116displayed.
117
118All other options are passed as ``name=value``, where the value is usually
119``0`` to disable a feature, ``1`` to enable a feature, or a path. The full list
120of available options is shown below.
121
122+---------------------------+--------------------------------------+--------------------------+
123| Name | Description | Default |
124+===========================+======================================+==========================+
Steve Dower9b608e52015-09-13 14:39:26 -0700125| InstallAllUsers | Perform a system-wide installation. | 0 |
Steve Dowerbb240872015-02-05 22:08:48 -0800126+---------------------------+--------------------------------------+--------------------------+
127| TargetDir | The installation directory | Selected based on |
128| | | InstallAllUsers |
129+---------------------------+--------------------------------------+--------------------------+
130| DefaultAllUsersTargetDir | The default installation directory | :file:`%ProgramFiles%\\\ |
131| | for all-user installs | Python X.Y` or :file:`\ |
132| | | %ProgramFiles(x86)%\\\ |
133| | | Python X.Y` |
134+---------------------------+--------------------------------------+--------------------------+
135| DefaultJustForMeTargetDir | The default install directory for | :file:`%LocalAppData%\\\ |
136| | just-for-me installs | Programs\\PythonXY` or |
137| | | :file:`%LocalAppData%\\\ |
138| | | Programs\\PythonXY-32` |
139+---------------------------+--------------------------------------+--------------------------+
140| DefaultCustomTargetDir | The default custom install directory | (empty) |
141| | displayed in the UI | |
142+---------------------------+--------------------------------------+--------------------------+
143| AssociateFiles | Create file associations if the | 1 |
144| | launcher is also installed. | |
145+---------------------------+--------------------------------------+--------------------------+
146| CompileAll | Compile all ``.py`` files to | 0 |
Brett Cannonf299abd2015-04-13 14:21:02 -0400147| | ``.pyc``. | |
Steve Dowerbb240872015-02-05 22:08:48 -0800148+---------------------------+--------------------------------------+--------------------------+
149| PrependPath | Add install and Scripts directories | 0 |
Segev Finer8e7e8bd2018-07-30 20:11:30 +0300150| | to :envvar:`PATH` and ``.PY`` to | |
Steve Dowerbb240872015-02-05 22:08:48 -0800151| | :envvar:`PATHEXT` | |
152+---------------------------+--------------------------------------+--------------------------+
Steve Dower1d4880d2015-05-03 14:54:32 -0700153| Shortcuts | Create shortcuts for the interpreter,| 1 |
154| | documentation and IDLE if installed. | |
155+---------------------------+--------------------------------------+--------------------------+
Steve Dowerbb240872015-02-05 22:08:48 -0800156| Include_doc | Install Python manual | 1 |
157+---------------------------+--------------------------------------+--------------------------+
158| Include_debug | Install debug binaries | 0 |
159+---------------------------+--------------------------------------+--------------------------+
160| Include_dev | Install developer headers and | 1 |
161| | libraries | |
162+---------------------------+--------------------------------------+--------------------------+
163| Include_exe | Install :file:`python.exe` and | 1 |
164| | related files | |
165+---------------------------+--------------------------------------+--------------------------+
166| Include_launcher | Install :ref:`launcher`. | 1 |
167+---------------------------+--------------------------------------+--------------------------+
Steve Dower2237bdc2015-07-16 16:33:55 -0700168| InstallLauncherAllUsers | Installs :ref:`launcher` for all | 1 |
169| | users. | |
170+---------------------------+--------------------------------------+--------------------------+
Steve Dowerbb240872015-02-05 22:08:48 -0800171| Include_lib | Install standard library and | 1 |
172| | extension modules | |
173+---------------------------+--------------------------------------+--------------------------+
174| Include_pip | Install bundled pip and setuptools | 1 |
175+---------------------------+--------------------------------------+--------------------------+
176| Include_symbols | Install debugging symbols (`*`.pdb) | 0 |
177+---------------------------+--------------------------------------+--------------------------+
178| Include_tcltk | Install Tcl/Tk support and IDLE | 1 |
179+---------------------------+--------------------------------------+--------------------------+
180| Include_test | Install standard library test suite | 1 |
181+---------------------------+--------------------------------------+--------------------------+
182| Include_tools | Install utility scripts | 1 |
183+---------------------------+--------------------------------------+--------------------------+
Steve Dowera3d03ec2015-07-18 09:27:52 -0700184| LauncherOnly | Only installs the launcher. This | 0 |
185| | will override most other options. | |
186+---------------------------+--------------------------------------+--------------------------+
Steve Dowerbb240872015-02-05 22:08:48 -0800187| SimpleInstall | Disable most install UI | 0 |
188+---------------------------+--------------------------------------+--------------------------+
Steve Dower2237bdc2015-07-16 16:33:55 -0700189| SimpleInstallDescription | A custom message to display when the | (empty) |
190| | simplified install UI is used. | |
191+---------------------------+--------------------------------------+--------------------------+
Steve Dowerbb240872015-02-05 22:08:48 -0800192
193For example, to silently install a default, system-wide Python installation,
194you could use the following command (from an elevated command prompt)::
195
Steve Dower4db86bc2016-09-09 09:17:35 -0700196 python-3.6.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
Steve Dowerbb240872015-02-05 22:08:48 -0800197
198To allow users to easily install a personal copy of Python without the test
Steve Dower2237bdc2015-07-16 16:33:55 -0700199suite, you could provide a shortcut with the following command. This will
200display a simplified initial page and disallow customization::
Steve Dowerbb240872015-02-05 22:08:48 -0800201
Steve Dower4db86bc2016-09-09 09:17:35 -0700202 python-3.6.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
Steve Dower2237bdc2015-07-16 16:33:55 -0700203 SimpleInstall=1 SimpleInstallDescription="Just for me, no test suite."
Steve Dowerbb240872015-02-05 22:08:48 -0800204
205(Note that omitting the launcher also omits file associations, and is only
206recommended for per-user installs when there is also a system-wide installation
207that included the launcher.)
208
Steve Dower2434aa22015-07-18 09:28:19 -0700209The options listed above can also be provided in a file named ``unattend.xml``
210alongside the executable. This file specifies a list of options and values.
211When a value is provided as an attribute, it will be converted to a number if
212possible. Values provided as element text are always left as strings. This
Segev Finer8e7e8bd2018-07-30 20:11:30 +0300213example file sets the same options as the previous example:
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300214
215.. code-block:: xml
Steve Dower2434aa22015-07-18 09:28:19 -0700216
217 <Options>
218 <Option Name="InstallAllUsers" Value="no" />
219 <Option Name="Include_launcher" Value="0" />
220 <Option Name="Include_test" Value="no" />
221 <Option Name="SimpleInstall" Value="yes" />
222 <Option Name="SimpleInstallDescription">Just for me, no test suite</Option>
223 </Options>
224
Steve Dowerbb240872015-02-05 22:08:48 -0800225.. _install-layout-option:
226
227Installing Without Downloading
228------------------------------
229
230As some features of Python are not included in the initial installer download,
231selecting those features may require an internet connection. To avoid this
232need, all possible components may be downloaded on-demand to create a complete
233*layout* that will no longer require an internet connection regardless of the
234selected features. Note that this download may be bigger than required, but
235where a large number of installations are going to be performed it is very
236useful to have a locally cached copy.
237
238Execute the following command from Command Prompt to download all possible
Steve Dower4db86bc2016-09-09 09:17:35 -0700239required files. Remember to substitute ``python-3.6.0.exe`` for the actual
Steve Dowerbb240872015-02-05 22:08:48 -0800240name of your installer, and to create layouts in their own directories to
241avoid collisions between files with the same name.
242
243::
244
Steve Dower4db86bc2016-09-09 09:17:35 -0700245 python-3.6.0.exe /layout [optional target directory]
Steve Dowerbb240872015-02-05 22:08:48 -0800246
247You may also specify the ``/quiet`` option to hide the progress display.
248
Steve Dowere248f682016-01-16 11:58:30 -0800249Modifying an install
250--------------------
251
252Once Python has been installed, you can add or remove features through the
253Programs and Features tool that is part of Windows. Select the Python entry and
254choose "Uninstall/Change" to open the installer in maintenance mode.
255
256"Modify" allows you to add or remove features by modifying the checkboxes -
257unchanged checkboxes will not install or remove anything. Some options cannot be
258changed in this mode, such as the install directory; to modify these, you will
259need to remove and then reinstall Python completely.
260
261"Repair" will verify all the files that should be installed using the current
262settings and replace any that have been removed or modified.
263
264"Uninstall" will remove Python entirely, with the exception of the
265:ref:`launcher`, which has its own entry in Programs and Features.
Steve Dowerbb240872015-02-05 22:08:48 -0800266
267Other Platforms
268---------------
Christian Heimesd8654cf2007-12-02 15:22:16 +0000269
270With ongoing development of Python, some platforms that used to be supported
Christian Heimes380f7f22008-02-28 11:19:05 +0000271earlier are no longer supported (due to the lack of users or developers).
Christian Heimesd8654cf2007-12-02 15:22:16 +0000272Check :pep:`11` for details on all unsupported platforms.
273
Christian Heimesd8654cf2007-12-02 15:22:16 +0000274* `Windows CE <http://pythonce.sourceforge.net/>`_ is still supported.
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300275* The `Cygwin <https://cygwin.com/>`_ installer offers to install the Python
Georg Brandl525d3552014-10-29 10:26:56 +0100276 interpreter as well (cf. `Cygwin package source
Christian Heimesd8654cf2007-12-02 15:22:16 +0000277 <ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/
278 release/python>`_, `Maintainer releases
279 <http://www.tishler.net/jason/software/python/>`_)
280
Georg Brandl5d941342016-02-26 19:37:12 +0100281See `Python for Windows <https://www.python.org/downloads/windows/>`_
Zachary Warec3cf97b2014-01-17 15:23:42 -0600282for detailed information about platforms with pre-compiled installers.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000283
284.. seealso::
285
Georg Brandl5d941342016-02-26 19:37:12 +0100286 `Python on XP <http://dooling.com/index.php/2006/03/14/python-on-xp-7-minutes-to-hello-world/>`_
Christian Heimesd8654cf2007-12-02 15:22:16 +0000287 "7 Minutes to "Hello World!""
288 by Richard Dooling, 2006
289
Georg Brandl525d3552014-10-29 10:26:56 +0100290 `Installing on Windows <http://www.diveintopython.net/installing_python/windows.html>`_
Christian Heimesd8654cf2007-12-02 15:22:16 +0000291 in "`Dive into Python: Python from novice to pro
Georg Brandl525d3552014-10-29 10:26:56 +0100292 <http://www.diveintopython.net/>`_"
Christian Heimesd8654cf2007-12-02 15:22:16 +0000293 by Mark Pilgrim, 2004,
294 ISBN 1-59059-356-1
295
Sanyam Khurana1b4587a2017-12-06 22:09:33 +0530296 `For Windows users <https://python.swaroopch.com/installation.html#installation-on-windows>`_
Christian Heimesd8654cf2007-12-02 15:22:16 +0000297 in "Installing Python"
Sanyam Khurana1b4587a2017-12-06 22:09:33 +0530298 in "`A Byte of Python <https://python.swaroopch.com/>`_"
Christian Heimesd8654cf2007-12-02 15:22:16 +0000299 by Swaroop C H, 2003
300
301
302Alternative bundles
303===================
304
305Besides the standard CPython distribution, there are modified packages including
306additional functionality. The following is a list of popular versions and their
307key features:
308
Georg Brandl5d941342016-02-26 19:37:12 +0100309`ActivePython <https://www.activestate.com/activepython/>`_
Christian Heimesd8654cf2007-12-02 15:22:16 +0000310 Installer with multi-platform compatibility, documentation, PyWin32
311
Sanyam Khurana338cd832018-01-20 05:55:37 +0530312`Anaconda <https://www.anaconda.com/download/>`_
Steve Dowerbb240872015-02-05 22:08:48 -0800313 Popular scientific modules (such as numpy, scipy and pandas) and the
314 ``conda`` package manager.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000315
Sanyam Khurana338cd832018-01-20 05:55:37 +0530316`Canopy <https://www.enthought.com/product/canopy/>`_
Steve Dowerbb240872015-02-05 22:08:48 -0800317 A "comprehensive Python analysis environment" with editors and other
318 development tools.
319
320`WinPython <https://winpython.github.io/>`_
321 Windows-specific distribution with prebuilt scientific packages and
322 tools for building packages.
323
324Note that these packages may not include the latest versions of Python or
325other libraries, and are not maintained or supported by the core Python team.
Christian Heimes255f53b2007-12-08 15:33:56 +0000326
Christian Heimesd8654cf2007-12-02 15:22:16 +0000327
328
329Configuring Python
330==================
331
Steve Dowerbb240872015-02-05 22:08:48 -0800332To run Python conveniently from a command prompt, you might consider changing
333some default environment variables in Windows. While the installer provides an
334option to configure the PATH and PATHEXT variables for you, this is only
335reliable for a single, system-wide installation. If you regularly use multiple
336versions of Python, consider using the :ref:`launcher`.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000337
338
Brian Curtina86f1852012-08-19 11:22:20 -0500339.. _setting-envvars:
340
Christian Heimesd8654cf2007-12-02 15:22:16 +0000341Excursus: Setting environment variables
342---------------------------------------
343
Steve Dowerbb240872015-02-05 22:08:48 -0800344Windows allows environment variables to be configured permanently at both the
345User level and the System level, or temporarily in a command prompt.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000346
Steve Dowerbb240872015-02-05 22:08:48 -0800347To temporarily set environment variables, open Command Prompt and use the
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300348:command:`set` command:
349
350.. code-block:: doscon
Christian Heimesd8654cf2007-12-02 15:22:16 +0000351
Steve Dower4db86bc2016-09-09 09:17:35 -0700352 C:\>set PATH=C:\Program Files\Python 3.6;%PATH%
Steve Dowerbb240872015-02-05 22:08:48 -0800353 C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
354 C:\>python
Christian Heimesd8654cf2007-12-02 15:22:16 +0000355
Steve Dowerbb240872015-02-05 22:08:48 -0800356These changes will apply to any further commands executed in that console, and
357will be inherited by any applications started from the console.
358
359Including the variable name within percent signs will expand to the existing
360value, allowing you to add your new value at either the start or the end.
361Modifying :envvar:`PATH` by adding the directory containing
362:program:`python.exe` to the start is a common way to ensure the correct version
363of Python is launched.
364
365To permanently modify the default environment variables, click Start and search
366for 'edit environment variables', or open System properties, :guilabel:`Advanced
367system settings` and click the :guilabel:`Environment Variables` button.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000368In this dialog, you can add or modify User and System variables. To change
369System variables, you need non-restricted access to your machine
370(i.e. Administrator rights).
371
Steve Dowerbb240872015-02-05 22:08:48 -0800372.. note::
Christian Heimesd8654cf2007-12-02 15:22:16 +0000373
Steve Dowerbb240872015-02-05 22:08:48 -0800374 Windows will concatenate User variables *after* System variables, which may
375 cause unexpected results when modifying :envvar:`PATH`.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000376
Steve Dowerbb240872015-02-05 22:08:48 -0800377 The :envvar:`PYTHONPATH` variable is used by all versions of Python 2 and
378 Python 3, so you should not permanently configure this variable unless it
Steve Dower9a578d92015-05-02 22:28:58 -0700379 only includes code that is compatible with all of your installed Python
Steve Dowerbb240872015-02-05 22:08:48 -0800380 versions.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000381
382.. seealso::
383
HiyashiChuka46ebe612018-08-03 11:44:06 +0900384 https://www.microsoft.com/en-us/wdsi/help/folder-variables
Christian Heimesd8654cf2007-12-02 15:22:16 +0000385 Environment variables in Windows NT
386
Georg Brandl5d941342016-02-26 19:37:12 +0100387 https://technet.microsoft.com/en-us/library/cc754250.aspx
Steve Dowerbb240872015-02-05 22:08:48 -0800388 The SET command, for temporarily modifying environment variables
389
Georg Brandl5d941342016-02-26 19:37:12 +0100390 https://technet.microsoft.com/en-us/library/cc755104.aspx
Steve Dowerbb240872015-02-05 22:08:48 -0800391 The SETX command, for permanently modifying environment variables
392
Sanyam Khurana338cd832018-01-20 05:55:37 +0530393 https://support.microsoft.com/en-us/help/310519/how-to-manage-environment-variables-in-windows-xp
Christian Heimesd8654cf2007-12-02 15:22:16 +0000394 How To Manage Environment Variables in Windows XP
395
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300396 https://www.chem.gla.ac.uk/~louis/software/faq/q1.html
Christian Heimes255f53b2007-12-08 15:33:56 +0000397 Setting Environment variables, Louis J. Farrugia
398
Nick Coghlan349c8022012-09-30 13:00:43 +0530399.. _windows-path-mod:
400
Christian Heimesd8654cf2007-12-02 15:22:16 +0000401Finding the Python executable
402-----------------------------
403
Steve Dowerbb240872015-02-05 22:08:48 -0800404.. versionchanged:: 3.5
Brian Curtina86f1852012-08-19 11:22:20 -0500405
Christian Heimesd8654cf2007-12-02 15:22:16 +0000406Besides using the automatically created start menu entry for the Python
Steve Dowerbb240872015-02-05 22:08:48 -0800407interpreter, you might want to start Python in the command prompt. The
Steve Dowerf333fd82016-09-09 09:40:06 -0700408installer has an option to set that up for you.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000409
Steve Dower4db86bc2016-09-09 09:17:35 -0700410On the first page of the installer, an option labelled "Add Python to PATH"
411may be selected to have the installer add the install location into the
Steve Dowerbb240872015-02-05 22:08:48 -0800412:envvar:`PATH`. The location of the :file:`Scripts\\` folder is also added.
413This allows you to type :command:`python` to run the interpreter, and
Zachary Ware72e483a2016-08-04 12:13:47 -0500414:command:`pip` for the package installer. Thus, you can also execute your
Brian Curtina86f1852012-08-19 11:22:20 -0500415scripts with command line options, see :ref:`using-on-cmdline` documentation.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000416
Brian Curtina86f1852012-08-19 11:22:20 -0500417If you don't enable this option at install time, you can always re-run the
Steve Dowerbb240872015-02-05 22:08:48 -0800418installer, select Modify, and enable it. Alternatively, you can manually
419modify the :envvar:`PATH` using the directions in :ref:`setting-envvars`. You
420need to set your :envvar:`PATH` environment variable to include the directory
421of your Python installation, delimited by a semicolon from other entries. An
422example variable could look like this (assuming the first two entries already
423existed)::
Brian Curtina86f1852012-08-19 11:22:20 -0500424
Steve Dower4db86bc2016-09-09 09:17:35 -0700425 C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.6
Christian Heimesd8654cf2007-12-02 15:22:16 +0000426
Vinay Sajipd89dae12012-06-24 11:23:07 +0100427.. _launcher:
428
429Python Launcher for Windows
430===========================
431
432.. versionadded:: 3.3
433
Steve Dowerbb240872015-02-05 22:08:48 -0800434The Python launcher for Windows is a utility which aids in locating and
435executing of different Python versions. It allows scripts (or the
Vinay Sajipd89dae12012-06-24 11:23:07 +0100436command-line) to indicate a preference for a specific Python version, and
437will locate and execute that version.
438
Steve Dowerbb240872015-02-05 22:08:48 -0800439Unlike the :envvar:`PATH` variable, the launcher will correctly select the most
440appropriate version of Python. It will prefer per-user installations over
441system-wide ones, and orders by language version rather than using the most
442recently installed version.
443
Vinay Sajipd89dae12012-06-24 11:23:07 +0100444Getting started
445---------------
446
447From the command-line
448^^^^^^^^^^^^^^^^^^^^^
449
Paul Moore835416c2016-05-22 12:28:41 +0100450.. versionchanged:: 3.6
451
Steve Dowerbb240872015-02-05 22:08:48 -0800452System-wide installations of Python 3.3 and later will put the launcher on your
453:envvar:`PATH`. The launcher is compatible with all available versions of
454Python, so it does not matter which version is installed. To check that the
455launcher is available, execute the following command in Command Prompt:
Vinay Sajipd89dae12012-06-24 11:23:07 +0100456
457::
458
459 py
460
Paul Moore835416c2016-05-22 12:28:41 +0100461You should find that the latest version of Python you have installed is
Vinay Sajipd89dae12012-06-24 11:23:07 +0100462started - it can be exited as normal, and any additional command-line
463arguments specified will be sent directly to Python.
464
Steve Dowerf333fd82016-09-09 09:40:06 -0700465If you have multiple versions of Python installed (e.g., 2.7 and |version|) you
466will have noticed that Python |version| was started - to launch Python 2.7, try
467the command:
Vinay Sajipd89dae12012-06-24 11:23:07 +0100468
469::
470
Paul Moore835416c2016-05-22 12:28:41 +0100471 py -2.7
Vinay Sajipd89dae12012-06-24 11:23:07 +0100472
Paul Moore835416c2016-05-22 12:28:41 +0100473If you want the latest version of Python 2.x you have installed, try the
474command:
Vinay Sajipd89dae12012-06-24 11:23:07 +0100475
476::
477
Paul Moore835416c2016-05-22 12:28:41 +0100478 py -2
Vinay Sajipd89dae12012-06-24 11:23:07 +0100479
Paul Moore835416c2016-05-22 12:28:41 +0100480You should find the latest version of Python 2.x starts.
Vinay Sajipd89dae12012-06-24 11:23:07 +0100481
Steve Dowerbb240872015-02-05 22:08:48 -0800482If you see the following error, you do not have the launcher installed:
483
484::
485
486 'py' is not recognized as an internal or external command,
487 operable program or batch file.
488
489Per-user installations of Python do not add the launcher to :envvar:`PATH`
490unless the option was selected on installation.
491
Steve Dower76998fe2015-02-26 14:25:33 -0800492Virtual environments
493^^^^^^^^^^^^^^^^^^^^
494
Berker Peksagf2d4e572015-03-02 05:36:19 +0200495.. versionadded:: 3.5
496
Steve Dower76998fe2015-02-26 14:25:33 -0800497If the launcher is run with no explicit Python version specification, and a
498virtual environment (created with the standard library :mod:`venv` module or
499the external ``virtualenv`` tool) active, the launcher will run the virtual
500environment's interpreter rather than the global one. To run the global
501interpreter, either deactivate the virtual environment, or explicitly specify
502the global Python version.
503
Vinay Sajipd89dae12012-06-24 11:23:07 +0100504From a script
505^^^^^^^^^^^^^
506
507Let's create a test Python script - create a file called ``hello.py`` with the
508following contents
509
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300510.. code-block:: python
Vinay Sajipd89dae12012-06-24 11:23:07 +0100511
512 #! python
513 import sys
514 sys.stdout.write("hello from Python %s\n" % (sys.version,))
515
516From the directory in which hello.py lives, execute the command:
517
518::
519
520 py hello.py
521
522You should notice the version number of your latest Python 2.x installation
523is printed. Now try changing the first line to be:
524
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300525.. code-block:: python
Vinay Sajipd89dae12012-06-24 11:23:07 +0100526
527 #! python3
528
529Re-executing the command should now print the latest Python 3.x information.
530As with the above command-line examples, you can specify a more explicit
531version qualifier. Assuming you have Python 2.6 installed, try changing the
532first line to ``#! python2.6`` and you should find the 2.6 version
533information printed.
534
Paul Moore835416c2016-05-22 12:28:41 +0100535Note that unlike interactive use, a bare "python" will use the latest
536version of Python 2.x that you have installed. This is for backward
537compatibility and for compatibility with Unix, where the command ``python``
538typically refers to Python 2.
539
Vinay Sajipd89dae12012-06-24 11:23:07 +0100540From file associations
541^^^^^^^^^^^^^^^^^^^^^^
542
543The launcher should have been associated with Python files (i.e. ``.py``,
Brett Cannonf299abd2015-04-13 14:21:02 -0400544``.pyw``, ``.pyc`` files) when it was installed. This means that
Vinay Sajipd89dae12012-06-24 11:23:07 +0100545when you double-click on one of these files from Windows explorer the launcher
546will be used, and therefore you can use the same facilities described above to
547have the script specify the version which should be used.
548
549The key benefit of this is that a single launcher can support multiple Python
550versions at the same time depending on the contents of the first line.
551
552Shebang Lines
553-------------
554
555If the first line of a script file starts with ``#!``, it is known as a
556"shebang" line. Linux and other Unix like operating systems have native
Wieland Hoffmann80a3da42017-04-28 18:12:57 +0200557support for such lines and they are commonly used on such systems to indicate
558how a script should be executed. This launcher allows the same facilities to
559be used with Python scripts on Windows and the examples above demonstrate their
Vinay Sajipd89dae12012-06-24 11:23:07 +0100560use.
561
562To allow shebang lines in Python scripts to be portable between Unix and
563Windows, this launcher supports a number of 'virtual' commands to specify
564which interpreter to use. The supported virtual commands are:
565
566* ``/usr/bin/env python``
567* ``/usr/bin/python``
568* ``/usr/local/bin/python``
569* ``python``
570
571For example, if the first line of your script starts with
572
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300573.. code-block:: sh
Vinay Sajipd89dae12012-06-24 11:23:07 +0100574
575 #! /usr/bin/python
576
577The default Python will be located and used. As many Python scripts written
578to work on Unix will already have this line, you should find these scripts can
579be used by the launcher without modification. If you are writing a new script
580on Windows which you hope will be useful on Unix, you should use one of the
581shebang lines starting with ``/usr``.
582
Steve Dower76998fe2015-02-26 14:25:33 -0800583Any of the above virtual commands can be suffixed with an explicit version
584(either just the major version, or the major and minor version) - for example
585``/usr/bin/python2.7`` - which will cause that specific version to be located
586and used.
587
588The ``/usr/bin/env`` form of shebang line has one further special property.
589Before looking for installed Python interpreters, this form will search the
590executable :envvar:`PATH` for a Python executable. This corresponds to the
591behaviour of the Unix ``env`` program, which performs a :envvar:`PATH` search.
592
Vinay Sajipd89dae12012-06-24 11:23:07 +0100593Arguments in shebang lines
594--------------------------
595
596The shebang lines can also specify additional options to be passed to the
597Python interpreter. For example, if you have a shebang line:
598
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300599.. code-block:: sh
Vinay Sajipd89dae12012-06-24 11:23:07 +0100600
601 #! /usr/bin/python -v
602
603Then Python will be started with the ``-v`` option
604
605Customization
606-------------
607
608Customization via INI files
609^^^^^^^^^^^^^^^^^^^^^^^^^^^
610
Steve Dowerbb240872015-02-05 22:08:48 -0800611Two .ini files will be searched by the launcher - ``py.ini`` in the current
612user's "application data" directory (i.e. the directory returned by calling the
Segev Finer8e7e8bd2018-07-30 20:11:30 +0300613Windows function ``SHGetFolderPath`` with ``CSIDL_LOCAL_APPDATA``) and ``py.ini`` in the
Steve Dowerbb240872015-02-05 22:08:48 -0800614same directory as the launcher. The same .ini files are used for both the
615'console' version of the launcher (i.e. py.exe) and for the 'windows' version
616(i.e. pyw.exe)
Vinay Sajipd89dae12012-06-24 11:23:07 +0100617
Steve Dowerbb240872015-02-05 22:08:48 -0800618Customization specified in the "application directory" will have precedence over
619the one next to the executable, so a user, who may not have write access to the
620.ini file next to the launcher, can override commands in that global .ini file)
Vinay Sajipd89dae12012-06-24 11:23:07 +0100621
622Customizing default Python versions
623^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
624
625In some cases, a version qualifier can be included in a command to dictate
626which version of Python will be used by the command. A version qualifier
627starts with a major version number and can optionally be followed by a period
628('.') and a minor version specifier. If the minor qualifier is specified, it
629may optionally be followed by "-32" to indicate the 32-bit implementation of
630that version be used.
631
632For example, a shebang line of ``#!python`` has no version qualifier, while
633``#!python3`` has a version qualifier which specifies only a major version.
634
635If no version qualifiers are found in a command, the environment variable
636``PY_PYTHON`` can be set to specify the default version qualifier - the default
637value is "2". Note this value could specify just a major version (e.g. "2") or
638a major.minor qualifier (e.g. "2.6"), or even major.minor-32.
639
640If no minor version qualifiers are found, the environment variable
641``PY_PYTHON{major}`` (where ``{major}`` is the current major version qualifier
642as determined above) can be set to specify the full version. If no such option
643is found, the launcher will enumerate the installed Python versions and use
644the latest minor release found for the major version, which is likely,
645although not guaranteed, to be the most recently installed version in that
646family.
647
648On 64-bit Windows with both 32-bit and 64-bit implementations of the same
649(major.minor) Python version installed, the 64-bit version will always be
650preferred. This will be true for both 32-bit and 64-bit implementations of the
651launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation
652of the specified version if available. This is so the behavior of the launcher
653can be predicted knowing only what versions are installed on the PC and
654without regard to the order in which they were installed (i.e., without knowing
655whether a 32 or 64-bit version of Python and corresponding launcher was
656installed last). As noted above, an optional "-32" suffix can be used on a
657version specifier to change this behaviour.
658
659Examples:
660
661* If no relevant options are set, the commands ``python`` and
662 ``python2`` will use the latest Python 2.x version installed and
663 the command ``python3`` will use the latest Python 3.x installed.
664
665* The commands ``python3.1`` and ``python2.7`` will not consult any
666 options at all as the versions are fully specified.
667
668* If ``PY_PYTHON=3``, the commands ``python`` and ``python3`` will both use
669 the latest installed Python 3 version.
670
671* If ``PY_PYTHON=3.1-32``, the command ``python`` will use the 32-bit
672 implementation of 3.1 whereas the command ``python3`` will use the latest
673 installed Python (PY_PYTHON was not considered at all as a major
674 version was specified.)
675
676* If ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1``, the commands
677 ``python`` and ``python3`` will both use specifically 3.1
678
679In addition to environment variables, the same settings can be configured
680in the .INI file used by the launcher. The section in the INI file is
681called ``[defaults]`` and the key name will be the same as the
Ezio Melotti5b69fbd2012-10-19 20:40:18 +0300682environment variables without the leading ``PY_`` prefix (and note that
Vinay Sajipd89dae12012-06-24 11:23:07 +0100683the key names in the INI file are case insensitive.) The contents of
684an environment variable will override things specified in the INI file.
685
686For example:
687
688* Setting ``PY_PYTHON=3.1`` is equivalent to the INI file containing:
689
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300690.. code-block:: ini
Vinay Sajipd89dae12012-06-24 11:23:07 +0100691
692 [defaults]
693 python=3.1
694
695* Setting ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1`` is equivalent to the INI file
696 containing:
697
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300698.. code-block:: ini
Vinay Sajipd89dae12012-06-24 11:23:07 +0100699
700 [defaults]
701 python=3
702 python3=3.1
703
704Diagnostics
705-----------
706
707If an environment variable ``PYLAUNCH_DEBUG`` is set (to any value), the
708launcher will print diagnostic information to stderr (i.e. to the console).
709While this information manages to be simultaneously verbose *and* terse, it
710should allow you to see what versions of Python were located, why a
711particular version was chosen and the exact command-line used to execute the
712target Python.
713
714
Steve Dowerbb240872015-02-05 22:08:48 -0800715
Steve Dower3cdd7f52016-09-09 15:22:13 -0700716.. _finding_modules:
Steve Dowereb3c16d2015-08-08 09:09:01 -0700717
Steve Dowerbb240872015-02-05 22:08:48 -0800718Finding modules
719===============
720
721Python usually stores its library (and thereby your site-packages folder) in the
722installation directory. So, if you had installed Python to
723:file:`C:\\Python\\`, the default library would reside in
724:file:`C:\\Python\\Lib\\` and third-party modules should be stored in
725:file:`C:\\Python\\Lib\\site-packages\\`.
726
Steve Dowered51b262016-09-17 12:54:06 -0700727To completely override :data:`sys.path`, create a ``._pth`` file with the same
Steve Dower15ea3a62018-01-29 10:34:09 +1100728name as the DLL (``python37._pth``) or the executable (``python._pth``) and
Steve Dowered51b262016-09-17 12:54:06 -0700729specify one line for each path to add to :data:`sys.path`. The file based on the
730DLL name overrides the one based on the executable, which allows paths to be
731restricted for any program loading the runtime if desired.
Steve Dower4db86bc2016-09-09 09:17:35 -0700732
Steve Dowered51b262016-09-17 12:54:06 -0700733When the file exists, all registry and environment variables are ignored,
734isolated mode is enabled, and :mod:`site` is not imported unless one line in the
735file specifies ``import site``. Blank paths and lines starting with ``#`` are
736ignored. Each path may be absolute or relative to the location of the file.
737Import statements other than to ``site`` are not permitted, and arbitrary code
738cannot be specified.
739
740Note that ``.pth`` files (without leading underscore) will be processed normally
Steve Dower15ea3a62018-01-29 10:34:09 +1100741by the :mod:`site` module when ``import site`` has been specified.
Steve Dowered51b262016-09-17 12:54:06 -0700742
743When no ``._pth`` file is found, this is how :data:`sys.path` is populated on
744Windows:
Steve Dowerbb240872015-02-05 22:08:48 -0800745
746* An empty entry is added at the start, which corresponds to the current
747 directory.
748
749* If the environment variable :envvar:`PYTHONPATH` exists, as described in
750 :ref:`using-on-envvars`, its entries are added next. Note that on Windows,
751 paths in this variable must be separated by semicolons, to distinguish them
752 from the colon used in drive identifiers (``C:\`` etc.).
753
754* Additional "application paths" can be added in the registry as subkeys of
755 :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the
756 ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have
757 semicolon-delimited path strings as their default value will cause each path
758 to be added to :data:`sys.path`. (Note that all known installers only use
759 HKLM, so HKCU is typically empty.)
760
761* If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as
762 "Python Home". Otherwise, the path of the main Python executable is used to
Steve Dower814f9ae2016-09-09 15:07:46 -0700763 locate a "landmark file" (either ``Lib\os.py`` or ``pythonXY.zip``) to deduce
764 the "Python Home". If a Python home is found, the relevant sub-directories
765 added to :data:`sys.path` (``Lib``, ``plat-win``, etc) are based on that
766 folder. Otherwise, the core Python path is constructed from the PythonPath
767 stored in the registry.
Steve Dowerbb240872015-02-05 22:08:48 -0800768
769* If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in
770 the environment, and no registry entries can be found, a default path with
771 relative entries is used (e.g. ``.\Lib;.\plat-win``, etc).
772
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700773If a ``pyvenv.cfg`` file is found alongside the main executable or in the
774directory one level above the executable, the following variations apply:
775
776* If ``home`` is an absolute path and :envvar:`PYTHONHOME` is not set, this
777 path is used instead of the path to the main executable when deducing the
778 home location.
779
Steve Dowerbb240872015-02-05 22:08:48 -0800780The end result of all this is:
781
782* When running :file:`python.exe`, or any other .exe in the main Python
783 directory (either an installed version, or directly from the PCbuild
784 directory), the core path is deduced, and the core paths in the registry are
785 ignored. Other "application paths" in the registry are always read.
786
787* When Python is hosted in another .exe (different directory, embedded via COM,
788 etc), the "Python Home" will not be deduced, so the core path from the
789 registry is used. Other "application paths" in the registry are always read.
790
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700791* If Python can't find its home and there are no registry value (frozen .exe,
792 some very strange installation setup) you get a path with some default, but
Steve Dowerbb240872015-02-05 22:08:48 -0800793 relative, paths.
794
795For those who want to bundle Python into their application or distribution, the
796following advice will prevent conflicts with other installations:
797
Steve Dowered51b262016-09-17 12:54:06 -0700798* Include a ``._pth`` file alongside your executable containing the
799 directories to include. This will ignore paths listed in the registry and
800 environment variables, and also ignore :mod:`site` unless ``import site`` is
801 listed.
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700802
Steve Dower15ea3a62018-01-29 10:34:09 +1100803* If you are loading :file:`python3.dll` or :file:`python37.dll` in your own
Steve Dowerbb240872015-02-05 22:08:48 -0800804 executable, explicitly call :c:func:`Py_SetPath` or (at least)
805 :c:func:`Py_SetProgramName` before :c:func:`Py_Initialize`.
806
807* Clear and/or overwrite :envvar:`PYTHONPATH` and set :envvar:`PYTHONHOME`
808 before launching :file:`python.exe` from your application.
809
810* If you cannot use the previous suggestions (for example, you are a
811 distribution that allows people to run :file:`python.exe` directly), ensure
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700812 that the landmark file (:file:`Lib\\os.py`) exists in your install directory.
Steve Dower814f9ae2016-09-09 15:07:46 -0700813 (Note that it will not be detected inside a ZIP file, but a correctly named
814 ZIP file will be detected instead.)
Steve Dowerbb240872015-02-05 22:08:48 -0800815
816These will ensure that the files in a system-wide installation will not take
817precedence over the copy of the standard library bundled with your application.
Steve Dowereb3c16d2015-08-08 09:09:01 -0700818Otherwise, your users may experience problems using your application. Note that
Segev Finer8e7e8bd2018-07-30 20:11:30 +0300819the first suggestion is the best, as the others may still be susceptible to
Steve Dowereb3c16d2015-08-08 09:09:01 -0700820non-standard paths in the registry and user site-packages.
Steve Dowerbb240872015-02-05 22:08:48 -0800821
Steve Dower814f9ae2016-09-09 15:07:46 -0700822.. versionchanged::
823 3.6
Steve Dower4db86bc2016-09-09 09:17:35 -0700824
Victor Stinnerc6b1e152016-10-20 00:45:50 +0200825 * Adds ``._pth`` file support and removes ``applocal`` option from
826 ``pyvenv.cfg``.
827 * Adds ``pythonXX.zip`` as a potential landmark when directly adjacent
828 to the executable.
Steve Dower4db86bc2016-09-09 09:17:35 -0700829
Steve Dower20367422016-12-07 13:02:27 -0800830.. deprecated::
831 3.6
832
833 Modules specified in the registry under ``Modules`` (not ``PythonPath``)
834 may be imported by :class:`importlib.machinery.WindowsRegistryFinder`.
835 This finder is enabled on Windows in 3.6.0 and earlier, but may need to
836 be explicitly added to :attr:`sys.meta_path` in the future.
837
Christian Heimesd8654cf2007-12-02 15:22:16 +0000838Additional modules
839==================
840
841Even though Python aims to be portable among all platforms, there are features
842that are unique to Windows. A couple of modules, both in the standard library
843and external, and snippets exist to use these features.
844
845The Windows-specific standard modules are documented in
846:ref:`mswin-specific-services`.
847
Christian Heimesd8654cf2007-12-02 15:22:16 +0000848PyWin32
849-------
850
Stéphane Wirtel19177fb2018-05-15 20:58:35 +0200851The `PyWin32 <https://pypi.org/project/pywin32>`_ module by Mark Hammond
Christian Heimesd8654cf2007-12-02 15:22:16 +0000852is a collection of modules for advanced Windows-specific support. This includes
Georg Brandl2ee470f2008-07-16 12:55:28 +0000853utilities for:
Christian Heimesd8654cf2007-12-02 15:22:16 +0000854
Georg Brandl5d941342016-02-26 19:37:12 +0100855* `Component Object Model <https://www.microsoft.com/com/>`_ (COM)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000856* Win32 API calls
857* Registry
858* Event log
Georg Brandl5d941342016-02-26 19:37:12 +0100859* `Microsoft Foundation Classes <https://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx>`_ (MFC)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000860 user interfaces
861
Georg Brandl5d941342016-02-26 19:37:12 +0100862`PythonWin <https://web.archive.org/web/20060524042422/
Georg Brandle73778c2014-10-29 08:36:35 +0100863https://www.python.org/windows/pythonwin/>`_ is a sample MFC application
Christian Heimesd8654cf2007-12-02 15:22:16 +0000864shipped with PyWin32. It is an embeddable IDE with a built-in debugger.
865
866.. seealso::
867
868 `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_
869 by Tim Golden
870
871 `Python and COM <http://www.boddie.org.uk/python/COM.html>`_
872 by David and Paul Boddie
873
874
Zachary Warec3cf97b2014-01-17 15:23:42 -0600875cx_Freeze
876---------
Christian Heimesd8654cf2007-12-02 15:22:16 +0000877
Sanyam Khurana1b4587a2017-12-06 22:09:33 +0530878`cx_Freeze <https://anthony-tuininga.github.io/cx_Freeze/>`_ is a :mod:`distutils`
Zachary Warec3cf97b2014-01-17 15:23:42 -0600879extension (see :ref:`extending-distutils`) which wraps Python scripts into
880executable Windows programs (:file:`{*}.exe` files). When you have done this,
881you can distribute your application without requiring your users to install
882Python.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000883
884
885WConio
886------
887
888Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to
889Unix-like systems, there is a library exclusive to Windows as well: Windows
890Console I/O for Python.
891
892`WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for
893Turbo-C's :file:`CONIO.H`, used to create text user interfaces.
894
895
896
897Compiling Python on Windows
898===========================
899
900If you want to compile CPython yourself, first thing you should do is get the
Georg Brandl5d941342016-02-26 19:37:12 +0100901`source <https://www.python.org/downloads/source/>`_. You can download either the
Christian Heimesd8654cf2007-12-02 15:22:16 +0000902latest release's source or just grab a fresh `checkout
Lisa Hewus Fresh384899d2017-08-30 09:37:43 -0700903<https://devguide.python.org/setup/#getting-the-source-code>`_.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000904
Zachary Waref8ceb042013-11-30 16:59:33 -0600905The source tree contains a build solution and project files for Microsoft
Steve Dowerbb240872015-02-05 22:08:48 -0800906Visual Studio 2015, which is the compiler used to build the official Python
907releases. These files are in the :file:`PCbuild` directory.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000908
Zachary Waref8ceb042013-11-30 16:59:33 -0600909Check :file:`PCbuild/readme.txt` for general information on the build process.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000910
911
912For extension modules, consult :ref:`building-on-windows`.
913
914.. seealso::
915
916 `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_
917 or "Creating Python extensions in C/C++ with SWIG and compiling them with
918 MinGW gcc under Windows" or "Installing Python extension with distutils
919 and without Microsoft Visual C++" by Sébastien Sauvage, 2003
920
Georg Brandl495f7b52009-10-27 15:28:25 +0000921 `MingW -- Python extensions <http://oldwiki.mingw.org/index.php/Python%20extensions>`_
Christian Heimesd8654cf2007-12-02 15:22:16 +0000922 by Trent Apted et al, 2007
923
924
Steve Dowereb3c16d2015-08-08 09:09:01 -0700925Embedded Distribution
926=====================
927
928.. versionadded:: 3.5
929
930The embedded distribution is a ZIP file containing a minimal Python environment.
931It is intended for acting as part of another application, rather than being
932directly accessed by end-users.
933
934When extracted, the embedded distribution is (almost) fully isolated from the
935user's system, including environment variables, system registry settings, and
936installed packages. The standard library is included as pre-compiled and
Steve Dower15ea3a62018-01-29 10:34:09 +1100937optimized ``.pyc`` files in a ZIP, and ``python3.dll``, ``python37.dll``,
Steve Dowereb3c16d2015-08-08 09:09:01 -0700938``python.exe`` and ``pythonw.exe`` are all provided. Tcl/tk (including all
939dependants, such as Idle), pip and the Python documentation are not included.
940
941.. note::
942
943 The embedded distribution does not include the `Microsoft C Runtime
Georg Brandl5d941342016-02-26 19:37:12 +0100944 <https://www.microsoft.com/en-us/download/details.aspx?id=48145>`_ and it is
Steve Dowereb3c16d2015-08-08 09:09:01 -0700945 the responsibility of the application installer to provide this. The
946 runtime may have already been installed on a user's system previously or
947 automatically via Windows Update, and can be detected by finding
948 ``ucrtbase.dll`` in the system directory.
949
950Third-party packages should be installed by the application installer alongside
951the embedded distribution. Using pip to manage dependencies as for a regular
952Python installation is not supported with this distribution, though with some
953care it may be possible to include and use pip for automatic updates. In
954general, third-party packages should be treated as part of the application
955("vendoring") so that the developer can ensure compatibility with newer
956versions before providing updates to users.
957
958The two recommended use cases for this distribution are described below.
959
960Python Application
961------------------
962
963An application written in Python does not necessarily require users to be aware
964of that fact. The embedded distribution may be used in this case to include a
965private version of Python in an install package. Depending on how transparent it
966should be (or conversely, how professional it should appear), there are two
967options.
968
969Using a specialized executable as a launcher requires some coding, but provides
970the most transparent experience for users. With a customized launcher, there are
971no obvious indications that the program is running on Python: icons can be
972customized, company and version information can be specified, and file
973associations behave properly. In most cases, a custom launcher should simply be
974able to call ``Py_Main`` with a hard-coded command line.
975
976The simpler approach is to provide a batch file or generated shortcut that
977directly calls the ``python.exe`` or ``pythonw.exe`` with the required
978command-line arguments. In this case, the application will appear to be Python
979and not its actual name, and users may have trouble distinguishing it from other
980running Python processes or file associations.
981
982With the latter approach, packages should be installed as directories alongside
983the Python executable to ensure they are available on the path. With the
984specialized launcher, packages can be located in other locations as there is an
985opportunity to specify the search path before launching the application.
986
987Embedding Python
988----------------
989
990Applications written in native code often require some form of scripting
991language, and the embedded Python distribution can be used for this purpose. In
992general, the majority of the application is in native code, and some part will
993either invoke ``python.exe`` or directly use ``python3.dll``. For either case,
994extracting the embedded distribution to a subdirectory of the application
995installation is sufficient to provide a loadable Python interpreter.
996
997As with the application use, packages can be installed to any location as there
998is an opportunity to specify search paths before initializing the interpreter.
999Otherwise, there is no fundamental differences between using the embedded
1000distribution and a regular installation.
1001
Christian Heimesd8654cf2007-12-02 15:22:16 +00001002Other resources
1003===============
1004
1005.. seealso::
1006
Georg Brandlb7354a62014-10-29 10:57:37 +01001007 `Python Programming On Win32 <http://shop.oreilly.com/product/9781565926219.do>`_
Christian Heimesd8654cf2007-12-02 15:22:16 +00001008 "Help for Windows Programmers"
1009 by Mark Hammond and Andy Robinson, O'Reilly Media, 2000,
1010 ISBN 1-56592-621-8
1011
1012 `A Python for Windows Tutorial <http://www.imladris.com/Scripts/PythonForWindows.html>`_
1013 by Amanda Birmingham, 2004
1014
Vinay Sajipd89dae12012-06-24 11:23:07 +01001015 :pep:`397` - Python launcher for Windows
1016 The proposal for the launcher to be included in the Python distribution.