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 | |
Sandro Tosi | 53bcd66 | 2011-10-31 17:46:04 +0100 | [diff] [blame] | 48 | `Installing on Windows <http://diveintopython.net/installing_python/windows.html>`_ |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 49 | in "`Dive into Python: Python from novice to pro |
Sandro Tosi | 53bcd66 | 2011-10-31 17:46:04 +0100 | [diff] [blame] | 50 | <http://diveintopython.net/index.html>`_" |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 70 | `Enthought Python Distribution <http://www.enthought.com/products/epd.php>`_ |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 71 | Popular modules (such as PyWin32) with their respective documentation, tool |
Ezio Melotti | 0639d5a | 2009-12-19 23:26:38 +0000 | [diff] [blame] | 72 | suite for building extensible Python applications |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 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 | |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 85 | .. _setting-envvars: |
| 86 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 87 | Excursus: Setting environment variables |
| 88 | --------------------------------------- |
| 89 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 90 | Windows has a built-in dialog for changing environment variables (following |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +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 |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 94 | and click the :guilabel:`Environment Variables` button. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +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 |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 113 | your :file:`autoexec.bat`. :program:`msconfig` is a graphical interface to this |
| 114 | file. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +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 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 131 | http://www.chem.gla.ac.uk/~louis/software/faq/q1.html |
| 132 | Setting Environment variables, Louis J. Farrugia |
| 133 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 134 | |
| 135 | Finding the Python executable |
| 136 | ----------------------------- |
| 137 | |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 138 | .. versionchanged:: 3.3 |
| 139 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 140 | Besides using the automatically created start menu entry for the Python |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 141 | interpreter, you might want to start Python in the command prompt. As of |
| 142 | Python 3.3, the installer has an option to set that up for you. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 143 | |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 144 | At the "Customize Python 3.3" screen, an option called |
| 145 | "Add python.exe to search path" can be enabled to have the installer place |
| 146 | your installation into the :envvar:`%PATH%`. This allows you to type |
| 147 | :command:`python` to run the interpreter. Thus, you can also execute your |
| 148 | scripts with command line options, see :ref:`using-on-cmdline` documentation. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 149 | |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 150 | If you don't enable this option at install time, you can always re-run the |
| 151 | installer to choose it. |
| 152 | |
| 153 | The alternative is manually modifying the :envvar:`%PATH%` using the |
| 154 | directions in :ref:`setting-envvars`. You need to set your :envvar:`%PATH%` |
| 155 | environment variable to include the directory of your Python distribution, |
| 156 | delimited by a semicolon from other entries. An example variable could look |
| 157 | like this (assuming the first two entries are Windows' default):: |
| 158 | |
| 159 | C:\WINDOWS\system32;C:\WINDOWS;C:\Python33 |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | Finding modules |
| 163 | --------------- |
| 164 | |
| 165 | Python usually stores its library (and thereby your site-packages folder) in the |
| 166 | installation directory. So, if you had installed Python to |
| 167 | :file:`C:\\Python\\`, the default library would reside in |
| 168 | :file:`C:\\Python\\Lib\\` and third-party modules should be stored in |
| 169 | :file:`C:\\Python\\Lib\\site-packages\\`. |
| 170 | |
Georg Brandl | 7306ad5 | 2010-10-17 10:05:13 +0000 | [diff] [blame] | 171 | This is how :data:`sys.path` is populated on Windows: |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 172 | |
Georg Brandl | 7306ad5 | 2010-10-17 10:05:13 +0000 | [diff] [blame] | 173 | * An empty entry is added at the start, which corresponds to the current |
| 174 | directory. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 175 | |
Georg Brandl | 7306ad5 | 2010-10-17 10:05:13 +0000 | [diff] [blame] | 176 | * If the environment variable :envvar:`PYTHONPATH` exists, as described in |
| 177 | :ref:`using-on-envvars`, its entries are added next. Note that on Windows, |
| 178 | paths in this variable must be separated by semicolons, to distinguish them |
| 179 | from the colon used in drive identifiers (``C:\`` etc.). |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 180 | |
Georg Brandl | 7306ad5 | 2010-10-17 10:05:13 +0000 | [diff] [blame] | 181 | * Additional "application paths" can be added in the registry as subkeys of |
| 182 | :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the |
| 183 | ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have |
| 184 | semicolon-delimited path strings as their default value will cause each path |
| 185 | to be added to :data:`sys.path`. (Note that all known installers only use |
| 186 | HKLM, so HKCU is typically empty.) |
| 187 | |
| 188 | * If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as |
| 189 | "Python Home". Otherwise, the path of the main Python executable is used to |
| 190 | locate a "landmark file" (``Lib\os.py``) to deduce the "Python Home". If a |
| 191 | Python home is found, the relevant sub-directories added to :data:`sys.path` |
| 192 | (``Lib``, ``plat-win``, etc) are based on that folder. Otherwise, the core |
| 193 | Python path is constructed from the PythonPath stored in the registry. |
| 194 | |
| 195 | * If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in |
| 196 | the environment, and no registry entries can be found, a default path with |
| 197 | relative entries is used (e.g. ``.\Lib;.\plat-win``, etc). |
| 198 | |
| 199 | The end result of all this is: |
| 200 | |
| 201 | * When running :file:`python.exe`, or any other .exe in the main Python |
| 202 | directory (either an installed version, or directly from the PCbuild |
| 203 | directory), the core path is deduced, and the core paths in the registry are |
| 204 | ignored. Other "application paths" in the registry are always read. |
| 205 | |
| 206 | * When Python is hosted in another .exe (different directory, embedded via COM, |
| 207 | etc), the "Python Home" will not be deduced, so the core path from the |
| 208 | registry is used. Other "application paths" in the registry are always read. |
| 209 | |
| 210 | * If Python can't find its home and there is no registry (eg, frozen .exe, some |
| 211 | very strange installation setup) you get a path with some default, but |
| 212 | relative, paths. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 213 | |
| 214 | |
| 215 | Executing scripts |
| 216 | ----------------- |
| 217 | |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 218 | As of Python 3.3, Python includes a launcher which facilitates running Python |
| 219 | scripts. See :ref:`launcher` for more information. |
| 220 | |
| 221 | Executing scripts without the Python launcher |
| 222 | --------------------------------------------- |
| 223 | |
| 224 | Without the Python launcher installed, Python scripts (files with the extension |
| 225 | ``.py``) will be executed by :program:`python.exe` by default. This executable |
| 226 | opens a terminal, which stays open even if the program uses a GUI. If you do |
| 227 | not want this to happen, use the extension ``.pyw`` which will cause the script |
| 228 | to be executed by :program:`pythonw.exe` by default (both executables are |
| 229 | located in the top-level of your Python installation directory). This |
| 230 | suppresses the terminal window on startup. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 231 | |
| 232 | You can also make all ``.py`` scripts execute with :program:`pythonw.exe`, |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 233 | setting this through the usual facilities, for example (might require |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 234 | administrative rights): |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 235 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 236 | #. Launch a command prompt. |
| 237 | #. Associate the correct file group with ``.py`` scripts:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 238 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 239 | assoc .py=Python.File |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 240 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 241 | #. Redirect all Python files to the new executable:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 242 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 243 | ftype Python.File=C:\Path\to\pythonw.exe "%1" %* |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 244 | |
| 245 | |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 246 | .. _launcher: |
| 247 | |
| 248 | Python Launcher for Windows |
| 249 | =========================== |
| 250 | |
| 251 | .. versionadded:: 3.3 |
| 252 | |
| 253 | The Python launcher for Windows is a utility which aids in the location and |
| 254 | execution of different Python versions. It allows scripts (or the |
| 255 | command-line) to indicate a preference for a specific Python version, and |
| 256 | will locate and execute that version. |
| 257 | |
| 258 | Getting started |
| 259 | --------------- |
| 260 | |
| 261 | From the command-line |
| 262 | ^^^^^^^^^^^^^^^^^^^^^ |
| 263 | |
| 264 | You should ensure the launcher is on your PATH - depending on how it was |
| 265 | installed it may already be there, but check just in case it is not. |
| 266 | |
| 267 | From a command-prompt, execute the following command: |
| 268 | |
| 269 | :: |
| 270 | |
| 271 | py |
| 272 | |
| 273 | You should find that the latest version of Python 2.x you have installed is |
| 274 | started - it can be exited as normal, and any additional command-line |
| 275 | arguments specified will be sent directly to Python. |
| 276 | |
| 277 | If you have multiple versions of Python 2.x installed (e.g., 2.6 and 2.7) you |
| 278 | will have noticed that Python 2.7 was started - to launch Python 2.6, try the |
| 279 | command: |
| 280 | |
| 281 | :: |
| 282 | |
| 283 | py -2.6 |
| 284 | |
| 285 | If you have a Python 3.x installed, try the command: |
| 286 | |
| 287 | :: |
| 288 | |
| 289 | py -3 |
| 290 | |
| 291 | You should find the latest version of Python 3.x starts. |
| 292 | |
| 293 | From a script |
| 294 | ^^^^^^^^^^^^^ |
| 295 | |
| 296 | Let's create a test Python script - create a file called ``hello.py`` with the |
| 297 | following contents |
| 298 | |
| 299 | :: |
| 300 | |
| 301 | #! python |
| 302 | import sys |
| 303 | sys.stdout.write("hello from Python %s\n" % (sys.version,)) |
| 304 | |
| 305 | From the directory in which hello.py lives, execute the command: |
| 306 | |
| 307 | :: |
| 308 | |
| 309 | py hello.py |
| 310 | |
| 311 | You should notice the version number of your latest Python 2.x installation |
| 312 | is printed. Now try changing the first line to be: |
| 313 | |
| 314 | :: |
| 315 | |
| 316 | #! python3 |
| 317 | |
| 318 | Re-executing the command should now print the latest Python 3.x information. |
| 319 | As with the above command-line examples, you can specify a more explicit |
| 320 | version qualifier. Assuming you have Python 2.6 installed, try changing the |
| 321 | first line to ``#! python2.6`` and you should find the 2.6 version |
| 322 | information printed. |
| 323 | |
| 324 | From file associations |
| 325 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 326 | |
| 327 | The launcher should have been associated with Python files (i.e. ``.py``, |
| 328 | ``.pyw``, ``.pyc``, ``.pyo`` files) when it was installed. This means that |
| 329 | when you double-click on one of these files from Windows explorer the launcher |
| 330 | will be used, and therefore you can use the same facilities described above to |
| 331 | have the script specify the version which should be used. |
| 332 | |
| 333 | The key benefit of this is that a single launcher can support multiple Python |
| 334 | versions at the same time depending on the contents of the first line. |
| 335 | |
| 336 | Shebang Lines |
| 337 | ------------- |
| 338 | |
| 339 | If the first line of a script file starts with ``#!``, it is known as a |
| 340 | "shebang" line. Linux and other Unix like operating systems have native |
| 341 | support for such lines and are commonly used on such systems to indicate how |
| 342 | a script should be executed. This launcher allows the same facilities to be |
| 343 | using with Python scripts on Windows and the examples above demonstrate their |
| 344 | use. |
| 345 | |
| 346 | To allow shebang lines in Python scripts to be portable between Unix and |
| 347 | Windows, this launcher supports a number of 'virtual' commands to specify |
| 348 | which interpreter to use. The supported virtual commands are: |
| 349 | |
| 350 | * ``/usr/bin/env python`` |
| 351 | * ``/usr/bin/python`` |
| 352 | * ``/usr/local/bin/python`` |
| 353 | * ``python`` |
| 354 | |
| 355 | For example, if the first line of your script starts with |
| 356 | |
| 357 | :: |
| 358 | |
| 359 | #! /usr/bin/python |
| 360 | |
| 361 | The default Python will be located and used. As many Python scripts written |
| 362 | to work on Unix will already have this line, you should find these scripts can |
| 363 | be used by the launcher without modification. If you are writing a new script |
| 364 | on Windows which you hope will be useful on Unix, you should use one of the |
| 365 | shebang lines starting with ``/usr``. |
| 366 | |
| 367 | Arguments in shebang lines |
| 368 | -------------------------- |
| 369 | |
| 370 | The shebang lines can also specify additional options to be passed to the |
| 371 | Python interpreter. For example, if you have a shebang line: |
| 372 | |
| 373 | :: |
| 374 | |
| 375 | #! /usr/bin/python -v |
| 376 | |
| 377 | Then Python will be started with the ``-v`` option |
| 378 | |
| 379 | Customization |
| 380 | ------------- |
| 381 | |
| 382 | Customization via INI files |
| 383 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 384 | |
| 385 | Two .ini files will be searched by the launcher - ``py.ini`` in the |
| 386 | current user's "application data" directory (i.e. the directory returned |
| 387 | by calling the Windows function SHGetFolderPath with CSIDL_LOCAL_APPDATA) |
| 388 | and ``py.ini`` in the same directory as the launcher. The same .ini |
| 389 | files are used for both the 'console' version of the launcher (i.e. |
| 390 | py.exe) and for the 'windows' version (i.e. pyw.exe) |
| 391 | |
| 392 | Customization specified in the "application directory" will have |
| 393 | precedence over the one next to the executable, so a user, who may not |
| 394 | have write access to the .ini file next to the launcher, can override |
| 395 | commands in that global .ini file) |
| 396 | |
| 397 | Customizing default Python versions |
| 398 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 399 | |
| 400 | In some cases, a version qualifier can be included in a command to dictate |
| 401 | which version of Python will be used by the command. A version qualifier |
| 402 | starts with a major version number and can optionally be followed by a period |
| 403 | ('.') and a minor version specifier. If the minor qualifier is specified, it |
| 404 | may optionally be followed by "-32" to indicate the 32-bit implementation of |
| 405 | that version be used. |
| 406 | |
| 407 | For example, a shebang line of ``#!python`` has no version qualifier, while |
| 408 | ``#!python3`` has a version qualifier which specifies only a major version. |
| 409 | |
| 410 | If no version qualifiers are found in a command, the environment variable |
| 411 | ``PY_PYTHON`` can be set to specify the default version qualifier - the default |
| 412 | value is "2". Note this value could specify just a major version (e.g. "2") or |
| 413 | a major.minor qualifier (e.g. "2.6"), or even major.minor-32. |
| 414 | |
| 415 | If no minor version qualifiers are found, the environment variable |
| 416 | ``PY_PYTHON{major}`` (where ``{major}`` is the current major version qualifier |
| 417 | as determined above) can be set to specify the full version. If no such option |
| 418 | is found, the launcher will enumerate the installed Python versions and use |
| 419 | the latest minor release found for the major version, which is likely, |
| 420 | although not guaranteed, to be the most recently installed version in that |
| 421 | family. |
| 422 | |
| 423 | On 64-bit Windows with both 32-bit and 64-bit implementations of the same |
| 424 | (major.minor) Python version installed, the 64-bit version will always be |
| 425 | preferred. This will be true for both 32-bit and 64-bit implementations of the |
| 426 | launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation |
| 427 | of the specified version if available. This is so the behavior of the launcher |
| 428 | can be predicted knowing only what versions are installed on the PC and |
| 429 | without regard to the order in which they were installed (i.e., without knowing |
| 430 | whether a 32 or 64-bit version of Python and corresponding launcher was |
| 431 | installed last). As noted above, an optional "-32" suffix can be used on a |
| 432 | version specifier to change this behaviour. |
| 433 | |
| 434 | Examples: |
| 435 | |
| 436 | * If no relevant options are set, the commands ``python`` and |
| 437 | ``python2`` will use the latest Python 2.x version installed and |
| 438 | the command ``python3`` will use the latest Python 3.x installed. |
| 439 | |
| 440 | * The commands ``python3.1`` and ``python2.7`` will not consult any |
| 441 | options at all as the versions are fully specified. |
| 442 | |
| 443 | * If ``PY_PYTHON=3``, the commands ``python`` and ``python3`` will both use |
| 444 | the latest installed Python 3 version. |
| 445 | |
| 446 | * If ``PY_PYTHON=3.1-32``, the command ``python`` will use the 32-bit |
| 447 | implementation of 3.1 whereas the command ``python3`` will use the latest |
| 448 | installed Python (PY_PYTHON was not considered at all as a major |
| 449 | version was specified.) |
| 450 | |
| 451 | * If ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1``, the commands |
| 452 | ``python`` and ``python3`` will both use specifically 3.1 |
| 453 | |
| 454 | In addition to environment variables, the same settings can be configured |
| 455 | in the .INI file used by the launcher. The section in the INI file is |
| 456 | called ``[defaults]`` and the key name will be the same as the |
| 457 | environment variables without the leading ``PY\_`` prefix (and note that |
| 458 | the key names in the INI file are case insensitive.) The contents of |
| 459 | an environment variable will override things specified in the INI file. |
| 460 | |
| 461 | For example: |
| 462 | |
| 463 | * Setting ``PY_PYTHON=3.1`` is equivalent to the INI file containing: |
| 464 | |
| 465 | :: |
| 466 | |
| 467 | [defaults] |
| 468 | python=3.1 |
| 469 | |
| 470 | * Setting ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1`` is equivalent to the INI file |
| 471 | containing: |
| 472 | |
| 473 | :: |
| 474 | |
| 475 | [defaults] |
| 476 | python=3 |
| 477 | python3=3.1 |
| 478 | |
| 479 | Diagnostics |
| 480 | ----------- |
| 481 | |
| 482 | If an environment variable ``PYLAUNCH_DEBUG`` is set (to any value), the |
| 483 | launcher will print diagnostic information to stderr (i.e. to the console). |
| 484 | While this information manages to be simultaneously verbose *and* terse, it |
| 485 | should allow you to see what versions of Python were located, why a |
| 486 | particular version was chosen and the exact command-line used to execute the |
| 487 | target Python. |
| 488 | |
| 489 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 490 | Additional modules |
| 491 | ================== |
| 492 | |
| 493 | Even though Python aims to be portable among all platforms, there are features |
| 494 | that are unique to Windows. A couple of modules, both in the standard library |
| 495 | and external, and snippets exist to use these features. |
| 496 | |
| 497 | The Windows-specific standard modules are documented in |
| 498 | :ref:`mswin-specific-services`. |
| 499 | |
| 500 | |
| 501 | PyWin32 |
| 502 | ------- |
| 503 | |
| 504 | The `PyWin32 <http://python.net/crew/mhammond/win32/>`_ module by Mark Hammond |
| 505 | is a collection of modules for advanced Windows-specific support. This includes |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 506 | utilities for: |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 507 | |
| 508 | * `Component Object Model <http://www.microsoft.com/com/>`_ (COM) |
| 509 | * Win32 API calls |
| 510 | * Registry |
| 511 | * Event log |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 512 | * `Microsoft Foundation Classes <http://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx>`_ (MFC) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 513 | user interfaces |
| 514 | |
| 515 | `PythonWin <http://web.archive.org/web/20060524042422/ |
| 516 | http://www.python.org/windows/pythonwin/>`_ is a sample MFC application |
| 517 | shipped with PyWin32. It is an embeddable IDE with a built-in debugger. |
| 518 | |
| 519 | .. seealso:: |
| 520 | |
| 521 | `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_ |
| 522 | by Tim Golden |
| 523 | |
| 524 | `Python and COM <http://www.boddie.org.uk/python/COM.html>`_ |
| 525 | by David and Paul Boddie |
| 526 | |
| 527 | |
| 528 | Py2exe |
| 529 | ------ |
| 530 | |
| 531 | `Py2exe <http://www.py2exe.org/>`_ is a :mod:`distutils` extension (see |
| 532 | :ref:`extending-distutils`) which wraps Python scripts into executable Windows |
| 533 | programs (:file:`{*}.exe` files). When you have done this, you can distribute |
| 534 | your application without requiring your users to install Python. |
| 535 | |
| 536 | |
| 537 | WConio |
| 538 | ------ |
| 539 | |
| 540 | Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to |
| 541 | Unix-like systems, there is a library exclusive to Windows as well: Windows |
| 542 | Console I/O for Python. |
| 543 | |
| 544 | `WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for |
| 545 | Turbo-C's :file:`CONIO.H`, used to create text user interfaces. |
| 546 | |
| 547 | |
| 548 | |
| 549 | Compiling Python on Windows |
| 550 | =========================== |
| 551 | |
| 552 | If you want to compile CPython yourself, first thing you should do is get the |
| 553 | `source <http://python.org/download/source/>`_. You can download either the |
| 554 | latest release's source or just grab a fresh `checkout |
Éric Araujo | 9be5723 | 2011-10-14 17:37:45 +0200 | [diff] [blame] | 555 | <http://docs.python.org/devguide/setup#checking-out-the-code>`_. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 556 | |
| 557 | For Microsoft Visual C++, which is the compiler with which official Python |
| 558 | releases are built, the source tree contains solutions/project files. View the |
| 559 | :file:`readme.txt` in their respective directories: |
| 560 | |
| 561 | +--------------------+--------------+-----------------------+ |
| 562 | | Directory | MSVC version | Visual Studio version | |
| 563 | +====================+==============+=======================+ |
Christian Heimes | 2c18161 | 2007-12-17 20:04:13 +0000 | [diff] [blame] | 564 | | :file:`PC/VC6/` | 6.0 | 97 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 565 | +--------------------+--------------+-----------------------+ |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 566 | | :file:`PC/VS7.1/` | 7.1 | 2003 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 567 | +--------------------+--------------+-----------------------+ |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 568 | | :file:`PC/VS8.0/` | 8.0 | 2005 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 569 | +--------------------+--------------+-----------------------+ |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 570 | | :file:`PCbuild/` | 9.0 | 2008 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 571 | +--------------------+--------------+-----------------------+ |
| 572 | |
| 573 | Note that not all of these build directories are fully supported. Read the |
| 574 | release notes to see which compiler version the official releases for your |
| 575 | version are built with. |
| 576 | |
| 577 | Check :file:`PC/readme.txt` for general information on the build process. |
| 578 | |
| 579 | |
| 580 | For extension modules, consult :ref:`building-on-windows`. |
| 581 | |
| 582 | .. seealso:: |
| 583 | |
| 584 | `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_ |
| 585 | or "Creating Python extensions in C/C++ with SWIG and compiling them with |
| 586 | MinGW gcc under Windows" or "Installing Python extension with distutils |
| 587 | and without Microsoft Visual C++" by Sébastien Sauvage, 2003 |
| 588 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 589 | `MingW -- Python extensions <http://oldwiki.mingw.org/index.php/Python%20extensions>`_ |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 590 | by Trent Apted et al, 2007 |
| 591 | |
| 592 | |
| 593 | Other resources |
| 594 | =============== |
| 595 | |
| 596 | .. seealso:: |
| 597 | |
| 598 | `Python Programming On Win32 <http://www.oreilly.com/catalog/pythonwin32/>`_ |
| 599 | "Help for Windows Programmers" |
| 600 | by Mark Hammond and Andy Robinson, O'Reilly Media, 2000, |
| 601 | ISBN 1-56592-621-8 |
| 602 | |
| 603 | `A Python for Windows Tutorial <http://www.imladris.com/Scripts/PythonForWindows.html>`_ |
| 604 | by Amanda Birmingham, 2004 |
| 605 | |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 606 | :pep:`397` - Python launcher for Windows |
| 607 | The proposal for the launcher to be included in the Python distribution. |
| 608 | |
| 609 | |