You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
13 KiB

Merged revisions 85548,85572-85573,85606,85609-85612,85614-85616 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r85548 | georg.brandl | 2010-10-15 21:46:19 +0200 (Fr, 15 Okt 2010) | 1 line #10072: assume a bit less knowledge of the FTP protocol in the ftplib docs. ........ r85572 | georg.brandl | 2010-10-16 20:51:05 +0200 (Sa, 16 Okt 2010) | 1 line #10122: typo fix. ........ r85573 | georg.brandl | 2010-10-16 20:53:08 +0200 (Sa, 16 Okt 2010) | 1 line #10124: typo fix. ........ r85606 | georg.brandl | 2010-10-17 08:32:59 +0200 (So, 17 Okt 2010) | 1 line #10058: tweak wording about exception returns. ........ r85609 | georg.brandl | 2010-10-17 11:19:03 +0200 (So, 17 Okt 2010) | 1 line #8556: use less confusing mapping key in example. ........ r85610 | georg.brandl | 2010-10-17 11:23:05 +0200 (So, 17 Okt 2010) | 1 line #8686: remove potentially confusing wording that does not add any value. ........ r85611 | georg.brandl | 2010-10-17 11:33:24 +0200 (So, 17 Okt 2010) | 1 line #8811: small fixes to sqlite3 docs. ........ r85612 | georg.brandl | 2010-10-17 11:37:54 +0200 (So, 17 Okt 2010) | 1 line #8855: add shelve security warning. ........ r85614 | georg.brandl | 2010-10-17 11:46:11 +0200 (So, 17 Okt 2010) | 1 line #8968: add actual name of token constants. ........ r85615 | georg.brandl | 2010-10-17 12:05:13 +0200 (So, 17 Okt 2010) | 1 line #459007: merge info from PC/getpathp.c and using/windows.rst to document the forming of sys.path under Windows. ........ r85616 | georg.brandl | 2010-10-17 12:07:29 +0200 (So, 17 Okt 2010) | 1 line Fix copy-paste error in example. ........
16 years ago
19 years ago
  1. .. highlightlang:: none
  2. .. _using-on-windows:
  3. *************************
  4. Using Python on Windows
  5. *************************
  6. .. sectionauthor:: Robert Lehmann <lehmannro@gmail.com>
  7. This document aims to give an overview of Windows-specific behaviour you should
  8. know about when using Python on Microsoft Windows.
  9. Installing Python
  10. =================
  11. Unlike most Unix systems and services, Windows does not require Python natively
  12. and thus does not pre-install a version of Python. However, the CPython team
  13. has compiled Windows installers (MSI packages) with every `release
  14. <http://www.python.org/download/releases/>`_ for many years.
  15. With ongoing development of Python, some platforms that used to be supported
  16. earlier are no longer supported (due to the lack of users or developers).
  17. Check :pep:`11` for details on all unsupported platforms.
  18. * DOS and Windows 3.x are deprecated since Python 2.0 and code specific to these
  19. systems was removed in Python 2.1.
  20. * Up to 2.5, Python was still compatible with Windows 95, 98 and ME (but already
  21. raised a deprecation warning on installation). For Python 2.6 (and all
  22. following releases), this support was dropped and new releases are just
  23. expected to work on the Windows NT family.
  24. * `Windows CE <http://pythonce.sourceforge.net/>`_ is still supported.
  25. * The `Cygwin <http://cygwin.com/>`_ installer offers to install the `Python
  26. interpreter <http://cygwin.com/packages/python>`_ as well; it is located under
  27. "Interpreters." (cf. `Cygwin package source
  28. <ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/
  29. release/python>`_, `Maintainer releases
  30. <http://www.tishler.net/jason/software/python/>`_)
  31. See `Python for Windows (and DOS) <http://www.python.org/download/windows/>`_
  32. for detailed information about platforms with precompiled installers.
  33. .. seealso::
  34. `Python on XP <http://www.richarddooling.com/index.php/2006/03/14/python-on-xp-7-minutes-to-hello-world/>`_
  35. "7 Minutes to "Hello World!""
  36. by Richard Dooling, 2006
  37. `Installing on Windows <http://diveintopython.net/installing_python/windows.html>`_
  38. in "`Dive into Python: Python from novice to pro
  39. <http://diveintopython.net/index.html>`_"
  40. by Mark Pilgrim, 2004,
  41. ISBN 1-59059-356-1
  42. `For Windows users <http://swaroopch.com/text/Byte_of_Python:Installing_Python#For_Windows_users>`_
  43. in "Installing Python"
  44. in "`A Byte of Python <http://www.byteofpython.info>`_"
  45. by Swaroop C H, 2003
  46. Alternative bundles
  47. ===================
  48. Besides the standard CPython distribution, there are modified packages including
  49. additional functionality. The following is a list of popular versions and their
  50. key features:
  51. `ActivePython <http://www.activestate.com/Products/activepython/>`_
  52. Installer with multi-platform compatibility, documentation, PyWin32
  53. `Enthought Python Distribution <http://www.enthought.com/products/epd.php>`_
  54. Popular modules (such as PyWin32) with their respective documentation, tool
  55. suite for building extensible Python applications
  56. Notice that these packages are likely to install *older* versions of Python.
  57. Configuring Python
  58. ==================
  59. In order to run Python flawlessly, you might have to change certain environment
  60. settings in Windows.
  61. Excursus: Setting environment variables
  62. ---------------------------------------
  63. Windows has a built-in dialog for changing environment variables (following
  64. guide applies to XP classical view): Right-click the icon for your machine
  65. (usually located on your Desktop and called "My Computer") and choose
  66. :menuselection:`Properties` there. Then, open the :guilabel:`Advanced` tab
  67. and click the :guilabel:`Environment Variables` button.
  68. In short, your path is:
  69. :menuselection:`My Computer
  70. --> Properties
  71. --> Advanced
  72. --> Environment Variables`
  73. In this dialog, you can add or modify User and System variables. To change
  74. System variables, you need non-restricted access to your machine
  75. (i.e. Administrator rights).
  76. Another way of adding variables to your environment is using the :command:`set`
  77. command::
  78. set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
  79. To make this setting permanent, you could add the corresponding command line to
  80. your :file:`autoexec.bat`. :program:`msconfig` is a graphical interface to this
  81. file.
  82. Viewing environment variables can also be done more straight-forward: The
  83. command prompt will expand strings wrapped into percent signs automatically::
  84. echo %PATH%
  85. Consult :command:`set /?` for details on this behaviour.
  86. .. seealso::
  87. http://support.microsoft.com/kb/100843
  88. Environment variables in Windows NT
  89. http://support.microsoft.com/kb/310519
  90. How To Manage Environment Variables in Windows XP
  91. http://www.chem.gla.ac.uk/~louis/software/faq/q1.html
  92. Setting Environment variables, Louis J. Farrugia
  93. Finding the Python executable
  94. -----------------------------
  95. Besides using the automatically created start menu entry for the Python
  96. interpreter, you might want to start Python in the DOS prompt. To make this
  97. work, you need to set your :envvar:`%PATH%` environment variable to include the
  98. directory of your Python distribution, delimited by a semicolon from other
  99. entries. An example variable could look like this (assuming the first two
  100. entries are Windows' default)::
  101. C:\WINDOWS\system32;C:\WINDOWS;C:\Python25
  102. Typing :command:`python` on your command prompt will now fire up the Python
  103. interpreter. Thus, you can also execute your scripts with command line options,
  104. see :ref:`using-on-cmdline` documentation.
  105. Finding modules
  106. ---------------
  107. Python usually stores its library (and thereby your site-packages folder) in the
  108. installation directory. So, if you had installed Python to
  109. :file:`C:\\Python\\`, the default library would reside in
  110. :file:`C:\\Python\\Lib\\` and third-party modules should be stored in
  111. :file:`C:\\Python\\Lib\\site-packages\\`.
  112. This is how :data:`sys.path` is populated on Windows:
  113. * An empty entry is added at the start, which corresponds to the current
  114. directory.
  115. * If the environment variable :envvar:`PYTHONPATH` exists, as described in
  116. :ref:`using-on-envvars`, its entries are added next. Note that on Windows,
  117. paths in this variable must be separated by semicolons, to distinguish them
  118. from the colon used in drive identifiers (``C:\`` etc.).
  119. * Additional "application paths" can be added in the registry as subkeys of
  120. :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the
  121. ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have
  122. semicolon-delimited path strings as their default value will cause each path
  123. to be added to :data:`sys.path`. (Note that all known installers only use
  124. HKLM, so HKCU is typically empty.)
  125. * If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as
  126. "Python Home". Otherwise, the path of the main Python executable is used to
  127. locate a "landmark file" (``Lib\os.py``) to deduce the "Python Home". If a
  128. Python home is found, the relevant sub-directories added to :data:`sys.path`
  129. (``Lib``, ``plat-win``, etc) are based on that folder. Otherwise, the core
  130. Python path is constructed from the PythonPath stored in the registry.
  131. * If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in
  132. the environment, and no registry entries can be found, a default path with
  133. relative entries is used (e.g. ``.\Lib;.\plat-win``, etc).
  134. The end result of all this is:
  135. * When running :file:`python.exe`, or any other .exe in the main Python
  136. directory (either an installed version, or directly from the PCbuild
  137. directory), the core path is deduced, and the core paths in the registry are
  138. ignored. Other "application paths" in the registry are always read.
  139. * When Python is hosted in another .exe (different directory, embedded via COM,
  140. etc), the "Python Home" will not be deduced, so the core path from the
  141. registry is used. Other "application paths" in the registry are always read.
  142. * If Python can't find its home and there is no registry (eg, frozen .exe, some
  143. very strange installation setup) you get a path with some default, but
  144. relative, paths.
  145. Executing scripts
  146. -----------------
  147. Python scripts (files with the extension ``.py``) will be executed by
  148. :program:`python.exe` by default. This executable opens a terminal, which stays
  149. open even if the program uses a GUI. If you do not want this to happen, use the
  150. extension ``.pyw`` which will cause the script to be executed by
  151. :program:`pythonw.exe` by default (both executables are located in the top-level
  152. of your Python installation directory). This suppresses the terminal window on
  153. startup.
  154. You can also make all ``.py`` scripts execute with :program:`pythonw.exe`,
  155. setting this through the usual facilities, for example (might require
  156. administrative rights):
  157. #. Launch a command prompt.
  158. #. Associate the correct file group with ``.py`` scripts::
  159. assoc .py=Python.File
  160. #. Redirect all Python files to the new executable::
  161. ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
  162. Additional modules
  163. ==================
  164. Even though Python aims to be portable among all platforms, there are features
  165. that are unique to Windows. A couple of modules, both in the standard library
  166. and external, and snippets exist to use these features.
  167. The Windows-specific standard modules are documented in
  168. :ref:`mswin-specific-services`.
  169. PyWin32
  170. -------
  171. The `PyWin32 <http://python.net/crew/mhammond/win32/>`_ module by Mark Hammond
  172. is a collection of modules for advanced Windows-specific support. This includes
  173. utilities for:
  174. * `Component Object Model <http://www.microsoft.com/com/>`_ (COM)
  175. * Win32 API calls
  176. * Registry
  177. * Event log
  178. * `Microsoft Foundation Classes <http://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx>`_ (MFC)
  179. user interfaces
  180. `PythonWin <http://web.archive.org/web/20060524042422/
  181. http://www.python.org/windows/pythonwin/>`_ is a sample MFC application
  182. shipped with PyWin32. It is an embeddable IDE with a built-in debugger.
  183. .. seealso::
  184. `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_
  185. by Tim Golden
  186. `Python and COM <http://www.boddie.org.uk/python/COM.html>`_
  187. by David and Paul Boddie
  188. Py2exe
  189. ------
  190. `Py2exe <http://www.py2exe.org/>`_ is a :mod:`distutils` extension (see
  191. :ref:`extending-distutils`) which wraps Python scripts into executable Windows
  192. programs (:file:`{*}.exe` files). When you have done this, you can distribute
  193. your application without requiring your users to install Python.
  194. WConio
  195. ------
  196. Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to
  197. Unix-like systems, there is a library exclusive to Windows as well: Windows
  198. Console I/O for Python.
  199. `WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for
  200. Turbo-C's :file:`CONIO.H`, used to create text user interfaces.
  201. Compiling Python on Windows
  202. ===========================
  203. If you want to compile CPython yourself, first thing you should do is get the
  204. `source <http://python.org/download/source/>`_. You can download either the
  205. latest release's source or just grab a fresh `checkout
  206. <http://docs.python.org/devguide/setup#checking-out-the-code>`_.
  207. For Microsoft Visual C++, which is the compiler with which official Python
  208. releases are built, the source tree contains solutions/project files. View the
  209. :file:`readme.txt` in their respective directories:
  210. +--------------------+--------------+-----------------------+
  211. | Directory | MSVC version | Visual Studio version |
  212. +====================+==============+=======================+
  213. | :file:`PC/VC6/` | 6.0 | 97 |
  214. +--------------------+--------------+-----------------------+
  215. | :file:`PC/VS7.1/` | 7.1 | 2003 |
  216. +--------------------+--------------+-----------------------+
  217. | :file:`PC/VS8.0/` | 8.0 | 2005 |
  218. +--------------------+--------------+-----------------------+
  219. | :file:`PCbuild/` | 9.0 | 2008 |
  220. +--------------------+--------------+-----------------------+
  221. Note that not all of these build directories are fully supported. Read the
  222. release notes to see which compiler version the official releases for your
  223. version are built with.
  224. Check :file:`PC/readme.txt` for general information on the build process.
  225. For extension modules, consult :ref:`building-on-windows`.
  226. .. seealso::
  227. `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_
  228. or "Creating Python extensions in C/C++ with SWIG and compiling them with
  229. MinGW gcc under Windows" or "Installing Python extension with distutils
  230. and without Microsoft Visual C++" by Sébastien Sauvage, 2003
  231. `MingW -- Python extensions <http://oldwiki.mingw.org/index.php/Python%20extensions>`_
  232. by Trent Apted et al, 2007
  233. Other resources
  234. ===============
  235. .. seealso::
  236. `Python Programming On Win32 <http://www.oreilly.com/catalog/pythonwin32/>`_
  237. "Help for Windows Programmers"
  238. by Mark Hammond and Andy Robinson, O'Reilly Media, 2000,
  239. ISBN 1-56592-621-8
  240. `A Python for Windows Tutorial <http://www.imladris.com/Scripts/PythonForWindows.html>`_
  241. by Amanda Birmingham, 2004