Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1 | .. highlightlang:: none |
| 2 | |
| 3 | .. _using-on-windows: |
| 4 | |
| 5 | ************************* |
| 6 | Using Python on Windows |
| 7 | ************************* |
| 8 | |
| 9 | .. sectionauthor:: Robert Lehmann <lehmannro@gmail.com> |
| 10 | |
| 11 | This document aims to give an overview of Windows-specific behaviour you should |
| 12 | know about when using Python on Microsoft Windows. |
| 13 | |
| 14 | |
| 15 | Installing Python |
| 16 | ================= |
| 17 | |
| 18 | Unlike most Unix systems and services, Windows does not require Python natively |
| 19 | and thus does not pre-install a version of Python. However, the CPython team |
| 20 | has compiled Windows installers (MSI packages) with every `release |
| 21 | <http://www.python.org/download/releases/>`_ for many years. |
| 22 | |
| 23 | With ongoing development of Python, some platforms that used to be supported |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 24 | earlier are no longer supported (due to the lack of users or developers). |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 25 | Check :pep:`11` for details on all unsupported platforms. |
| 26 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 27 | * Up to 2.5, Python was still compatible with Windows 95, 98 and ME (but already |
| 28 | raised a deprecation warning on installation). For Python 2.6 (and all |
| 29 | following releases), this support was dropped and new releases are just |
| 30 | expected to work on the Windows NT family. |
| 31 | * `Windows CE <http://pythonce.sourceforge.net/>`_ is still supported. |
| 32 | * The `Cygwin <http://cygwin.com/>`_ installer offers to install the `Python |
| 33 | interpreter <http://cygwin.com/packages/python>`_ as well; it is located under |
| 34 | "Interpreters." (cf. `Cygwin package source |
| 35 | <ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/ |
| 36 | release/python>`_, `Maintainer releases |
| 37 | <http://www.tishler.net/jason/software/python/>`_) |
| 38 | |
| 39 | See `Python for Windows (and DOS) <http://www.python.org/download/windows/>`_ |
| 40 | for detailed information about platforms with precompiled installers. |
| 41 | |
| 42 | .. seealso:: |
| 43 | |
| 44 | `Python on XP <http://www.richarddooling.com/index.php/2006/03/14/python-on-xp-7-minutes-to-hello-world/>`_ |
| 45 | "7 Minutes to "Hello World!"" |
| 46 | by Richard Dooling, 2006 |
| 47 | |
| 48 | `Installing on Windows <http://diveintopython.org/installing_python/windows.html>`_ |
| 49 | in "`Dive into Python: Python from novice to pro |
| 50 | <http://diveintopython.org/index.html>`_" |
| 51 | by Mark Pilgrim, 2004, |
| 52 | ISBN 1-59059-356-1 |
| 53 | |
| 54 | `For Windows users <http://swaroopch.com/text/Byte_of_Python:Installing_Python#For_Windows_users>`_ |
| 55 | in "Installing Python" |
| 56 | in "`A Byte of Python <http://www.byteofpython.info>`_" |
| 57 | by Swaroop C H, 2003 |
| 58 | |
| 59 | |
| 60 | Alternative bundles |
| 61 | =================== |
| 62 | |
| 63 | Besides the standard CPython distribution, there are modified packages including |
| 64 | additional functionality. The following is a list of popular versions and their |
| 65 | key features: |
| 66 | |
| 67 | `ActivePython <http://www.activestate.com/Products/activepython/>`_ |
| 68 | Installer with multi-platform compatibility, documentation, PyWin32 |
| 69 | |
| 70 | `Python Enthought Edition <http://code.enthought.com/enthon/>`_ |
| 71 | Popular modules (such as PyWin32) with their respective documentation, tool |
| 72 | suite for building extensible python applications |
| 73 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 74 | Notice that these packages are likely to install *older* versions of Python. |
| 75 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | Configuring Python |
| 79 | ================== |
| 80 | |
| 81 | In order to run Python flawlessly, you might have to change certain environment |
| 82 | settings in Windows. |
| 83 | |
| 84 | |
| 85 | Excursus: Setting environment variables |
| 86 | --------------------------------------- |
| 87 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 88 | Windows has a built-in dialog for changing environment variables (following |
| 89 | guide applies to XP classical view): Right-click the icon for your machine |
| 90 | (usually located on your Desktop and called "My Computer") and choose |
| 91 | :menuselection:`Properties` there. Then, open the :guilabel:`Advanced` tab |
| 92 | and click the :guilabel:`Environment Variables` button. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 93 | |
| 94 | In short, your path is: |
| 95 | |
| 96 | :menuselection:`My Computer |
| 97 | --> Properties |
| 98 | --> Advanced |
| 99 | --> Environment Variables` |
| 100 | |
| 101 | In this dialog, you can add or modify User and System variables. To change |
| 102 | System variables, you need non-restricted access to your machine |
| 103 | (i.e. Administrator rights). |
| 104 | |
| 105 | Another way of adding variables to your environment is using the :command:`set` |
| 106 | command:: |
| 107 | |
| 108 | set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib |
| 109 | |
| 110 | To make this setting permanent, you could add the corresponding command line to |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 111 | your :file:`autoexec.bat`. :program:`msconfig` is a graphical interface to this |
| 112 | file. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 113 | |
| 114 | Viewing environment variables can also be done more straight-forward: The |
| 115 | command prompt will expand strings wrapped into percent signs automatically:: |
| 116 | |
| 117 | echo %PATH% |
| 118 | |
| 119 | Consult :command:`set /?` for details on this behaviour. |
| 120 | |
| 121 | .. seealso:: |
| 122 | |
| 123 | http://support.microsoft.com/kb/100843 |
| 124 | Environment variables in Windows NT |
| 125 | |
| 126 | http://support.microsoft.com/kb/310519 |
| 127 | How To Manage Environment Variables in Windows XP |
| 128 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 129 | http://www.chem.gla.ac.uk/~louis/software/faq/q1.html |
| 130 | Setting Environment variables, Louis J. Farrugia |
| 131 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 132 | |
| 133 | Finding the Python executable |
| 134 | ----------------------------- |
| 135 | |
| 136 | Besides using the automatically created start menu entry for the Python |
| 137 | interpreter, you might want to start Python in the DOS prompt. To make this |
| 138 | work, you need to set your :envvar:`%PATH%` environment variable to include the |
| 139 | directory of your Python distribution, delimited by a semicolon from other |
| 140 | entries. An example variable could look like this (assuming the first two |
| 141 | entries are Windows' default):: |
| 142 | |
| 143 | C:\WINNT\system32;C:\WINNT;C:\Python25 |
| 144 | |
| 145 | Typing :command:`python` on your command prompt will now fire up the Python |
| 146 | interpreter. Thus, you can also execute your scripts with command line options, |
| 147 | see :ref:`using-on-cmdline` documentation. |
| 148 | |
| 149 | |
| 150 | Finding modules |
| 151 | --------------- |
| 152 | |
| 153 | Python usually stores its library (and thereby your site-packages folder) in the |
| 154 | installation directory. So, if you had installed Python to |
| 155 | :file:`C:\\Python\\`, the default library would reside in |
| 156 | :file:`C:\\Python\\Lib\\` and third-party modules should be stored in |
| 157 | :file:`C:\\Python\\Lib\\site-packages\\`. |
| 158 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 159 | .. `` this fixes syntax highlighting errors in some editors due to the \\ hackery |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 160 | |
| 161 | You can add folders to your search path to make Python's import mechanism search |
| 162 | in these directories as well. Use :envvar:`PYTHONPATH`, as described in |
| 163 | :ref:`using-on-envvars`, to modify :data:`sys.path`. On Windows, paths are |
| 164 | separated by semicolons, though, to distinguish them from drive identifiers |
| 165 | (:file:`C:\\` etc.). |
| 166 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 167 | .. `` |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 168 | |
| 169 | Modifying the module search path can also be done through the Windows registry: |
| 170 | Edit |
| 171 | :file:`HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath\\`, |
| 172 | as described above for the environment variable :envvar:`%PYTHONPATH%`. A |
| 173 | convenient registry editor is :program:`regedit` (start it by typing "regedit" |
| 174 | into :menuselection:`Start --> Run`). |
| 175 | |
| 176 | |
| 177 | Executing scripts |
| 178 | ----------------- |
| 179 | |
| 180 | Python scripts (files with the extension ``.py``) will be executed by |
| 181 | :program:`python.exe` by default. This executable opens a terminal, which stays |
| 182 | open even if the program uses a GUI. If you do not want this to happen, use the |
| 183 | extension ``.pyw`` which will cause the script to be executed by |
| 184 | :program:`pythonw.exe` by default (both executables are located in the top-level |
| 185 | of your Python installation directory). This suppresses the terminal window on |
| 186 | startup. |
| 187 | |
| 188 | You can also make all ``.py`` scripts execute with :program:`pythonw.exe`, |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 189 | setting this through the usual facilities, for example (might require |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 190 | administrative rights): |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 191 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 192 | #. Launch a command prompt. |
| 193 | #. Associate the correct file group with ``.py`` scripts:: |
| 194 | |
| 195 | assoc .py=Python.File |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 196 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 197 | #. Redirect all Python files to the new executable:: |
| 198 | |
| 199 | ftype Python.File=C:\Path\to\pythonw.exe "%1" %* |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | Additional modules |
| 203 | ================== |
| 204 | |
| 205 | Even though Python aims to be portable among all platforms, there are features |
| 206 | that are unique to Windows. A couple of modules, both in the standard library |
| 207 | and external, and snippets exist to use these features. |
| 208 | |
| 209 | The Windows-specific standard modules are documented in |
| 210 | :ref:`mswin-specific-services`. |
| 211 | |
| 212 | |
| 213 | PyWin32 |
| 214 | ------- |
| 215 | |
| 216 | The `PyWin32 <http://python.net/crew/mhammond/win32/>`_ module by Mark Hammond |
| 217 | is a collection of modules for advanced Windows-specific support. This includes |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 218 | utilities for: |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 219 | |
| 220 | * `Component Object Model <http://www.microsoft.com/com/>`_ (COM) |
| 221 | * Win32 API calls |
| 222 | * Registry |
| 223 | * Event log |
| 224 | * `Microsoft Foundation Classes <http://msdn.microsoft.com/library/ |
| 225 | en-us/vclib/html/_mfc_Class_Library_Reference_Introduction.asp>`_ (MFC) |
| 226 | user interfaces |
| 227 | |
| 228 | `PythonWin <http://web.archive.org/web/20060524042422/ |
| 229 | http://www.python.org/windows/pythonwin/>`_ is a sample MFC application |
| 230 | shipped with PyWin32. It is an embeddable IDE with a built-in debugger. |
| 231 | |
| 232 | .. seealso:: |
| 233 | |
| 234 | `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_ |
| 235 | by Tim Golden |
| 236 | |
| 237 | `Python and COM <http://www.boddie.org.uk/python/COM.html>`_ |
| 238 | by David and Paul Boddie |
| 239 | |
| 240 | |
| 241 | Py2exe |
| 242 | ------ |
| 243 | |
| 244 | `Py2exe <http://www.py2exe.org/>`_ is a :mod:`distutils` extension (see |
| 245 | :ref:`extending-distutils`) which wraps Python scripts into executable Windows |
| 246 | programs (:file:`{*}.exe` files). When you have done this, you can distribute |
| 247 | your application without requiring your users to install Python. |
| 248 | |
| 249 | |
| 250 | WConio |
| 251 | ------ |
| 252 | |
| 253 | Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to |
| 254 | Unix-like systems, there is a library exclusive to Windows as well: Windows |
| 255 | Console I/O for Python. |
| 256 | |
| 257 | `WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for |
| 258 | Turbo-C's :file:`CONIO.H`, used to create text user interfaces. |
| 259 | |
| 260 | |
| 261 | |
| 262 | Compiling Python on Windows |
| 263 | =========================== |
| 264 | |
| 265 | If you want to compile CPython yourself, first thing you should do is get the |
| 266 | `source <http://python.org/download/source/>`_. You can download either the |
| 267 | latest release's source or just grab a fresh `checkout |
Christian Heimes | 8b0facf | 2007-12-04 19:30:01 +0000 | [diff] [blame] | 268 | <http://www.python.org/dev/faq/#how-do-i-get-a-checkout-of-the-repository-read-only-and-read-write>`_. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 269 | |
| 270 | For Microsoft Visual C++, which is the compiler with which official Python |
| 271 | releases are built, the source tree contains solutions/project files. View the |
| 272 | :file:`readme.txt` in their respective directories: |
| 273 | |
| 274 | +--------------------+--------------+-----------------------+ |
| 275 | | Directory | MSVC version | Visual Studio version | |
| 276 | +====================+==============+=======================+ |
Christian Heimes | 2c18161 | 2007-12-17 20:04:13 +0000 | [diff] [blame] | 277 | | :file:`PC/VC6/` | 6.0 | 97 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 278 | +--------------------+--------------+-----------------------+ |
| 279 | | :file:`PCbuild/` | 7.1 | 2003 | |
| 280 | +--------------------+--------------+-----------------------+ |
| 281 | | :file:`PCbuild8/` | 8.0 | 2005 | |
| 282 | +--------------------+--------------+-----------------------+ |
| 283 | | :file:`PCbuild9/` | 9.0 | 2008 | |
| 284 | +--------------------+--------------+-----------------------+ |
| 285 | |
| 286 | Note that not all of these build directories are fully supported. Read the |
| 287 | release notes to see which compiler version the official releases for your |
| 288 | version are built with. |
| 289 | |
| 290 | Check :file:`PC/readme.txt` for general information on the build process. |
| 291 | |
| 292 | |
| 293 | For extension modules, consult :ref:`building-on-windows`. |
| 294 | |
| 295 | .. seealso:: |
| 296 | |
| 297 | `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_ |
| 298 | or "Creating Python extensions in C/C++ with SWIG and compiling them with |
| 299 | MinGW gcc under Windows" or "Installing Python extension with distutils |
| 300 | and without Microsoft Visual C++" by Sébastien Sauvage, 2003 |
| 301 | |
| 302 | `MingW -- Python extensions <http://www.mingw.org/MinGWiki/index.php/Python%20extensions>`_ |
| 303 | by Trent Apted et al, 2007 |
| 304 | |
| 305 | |
| 306 | Other resources |
| 307 | =============== |
| 308 | |
| 309 | .. seealso:: |
| 310 | |
| 311 | `Python Programming On Win32 <http://www.oreilly.com/catalog/pythonwin32/>`_ |
| 312 | "Help for Windows Programmers" |
| 313 | by Mark Hammond and Andy Robinson, O'Reilly Media, 2000, |
| 314 | ISBN 1-56592-621-8 |
| 315 | |
| 316 | `A Python for Windows Tutorial <http://www.imladris.com/Scripts/PythonForWindows.html>`_ |
| 317 | by Amanda Birmingham, 2004 |
| 318 | |