Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +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 |
Georg Brandl | 3ef2063 | 2008-02-26 19:13:45 +0000 | [diff] [blame] | 24 | earlier are no longer supported (due to the lack of users or developers). |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 25 | Check :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 | |
| 41 | See `Python for Windows (and DOS) <http://www.python.org/download/windows/>`_ |
| 42 | for 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 Tosi | c4114ea | 2011-10-31 17:45:09 +0100 | [diff] [blame] | 50 | `Installing on Windows <http://diveintopython.net/installing_python/windows.html>`_ |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 51 | in "`Dive into Python: Python from novice to pro |
Sandro Tosi | c4114ea | 2011-10-31 17:45:09 +0100 | [diff] [blame] | 52 | <http://diveintopython.net/index.html>`_" |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 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 | |
| 62 | Alternative bundles |
| 63 | =================== |
| 64 | |
| 65 | Besides the standard CPython distribution, there are modified packages including |
| 66 | additional functionality. The following is a list of popular versions and their |
| 67 | key features: |
| 68 | |
| 69 | `ActivePython <http://www.activestate.com/Products/activepython/>`_ |
| 70 | Installer with multi-platform compatibility, documentation, PyWin32 |
| 71 | |
Georg Brandl | a4314c2 | 2009-10-11 20:16:16 +0000 | [diff] [blame] | 72 | `Enthought Python Distribution <http://www.enthought.com/products/epd.php>`_ |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 73 | Popular modules (such as PyWin32) with their respective documentation, tool |
Ezio Melotti | 062d2b5 | 2009-12-19 22:41:49 +0000 | [diff] [blame] | 74 | suite for building extensible Python applications |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 75 | |
Georg Brandl | 013d574 | 2007-12-07 15:16:57 +0000 | [diff] [blame] | 76 | Notice that these packages are likely to install *older* versions of Python. |
| 77 | |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | Configuring Python |
| 81 | ================== |
| 82 | |
| 83 | In order to run Python flawlessly, you might have to change certain environment |
| 84 | settings in Windows. |
| 85 | |
| 86 | |
| 87 | Excursus: Setting environment variables |
| 88 | --------------------------------------- |
| 89 | |
Georg Brandl | 013d574 | 2007-12-07 15:16:57 +0000 | [diff] [blame] | 90 | Windows has a built-in dialog for changing environment variables (following |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 91 | guide applies to XP classical view): Right-click the icon for your machine |
| 92 | (usually located on your Desktop and called "My Computer") and choose |
| 93 | :menuselection:`Properties` there. Then, open the :guilabel:`Advanced` tab |
Georg Brandl | 013d574 | 2007-12-07 15:16:57 +0000 | [diff] [blame] | 94 | and click the :guilabel:`Environment Variables` button. |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 95 | |
| 96 | In short, your path is: |
| 97 | |
| 98 | :menuselection:`My Computer |
| 99 | --> Properties |
| 100 | --> Advanced |
| 101 | --> Environment Variables` |
| 102 | |
| 103 | In this dialog, you can add or modify User and System variables. To change |
| 104 | System variables, you need non-restricted access to your machine |
| 105 | (i.e. Administrator rights). |
| 106 | |
| 107 | Another way of adding variables to your environment is using the :command:`set` |
| 108 | command:: |
| 109 | |
| 110 | set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib |
| 111 | |
| 112 | To make this setting permanent, you could add the corresponding command line to |
Georg Brandl | 013d574 | 2007-12-07 15:16:57 +0000 | [diff] [blame] | 113 | your :file:`autoexec.bat`. :program:`msconfig` is a graphical interface to this |
| 114 | file. |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 115 | |
| 116 | Viewing environment variables can also be done more straight-forward: The |
| 117 | command prompt will expand strings wrapped into percent signs automatically:: |
| 118 | |
| 119 | echo %PATH% |
| 120 | |
| 121 | Consult :command:`set /?` for details on this behaviour. |
| 122 | |
| 123 | .. seealso:: |
| 124 | |
| 125 | http://support.microsoft.com/kb/100843 |
| 126 | Environment variables in Windows NT |
| 127 | |
| 128 | http://support.microsoft.com/kb/310519 |
| 129 | How To Manage Environment Variables in Windows XP |
| 130 | |
Georg Brandl | 013d574 | 2007-12-07 15:16:57 +0000 | [diff] [blame] | 131 | http://www.chem.gla.ac.uk/~louis/software/faq/q1.html |
| 132 | Setting Environment variables, Louis J. Farrugia |
| 133 | |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 134 | |
| 135 | Finding the Python executable |
| 136 | ----------------------------- |
| 137 | |
| 138 | Besides using the automatically created start menu entry for the Python |
| 139 | interpreter, you might want to start Python in the DOS prompt. To make this |
| 140 | work, you need to set your :envvar:`%PATH%` environment variable to include the |
| 141 | directory of your Python distribution, delimited by a semicolon from other |
| 142 | entries. An example variable could look like this (assuming the first two |
| 143 | entries are Windows' default):: |
| 144 | |
Georg Brandl | 73ac29e | 2008-09-21 07:17:00 +0000 | [diff] [blame] | 145 | C:\WINDOWS\system32;C:\WINDOWS;C:\Python25 |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 146 | |
| 147 | Typing :command:`python` on your command prompt will now fire up the Python |
| 148 | interpreter. Thus, you can also execute your scripts with command line options, |
| 149 | see :ref:`using-on-cmdline` documentation. |
| 150 | |
| 151 | |
| 152 | Finding modules |
| 153 | --------------- |
| 154 | |
| 155 | Python usually stores its library (and thereby your site-packages folder) in the |
| 156 | installation directory. So, if you had installed Python to |
| 157 | :file:`C:\\Python\\`, the default library would reside in |
| 158 | :file:`C:\\Python\\Lib\\` and third-party modules should be stored in |
| 159 | :file:`C:\\Python\\Lib\\site-packages\\`. |
| 160 | |
Georg Brandl | 26946ec | 2010-11-26 07:42:15 +0000 | [diff] [blame] | 161 | This is how :data:`sys.path` is populated on Windows: |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 162 | |
Georg Brandl | 26946ec | 2010-11-26 07:42:15 +0000 | [diff] [blame] | 163 | * An empty entry is added at the start, which corresponds to the current |
| 164 | directory. |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 165 | |
Georg Brandl | 26946ec | 2010-11-26 07:42:15 +0000 | [diff] [blame] | 166 | * If the environment variable :envvar:`PYTHONPATH` exists, as described in |
| 167 | :ref:`using-on-envvars`, its entries are added next. Note that on Windows, |
| 168 | paths in this variable must be separated by semicolons, to distinguish them |
| 169 | from the colon used in drive identifiers (``C:\`` etc.). |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 170 | |
Georg Brandl | 26946ec | 2010-11-26 07:42:15 +0000 | [diff] [blame] | 171 | * Additional "application paths" can be added in the registry as subkeys of |
| 172 | :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the |
| 173 | ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have |
| 174 | semicolon-delimited path strings as their default value will cause each path |
| 175 | to be added to :data:`sys.path`. (Note that all known installers only use |
| 176 | HKLM, so HKCU is typically empty.) |
| 177 | |
| 178 | * If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as |
| 179 | "Python Home". Otherwise, the path of the main Python executable is used to |
| 180 | locate a "landmark file" (``Lib\os.py``) to deduce the "Python Home". If a |
| 181 | Python home is found, the relevant sub-directories added to :data:`sys.path` |
| 182 | (``Lib``, ``plat-win``, etc) are based on that folder. Otherwise, the core |
| 183 | Python path is constructed from the PythonPath stored in the registry. |
| 184 | |
| 185 | * If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in |
| 186 | the environment, and no registry entries can be found, a default path with |
| 187 | relative entries is used (e.g. ``.\Lib;.\plat-win``, etc). |
| 188 | |
| 189 | The end result of all this is: |
| 190 | |
| 191 | * When running :file:`python.exe`, or any other .exe in the main Python |
| 192 | directory (either an installed version, or directly from the PCbuild |
| 193 | directory), the core path is deduced, and the core paths in the registry are |
| 194 | ignored. Other "application paths" in the registry are always read. |
| 195 | |
| 196 | * When Python is hosted in another .exe (different directory, embedded via COM, |
| 197 | etc), the "Python Home" will not be deduced, so the core path from the |
| 198 | registry is used. Other "application paths" in the registry are always read. |
| 199 | |
| 200 | * If Python can't find its home and there is no registry (eg, frozen .exe, some |
| 201 | very strange installation setup) you get a path with some default, but |
| 202 | relative, paths. |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 203 | |
| 204 | |
| 205 | Executing scripts |
| 206 | ----------------- |
| 207 | |
| 208 | Python scripts (files with the extension ``.py``) will be executed by |
| 209 | :program:`python.exe` by default. This executable opens a terminal, which stays |
| 210 | open even if the program uses a GUI. If you do not want this to happen, use the |
| 211 | extension ``.pyw`` which will cause the script to be executed by |
| 212 | :program:`pythonw.exe` by default (both executables are located in the top-level |
| 213 | of your Python installation directory). This suppresses the terminal window on |
| 214 | startup. |
| 215 | |
| 216 | You can also make all ``.py`` scripts execute with :program:`pythonw.exe`, |
Benjamin Peterson | 90f3673 | 2008-07-12 20:16:19 +0000 | [diff] [blame] | 217 | setting this through the usual facilities, for example (might require |
Georg Brandl | 8d10167 | 2008-01-08 19:42:30 +0000 | [diff] [blame] | 218 | administrative rights): |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 219 | |
Georg Brandl | 8d10167 | 2008-01-08 19:42:30 +0000 | [diff] [blame] | 220 | #. Launch a command prompt. |
| 221 | #. Associate the correct file group with ``.py`` scripts:: |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 222 | |
Georg Brandl | 8d10167 | 2008-01-08 19:42:30 +0000 | [diff] [blame] | 223 | assoc .py=Python.File |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 224 | |
Georg Brandl | 8d10167 | 2008-01-08 19:42:30 +0000 | [diff] [blame] | 225 | #. Redirect all Python files to the new executable:: |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 226 | |
Georg Brandl | 8d10167 | 2008-01-08 19:42:30 +0000 | [diff] [blame] | 227 | ftype Python.File=C:\Path\to\pythonw.exe "%1" %* |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 228 | |
| 229 | |
| 230 | Additional modules |
| 231 | ================== |
| 232 | |
| 233 | Even though Python aims to be portable among all platforms, there are features |
| 234 | that are unique to Windows. A couple of modules, both in the standard library |
| 235 | and external, and snippets exist to use these features. |
| 236 | |
| 237 | The Windows-specific standard modules are documented in |
| 238 | :ref:`mswin-specific-services`. |
| 239 | |
| 240 | |
| 241 | PyWin32 |
| 242 | ------- |
| 243 | |
| 244 | The `PyWin32 <http://python.net/crew/mhammond/win32/>`_ module by Mark Hammond |
| 245 | is a collection of modules for advanced Windows-specific support. This includes |
Benjamin Peterson | 90f3673 | 2008-07-12 20:16:19 +0000 | [diff] [blame] | 246 | utilities for: |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 247 | |
| 248 | * `Component Object Model <http://www.microsoft.com/com/>`_ (COM) |
| 249 | * Win32 API calls |
| 250 | * Registry |
| 251 | * Event log |
Georg Brandl | a4314c2 | 2009-10-11 20:16:16 +0000 | [diff] [blame] | 252 | * `Microsoft Foundation Classes <http://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx>`_ (MFC) |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 253 | user interfaces |
| 254 | |
| 255 | `PythonWin <http://web.archive.org/web/20060524042422/ |
| 256 | http://www.python.org/windows/pythonwin/>`_ is a sample MFC application |
| 257 | shipped with PyWin32. It is an embeddable IDE with a built-in debugger. |
| 258 | |
| 259 | .. seealso:: |
| 260 | |
| 261 | `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_ |
| 262 | by Tim Golden |
| 263 | |
| 264 | `Python and COM <http://www.boddie.org.uk/python/COM.html>`_ |
| 265 | by David and Paul Boddie |
| 266 | |
| 267 | |
| 268 | Py2exe |
| 269 | ------ |
| 270 | |
| 271 | `Py2exe <http://www.py2exe.org/>`_ is a :mod:`distutils` extension (see |
| 272 | :ref:`extending-distutils`) which wraps Python scripts into executable Windows |
| 273 | programs (:file:`{*}.exe` files). When you have done this, you can distribute |
| 274 | your application without requiring your users to install Python. |
| 275 | |
| 276 | |
| 277 | WConio |
| 278 | ------ |
| 279 | |
| 280 | Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to |
| 281 | Unix-like systems, there is a library exclusive to Windows as well: Windows |
| 282 | Console I/O for Python. |
| 283 | |
| 284 | `WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for |
| 285 | Turbo-C's :file:`CONIO.H`, used to create text user interfaces. |
| 286 | |
| 287 | |
| 288 | |
| 289 | Compiling Python on Windows |
| 290 | =========================== |
| 291 | |
| 292 | If you want to compile CPython yourself, first thing you should do is get the |
| 293 | `source <http://python.org/download/source/>`_. You can download either the |
| 294 | latest release's source or just grab a fresh `checkout |
Éric Araujo | 9bcbc62 | 2011-10-14 18:05:56 +0200 | [diff] [blame] | 295 | <http://docs.python.org/devguide/setup#checking-out-the-code>`_. |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 296 | |
| 297 | For Microsoft Visual C++, which is the compiler with which official Python |
| 298 | releases are built, the source tree contains solutions/project files. View the |
| 299 | :file:`readme.txt` in their respective directories: |
| 300 | |
| 301 | +--------------------+--------------+-----------------------+ |
| 302 | | Directory | MSVC version | Visual Studio version | |
| 303 | +====================+==============+=======================+ |
Georg Brandl | 001e838 | 2007-12-16 23:11:16 +0000 | [diff] [blame] | 304 | | :file:`PC/VC6/` | 6.0 | 97 | |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 305 | +--------------------+--------------+-----------------------+ |
Georg Brandl | 73ac29e | 2008-09-21 07:17:00 +0000 | [diff] [blame] | 306 | | :file:`PC/VS7.1/` | 7.1 | 2003 | |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 307 | +--------------------+--------------+-----------------------+ |
Georg Brandl | 73ac29e | 2008-09-21 07:17:00 +0000 | [diff] [blame] | 308 | | :file:`PC/VS8.0/` | 8.0 | 2005 | |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 309 | +--------------------+--------------+-----------------------+ |
Georg Brandl | 73ac29e | 2008-09-21 07:17:00 +0000 | [diff] [blame] | 310 | | :file:`PCbuild/` | 9.0 | 2008 | |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 311 | +--------------------+--------------+-----------------------+ |
| 312 | |
| 313 | Note that not all of these build directories are fully supported. Read the |
| 314 | release notes to see which compiler version the official releases for your |
| 315 | version are built with. |
| 316 | |
| 317 | Check :file:`PC/readme.txt` for general information on the build process. |
| 318 | |
| 319 | |
| 320 | For extension modules, consult :ref:`building-on-windows`. |
| 321 | |
| 322 | .. seealso:: |
| 323 | |
| 324 | `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_ |
| 325 | or "Creating Python extensions in C/C++ with SWIG and compiling them with |
| 326 | MinGW gcc under Windows" or "Installing Python extension with distutils |
| 327 | and without Microsoft Visual C++" by Sébastien Sauvage, 2003 |
| 328 | |
Georg Brandl | a4314c2 | 2009-10-11 20:16:16 +0000 | [diff] [blame] | 329 | `MingW -- Python extensions <http://oldwiki.mingw.org/index.php/Python%20extensions>`_ |
Georg Brandl | 87983f2 | 2007-12-01 23:12:45 +0000 | [diff] [blame] | 330 | by Trent Apted et al, 2007 |
| 331 | |
| 332 | |
| 333 | Other resources |
| 334 | =============== |
| 335 | |
| 336 | .. seealso:: |
| 337 | |
| 338 | `Python Programming On Win32 <http://www.oreilly.com/catalog/pythonwin32/>`_ |
| 339 | "Help for Windows Programmers" |
| 340 | by Mark Hammond and Andy Robinson, O'Reilly Media, 2000, |
| 341 | ISBN 1-56592-621-8 |
| 342 | |
| 343 | `A Python for Windows Tutorial <http://www.imladris.com/Scripts/PythonForWindows.html>`_ |
| 344 | by Amanda Birmingham, 2004 |
| 345 | |