blob: db836057b71cb11e1d5174786f23dff9f8f5a699 [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>
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
21<http://www.python.org/download/releases/>`_ for many years.
22
23With ongoing development of Python, some platforms that used to be supported
24earlier are not longer supported (due to the lack of users or developers).
25Check :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
41See `Python for Windows (and DOS) <http://www.python.org/download/windows/>`_
42for 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
50 `Installing on Windows <http://diveintopython.org/installing_python/windows.html>`_
51 in "`Dive into Python: Python from novice to pro
52 <http://diveintopython.org/index.html>`_"
53 by Mark Pilgrim, 2004,
54 ISBN 1-59059-356-1
55
56 `For Windows users <http://swaroopch.com/text/Byte_of_Python:Installing_Python#For_Windows_users>`_
57 in "Installing Python"
58 in "`A Byte of Python <http://www.byteofpython.info>`_"
59 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
72`Python Enthought Edition <http://code.enthought.com/enthon/>`_
73 Popular modules (such as PyWin32) with their respective documentation, tool
74 suite for building extensible python applications
75
76
77
78Configuring Python
79==================
80
81In order to run Python flawlessly, you might have to change certain environment
82settings in Windows.
83
84
85Excursus: Setting environment variables
86---------------------------------------
87
88Windows has a built-in dialog for changing environment variables: Right-click
89the icon for your machine (usually located on your Desktop and called "My
90Computer") and choose :menuselection:`Properties` there. Then, open the
91:guilabel:`Advanced` tab and click the :guilabel:`Environment Variables` button.
92
93In short, your path is:
94
95 :menuselection:`My Computer
96 --> Properties
97 --> Advanced
98 --> Environment Variables`
99
100In this dialog, you can add or modify User and System variables. To change
101System variables, you need non-restricted access to your machine
102(i.e. Administrator rights).
103
104Another way of adding variables to your environment is using the :command:`set`
105command::
106
107 set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
108
109To make this setting permanent, you could add the corresponding command line to
110your :file:`autoexec.bat`.
111
112Viewing environment variables can also be done more straight-forward: The
113command prompt will expand strings wrapped into percent signs automatically::
114
115 echo %PATH%
116
117Consult :command:`set /?` for details on this behaviour.
118
119.. seealso::
120
121 http://support.microsoft.com/kb/100843
122 Environment variables in Windows NT
123
124 http://support.microsoft.com/kb/310519
125 How To Manage Environment Variables in Windows XP
126
127
128Finding the Python executable
129-----------------------------
130
131Besides using the automatically created start menu entry for the Python
132interpreter, you might want to start Python in the DOS prompt. To make this
133work, you need to set your :envvar:`%PATH%` environment variable to include the
134directory of your Python distribution, delimited by a semicolon from other
135entries. An example variable could look like this (assuming the first two
136entries are Windows' default)::
137
138 C:\WINNT\system32;C:\WINNT;C:\Python25
139
140Typing :command:`python` on your command prompt will now fire up the Python
141interpreter. Thus, you can also execute your scripts with command line options,
142see :ref:`using-on-cmdline` documentation.
143
144
145Finding modules
146---------------
147
148Python usually stores its library (and thereby your site-packages folder) in the
149installation directory. So, if you had installed Python to
150:file:`C:\\Python\\`, the default library would reside in
151:file:`C:\\Python\\Lib\\` and third-party modules should be stored in
152:file:`C:\\Python\\Lib\\site-packages\\`.
153
154.. % `` this fixes syntax highlighting errors in some editors
155 due to the \\ hackery
156
157You can add folders to your search path to make Python's import mechanism search
158in these directories as well. Use :envvar:`PYTHONPATH`, as described in
159:ref:`using-on-envvars`, to modify :data:`sys.path`. On Windows, paths are
160separated by semicolons, though, to distinguish them from drive identifiers
161(:file:`C:\\` etc.).
162
163.. % ``
164
165Modifying the module search path can also be done through the Windows registry:
166Edit
167:file:`HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath\\`,
168as described above for the environment variable :envvar:`%PYTHONPATH%`. A
169convenient registry editor is :program:`regedit` (start it by typing "regedit"
170into :menuselection:`Start --> Run`).
171
172
173Executing scripts
174-----------------
175
176Python scripts (files with the extension ``.py``) will be executed by
177:program:`python.exe` by default. This executable opens a terminal, which stays
178open even if the program uses a GUI. If you do not want this to happen, use the
179extension ``.pyw`` which will cause the script to be executed by
180:program:`pythonw.exe` by default (both executables are located in the top-level
181of your Python installation directory). This suppresses the terminal window on
182startup.
183
184You can also make all ``.py`` scripts execute with :program:`pythonw.exe`,
185setting this through the usual facilites, for example (names might differ,
186depending on your version of Windows):
187
188#. Open the context menu of a :file:`{*}.py` file.
189#. Click :menuselection:`Open with...`.
190#. Choose the interpreter of your choice (utilize :guilabel:`Other...` or
191 :guilabel:`Choose Program...` if it is not in the list of default programs).
192#. Check :guilabel:`Always open files with this program`.
193#. Click :guilabel:`OK`.
194
195
196
197Additional modules
198==================
199
200Even though Python aims to be portable among all platforms, there are features
201that are unique to Windows. A couple of modules, both in the standard library
202and external, and snippets exist to use these features.
203
204The Windows-specific standard modules are documented in
205:ref:`mswin-specific-services`.
206
207
208PyWin32
209-------
210
211The `PyWin32 <http://python.net/crew/mhammond/win32/>`_ module by Mark Hammond
212is a collection of modules for advanced Windows-specific support. This includes
213utilites for:
214
215* `Component Object Model <http://www.microsoft.com/com/>`_ (COM)
216* Win32 API calls
217* Registry
218* Event log
219* `Microsoft Foundation Classes <http://msdn.microsoft.com/library/
220 en-us/vclib/html/_mfc_Class_Library_Reference_Introduction.asp>`_ (MFC)
221 user interfaces
222
223`PythonWin <http://web.archive.org/web/20060524042422/
224http://www.python.org/windows/pythonwin/>`_ is a sample MFC application
225shipped with PyWin32. It is an embeddable IDE with a built-in debugger.
226
227.. seealso::
228
229 `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_
230 by Tim Golden
231
232 `Python and COM <http://www.boddie.org.uk/python/COM.html>`_
233 by David and Paul Boddie
234
235
236Py2exe
237------
238
239`Py2exe <http://www.py2exe.org/>`_ is a :mod:`distutils` extension (see
240:ref:`extending-distutils`) which wraps Python scripts into executable Windows
241programs (:file:`{*}.exe` files). When you have done this, you can distribute
242your application without requiring your users to install Python.
243
244
245WConio
246------
247
248Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to
249Unix-like systems, there is a library exclusive to Windows as well: Windows
250Console I/O for Python.
251
252`WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for
253Turbo-C's :file:`CONIO.H`, used to create text user interfaces.
254
255
256
257Compiling Python on Windows
258===========================
259
260If you want to compile CPython yourself, first thing you should do is get the
261`source <http://python.org/download/source/>`_. You can download either the
262latest release's source or just grab a fresh `checkout
263<http://www.python.org/dev/faq/
264#how-do-i-get-a-checkout-of-the-repository-read-only-and-read-write>`_.
265
266For Microsoft Visual C++, which is the compiler with which official Python
267releases are built, the source tree contains solutions/project files. View the
268:file:`readme.txt` in their respective directories:
269
270+--------------------+--------------+-----------------------+
271| Directory | MSVC version | Visual Studio version |
272+====================+==============+=======================+
273| :file:`PC/VC6/` | 5.0 | 97 |
274| +--------------+-----------------------+
275| | 6.0 | 6.0 |
276+--------------------+--------------+-----------------------+
277| :file:`PCbuild/` | 7.1 | 2003 |
278+--------------------+--------------+-----------------------+
279| :file:`PCbuild8/` | 8.0 | 2005 |
280+--------------------+--------------+-----------------------+
281| :file:`PCbuild9/` | 9.0 | 2008 |
282+--------------------+--------------+-----------------------+
283
284Note that not all of these build directories are fully supported. Read the
285release notes to see which compiler version the official releases for your
286version are built with.
287
288Check :file:`PC/readme.txt` for general information on the build process.
289
290
291For extension modules, consult :ref:`building-on-windows`.
292
293.. seealso::
294
295 `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_
296 or "Creating Python extensions in C/C++ with SWIG and compiling them with
297 MinGW gcc under Windows" or "Installing Python extension with distutils
298 and without Microsoft Visual C++" by Sébastien Sauvage, 2003
299
300 `MingW -- Python extensions <http://www.mingw.org/MinGWiki/index.php/Python%20extensions>`_
301 by Trent Apted et al, 2007
302
303
304Other resources
305===============
306
307.. seealso::
308
309 `Python Programming On Win32 <http://www.oreilly.com/catalog/pythonwin32/>`_
310 "Help for Windows Programmers"
311 by Mark Hammond and Andy Robinson, O'Reilly Media, 2000,
312 ISBN 1-56592-621-8
313
314 `A Python for Windows Tutorial <http://www.imladris.com/Scripts/PythonForWindows.html>`_
315 by Amanda Birmingham, 2004
316