blob: 4911288404079cf734d2667cb10dff2b577887e5 [file] [log] [blame]
Georg Brandl87983f22007-12-01 23:12:45 +00001.. highlightlang:: none
2
3.. _using-on-windows:
4
5*************************
6 Using Python on Windows
7*************************
8
9.. sectionauthor:: Robert Lehmann <lehmannro@gmail.com>
10
11This document aims to give an overview of Windows-specific behaviour you should
12know about when using Python on Microsoft Windows.
13
14
15Installing Python
16=================
17
18Unlike most Unix systems and services, Windows does not require Python natively
19and thus does not pre-install a version of Python. However, the CPython team
20has compiled Windows installers (MSI packages) with every `release
Georg Brandl06f3b3b2014-10-29 08:36:35 +010021<https://www.python.org/download/releases/>`_ for many years.
Georg Brandl87983f22007-12-01 23:12:45 +000022
23With ongoing development of Python, some platforms that used to be supported
Georg Brandl3ef20632008-02-26 19:13:45 +000024earlier are no longer supported (due to the lack of users or developers).
Georg Brandl87983f22007-12-01 23:12:45 +000025Check :pep:`11` for details on all unsupported platforms.
26
27* DOS and Windows 3.x are deprecated since Python 2.0 and code specific to these
28 systems was removed in Python 2.1.
29* Up to 2.5, Python was still compatible with Windows 95, 98 and ME (but already
30 raised a deprecation warning on installation). For Python 2.6 (and all
31 following releases), this support was dropped and new releases are just
32 expected to work on the Windows NT family.
33* `Windows CE <http://pythonce.sourceforge.net/>`_ is still supported.
34* The `Cygwin <http://cygwin.com/>`_ installer offers to install the `Python
35 interpreter <http://cygwin.com/packages/python>`_ as well; it is located under
36 "Interpreters." (cf. `Cygwin package source
37 <ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/
38 release/python>`_, `Maintainer releases
39 <http://www.tishler.net/jason/software/python/>`_)
40
Georg Brandl06f3b3b2014-10-29 08:36:35 +010041See `Python for Windows (and DOS) <https://www.python.org/download/windows/>`_
Georg Brandl87983f22007-12-01 23:12:45 +000042for detailed information about platforms with precompiled installers.
43
44.. seealso::
45
46 `Python on XP <http://www.richarddooling.com/index.php/2006/03/14/python-on-xp-7-minutes-to-hello-world/>`_
47 "7 Minutes to "Hello World!""
48 by Richard Dooling, 2006
49
Sandro Tosic4114ea2011-10-31 17:45:09 +010050 `Installing on Windows <http://diveintopython.net/installing_python/windows.html>`_
Georg Brandl87983f22007-12-01 23:12:45 +000051 in "`Dive into Python: Python from novice to pro
Sandro Tosic4114ea2011-10-31 17:45:09 +010052 <http://diveintopython.net/index.html>`_"
Georg Brandl87983f22007-12-01 23:12:45 +000053 by Mark Pilgrim, 2004,
54 ISBN 1-59059-356-1
55
Georg Brandl8a91c5b2014-10-28 23:00:25 +010056 `For Windows users <http://www.swaroopch.com/notes/python/#install_windows>`_
Georg Brandl87983f22007-12-01 23:12:45 +000057 in "Installing Python"
Georg Brandl8a91c5b2014-10-28 23:00:25 +010058 in "`A Byte of Python <http://www.swaroopch.com/notes/python/>`_"
Georg Brandl87983f22007-12-01 23:12:45 +000059 by Swaroop C H, 2003
60
61
62Alternative bundles
63===================
64
65Besides the standard CPython distribution, there are modified packages including
66additional functionality. The following is a list of popular versions and their
67key features:
68
69`ActivePython <http://www.activestate.com/Products/activepython/>`_
70 Installer with multi-platform compatibility, documentation, PyWin32
71
Georg Brandla4314c22009-10-11 20:16:16 +000072`Enthought Python Distribution <http://www.enthought.com/products/epd.php>`_
Georg Brandl87983f22007-12-01 23:12:45 +000073 Popular modules (such as PyWin32) with their respective documentation, tool
Ezio Melotti062d2b52009-12-19 22:41:49 +000074 suite for building extensible Python applications
Georg Brandl87983f22007-12-01 23:12:45 +000075
Georg Brandl013d5742007-12-07 15:16:57 +000076Notice that these packages are likely to install *older* versions of Python.
77
Georg Brandl87983f22007-12-01 23:12:45 +000078
79
80Configuring Python
81==================
82
83In order to run Python flawlessly, you might have to change certain environment
84settings in Windows.
85
86
Georg Brandl8e1e8162012-12-22 10:44:11 +010087.. _setting-envvars:
88
Georg Brandl87983f22007-12-01 23:12:45 +000089Excursus: Setting environment variables
90---------------------------------------
91
Georg Brandl013d5742007-12-07 15:16:57 +000092Windows has a built-in dialog for changing environment variables (following
Georg Brandlc62ef8b2009-01-03 20:55:06 +000093guide applies to XP classical view): Right-click the icon for your machine
94(usually located on your Desktop and called "My Computer") and choose
95:menuselection:`Properties` there. Then, open the :guilabel:`Advanced` tab
Georg Brandl013d5742007-12-07 15:16:57 +000096and click the :guilabel:`Environment Variables` button.
Georg Brandl87983f22007-12-01 23:12:45 +000097
98In short, your path is:
99
100 :menuselection:`My Computer
101 --> Properties
102 --> Advanced
103 --> Environment Variables`
104
105In this dialog, you can add or modify User and System variables. To change
106System variables, you need non-restricted access to your machine
107(i.e. Administrator rights).
108
109Another way of adding variables to your environment is using the :command:`set`
110command::
111
112 set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
113
114To make this setting permanent, you could add the corresponding command line to
Georg Brandl013d5742007-12-07 15:16:57 +0000115your :file:`autoexec.bat`. :program:`msconfig` is a graphical interface to this
116file.
Georg Brandl87983f22007-12-01 23:12:45 +0000117
118Viewing environment variables can also be done more straight-forward: The
119command prompt will expand strings wrapped into percent signs automatically::
120
121 echo %PATH%
122
123Consult :command:`set /?` for details on this behaviour.
124
125.. seealso::
126
127 http://support.microsoft.com/kb/100843
128 Environment variables in Windows NT
129
130 http://support.microsoft.com/kb/310519
131 How To Manage Environment Variables in Windows XP
132
Georg Brandl013d5742007-12-07 15:16:57 +0000133 http://www.chem.gla.ac.uk/~louis/software/faq/q1.html
134 Setting Environment variables, Louis J. Farrugia
135
Georg Brandl87983f22007-12-01 23:12:45 +0000136
137Finding the Python executable
138-----------------------------
139
140Besides using the automatically created start menu entry for the Python
141interpreter, you might want to start Python in the DOS prompt. To make this
142work, you need to set your :envvar:`%PATH%` environment variable to include the
143directory of your Python distribution, delimited by a semicolon from other
144entries. An example variable could look like this (assuming the first two
145entries are Windows' default)::
146
Georg Brandl73ac29e2008-09-21 07:17:00 +0000147 C:\WINDOWS\system32;C:\WINDOWS;C:\Python25
Georg Brandl87983f22007-12-01 23:12:45 +0000148
149Typing :command:`python` on your command prompt will now fire up the Python
150interpreter. Thus, you can also execute your scripts with command line options,
151see :ref:`using-on-cmdline` documentation.
152
153
154Finding modules
155---------------
156
157Python usually stores its library (and thereby your site-packages folder) in the
158installation directory. So, if you had installed Python to
159:file:`C:\\Python\\`, the default library would reside in
160:file:`C:\\Python\\Lib\\` and third-party modules should be stored in
161:file:`C:\\Python\\Lib\\site-packages\\`.
162
Georg Brandl26946ec2010-11-26 07:42:15 +0000163This is how :data:`sys.path` is populated on Windows:
Georg Brandl87983f22007-12-01 23:12:45 +0000164
Georg Brandl26946ec2010-11-26 07:42:15 +0000165* An empty entry is added at the start, which corresponds to the current
166 directory.
Georg Brandl87983f22007-12-01 23:12:45 +0000167
Georg Brandl26946ec2010-11-26 07:42:15 +0000168* If the environment variable :envvar:`PYTHONPATH` exists, as described in
169 :ref:`using-on-envvars`, its entries are added next. Note that on Windows,
170 paths in this variable must be separated by semicolons, to distinguish them
171 from the colon used in drive identifiers (``C:\`` etc.).
Georg Brandl87983f22007-12-01 23:12:45 +0000172
Georg Brandl26946ec2010-11-26 07:42:15 +0000173* Additional "application paths" can be added in the registry as subkeys of
174 :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the
175 ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have
176 semicolon-delimited path strings as their default value will cause each path
177 to be added to :data:`sys.path`. (Note that all known installers only use
178 HKLM, so HKCU is typically empty.)
179
180* If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as
181 "Python Home". Otherwise, the path of the main Python executable is used to
182 locate a "landmark file" (``Lib\os.py``) to deduce the "Python Home". If a
183 Python home is found, the relevant sub-directories added to :data:`sys.path`
184 (``Lib``, ``plat-win``, etc) are based on that folder. Otherwise, the core
185 Python path is constructed from the PythonPath stored in the registry.
186
187* If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in
188 the environment, and no registry entries can be found, a default path with
189 relative entries is used (e.g. ``.\Lib;.\plat-win``, etc).
190
191The end result of all this is:
192
193* When running :file:`python.exe`, or any other .exe in the main Python
194 directory (either an installed version, or directly from the PCbuild
195 directory), the core path is deduced, and the core paths in the registry are
196 ignored. Other "application paths" in the registry are always read.
197
198* When Python is hosted in another .exe (different directory, embedded via COM,
199 etc), the "Python Home" will not be deduced, so the core path from the
200 registry is used. Other "application paths" in the registry are always read.
201
202* If Python can't find its home and there is no registry (eg, frozen .exe, some
203 very strange installation setup) you get a path with some default, but
204 relative, paths.
Georg Brandl87983f22007-12-01 23:12:45 +0000205
206
207Executing scripts
208-----------------
209
210Python scripts (files with the extension ``.py``) will be executed by
211:program:`python.exe` by default. This executable opens a terminal, which stays
212open even if the program uses a GUI. If you do not want this to happen, use the
213extension ``.pyw`` which will cause the script to be executed by
214:program:`pythonw.exe` by default (both executables are located in the top-level
215of your Python installation directory). This suppresses the terminal window on
216startup.
217
218You can also make all ``.py`` scripts execute with :program:`pythonw.exe`,
Benjamin Peterson90f36732008-07-12 20:16:19 +0000219setting this through the usual facilities, for example (might require
Georg Brandl8d101672008-01-08 19:42:30 +0000220administrative rights):
Georg Brandl87983f22007-12-01 23:12:45 +0000221
Georg Brandl8d101672008-01-08 19:42:30 +0000222#. Launch a command prompt.
223#. Associate the correct file group with ``.py`` scripts::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000224
Georg Brandl8d101672008-01-08 19:42:30 +0000225 assoc .py=Python.File
Georg Brandl87983f22007-12-01 23:12:45 +0000226
Georg Brandl8d101672008-01-08 19:42:30 +0000227#. Redirect all Python files to the new executable::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000228
Georg Brandl8d101672008-01-08 19:42:30 +0000229 ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
Georg Brandl87983f22007-12-01 23:12:45 +0000230
231
232Additional modules
233==================
234
235Even though Python aims to be portable among all platforms, there are features
236that are unique to Windows. A couple of modules, both in the standard library
237and external, and snippets exist to use these features.
238
239The Windows-specific standard modules are documented in
240:ref:`mswin-specific-services`.
241
242
243PyWin32
244-------
245
246The `PyWin32 <http://python.net/crew/mhammond/win32/>`_ module by Mark Hammond
247is a collection of modules for advanced Windows-specific support. This includes
Benjamin Peterson90f36732008-07-12 20:16:19 +0000248utilities for:
Georg Brandl87983f22007-12-01 23:12:45 +0000249
250* `Component Object Model <http://www.microsoft.com/com/>`_ (COM)
251* Win32 API calls
252* Registry
253* Event log
Georg Brandla4314c22009-10-11 20:16:16 +0000254* `Microsoft Foundation Classes <http://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx>`_ (MFC)
Georg Brandl87983f22007-12-01 23:12:45 +0000255 user interfaces
256
257`PythonWin <http://web.archive.org/web/20060524042422/
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100258https://www.python.org/windows/pythonwin/>`_ is a sample MFC application
Georg Brandl87983f22007-12-01 23:12:45 +0000259shipped with PyWin32. It is an embeddable IDE with a built-in debugger.
260
261.. seealso::
262
263 `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_
264 by Tim Golden
265
266 `Python and COM <http://www.boddie.org.uk/python/COM.html>`_
267 by David and Paul Boddie
268
269
270Py2exe
271------
272
273`Py2exe <http://www.py2exe.org/>`_ is a :mod:`distutils` extension (see
274:ref:`extending-distutils`) which wraps Python scripts into executable Windows
275programs (:file:`{*}.exe` files). When you have done this, you can distribute
276your application without requiring your users to install Python.
277
278
279WConio
280------
281
282Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to
283Unix-like systems, there is a library exclusive to Windows as well: Windows
284Console I/O for Python.
285
286`WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for
287Turbo-C's :file:`CONIO.H`, used to create text user interfaces.
288
289
290
291Compiling Python on Windows
292===========================
293
294If you want to compile CPython yourself, first thing you should do is get the
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100295`source <https://www.python.org/download/source/>`_. You can download either the
Georg Brandl87983f22007-12-01 23:12:45 +0000296latest release's source or just grab a fresh `checkout
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100297<https://docs.python.org/devguide/setup#checking-out-the-code>`_.
Georg Brandl87983f22007-12-01 23:12:45 +0000298
299For Microsoft Visual C++, which is the compiler with which official Python
300releases are built, the source tree contains solutions/project files. View the
301:file:`readme.txt` in their respective directories:
302
303+--------------------+--------------+-----------------------+
304| Directory | MSVC version | Visual Studio version |
305+====================+==============+=======================+
Georg Brandl001e8382007-12-16 23:11:16 +0000306| :file:`PC/VC6/` | 6.0 | 97 |
Georg Brandl87983f22007-12-01 23:12:45 +0000307+--------------------+--------------+-----------------------+
Georg Brandl73ac29e2008-09-21 07:17:00 +0000308| :file:`PC/VS7.1/` | 7.1 | 2003 |
Georg Brandl87983f22007-12-01 23:12:45 +0000309+--------------------+--------------+-----------------------+
Georg Brandl73ac29e2008-09-21 07:17:00 +0000310| :file:`PC/VS8.0/` | 8.0 | 2005 |
Georg Brandl87983f22007-12-01 23:12:45 +0000311+--------------------+--------------+-----------------------+
Georg Brandl73ac29e2008-09-21 07:17:00 +0000312| :file:`PCbuild/` | 9.0 | 2008 |
Georg Brandl87983f22007-12-01 23:12:45 +0000313+--------------------+--------------+-----------------------+
314
315Note that not all of these build directories are fully supported. Read the
316release notes to see which compiler version the official releases for your
317version are built with.
318
319Check :file:`PC/readme.txt` for general information on the build process.
320
321
322For extension modules, consult :ref:`building-on-windows`.
323
324.. seealso::
325
326 `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_
327 or "Creating Python extensions in C/C++ with SWIG and compiling them with
328 MinGW gcc under Windows" or "Installing Python extension with distutils
329 and without Microsoft Visual C++" by Sébastien Sauvage, 2003
330
Georg Brandla4314c22009-10-11 20:16:16 +0000331 `MingW -- Python extensions <http://oldwiki.mingw.org/index.php/Python%20extensions>`_
Georg Brandl87983f22007-12-01 23:12:45 +0000332 by Trent Apted et al, 2007
333
334
335Other resources
336===============
337
338.. seealso::
339
340 `Python Programming On Win32 <http://www.oreilly.com/catalog/pythonwin32/>`_
341 "Help for Windows Programmers"
342 by Mark Hammond and Andy Robinson, O'Reilly Media, 2000,
343 ISBN 1-56592-621-8
344
345 `A Python for Windows Tutorial <http://www.imladris.com/Scripts/PythonForWindows.html>`_
346 by Amanda Birmingham, 2004
347