3. 在Windows上使用 Python

本文档旨在概述在 Microsoft Windows 上使用 Python 时应了解的特定于 Windows 的行为。

3.1. Installing Python

Unlike most Unix systems and services, Windows does not require Python natively and thus does not pre-install a version of Python. However, the CPython team has compiled Windows installers (MSI packages) with every release for many years.

随着Python的不断发展,不再支持以前曾经支持的一些平台(由于缺少用户或开发人员)。检查 PEP 11 了解所有不支持的平台的详细信息。

  • DOS and Windows 3.x are deprecated since Python 2.0 and code specific to these systems was removed in Python 2.1.
  • Up to 2.5, Python was still compatible with Windows 95, 98 and ME (but already raised a deprecation warning on installation). For Python 2.6 (and all following releases), this support was dropped and new releases are just expected to work on the Windows NT family.
  • Windows CE 仍然受支持。
  • Cygwin 安装包也提供安装Python解释器 (cf. Cygwin package source, Maintainer releases)

See Python for Windows (and DOS) for detailed information about platforms with precompiled installers.

参见

Python on XP
“7 Minutes to “Hello World!”” by Richard Dooling, 2006
Installing on Windows
in “Dive into Python: Python from novice to pro” by Mark Pilgrim, 2004, ISBN 1-59059-356-1
For Windows users
in “Installing Python” in “A Byte of Python” by Swaroop C H, 2003

3.2. 替代捆绑包

除了标准的CPython发行版之外,还有一些包含附加功能的修改包。以下是热门版本及其主要功能的列表:

ActivePython
具有多平台兼容性的安装程序,文档,PyWin32
Enthought Python Distribution
Popular modules (such as PyWin32) with their respective documentation, tool suite for building extensible Python applications

Notice that these packages are likely to install older versions of Python.

3.3. 配置Python

In order to run Python flawlessly, you might have to change certain environment settings in Windows.

3.3.1. 附录:设置环境变量

Windows has a built-in dialog for changing environment variables (following guide applies to XP classical view): Right-click the icon for your machine (usually located on your Desktop and called “My Computer”) and choose Properties there. Then, open the Advanced tab and click the Environment Variables button.

In short, your path is:

My Computer ‣ Properties ‣ Advanced ‣ Environment Variables

In this dialog, you can add or modify User and System variables. To change System variables, you need non-restricted access to your machine (i.e. Administrator rights).

Another way of adding variables to your environment is using the set command:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

To make this setting permanent, you could add the corresponding command line to your autoexec.bat. msconfig is a graphical interface to this file.

Viewing environment variables can also be done more straight-forward: The command prompt will expand strings wrapped into percent signs automatically:

echo %PATH%

Consult set /? for details on this behaviour.

参见

https://support.microsoft.com/kb/100843
Windows NT 的环境变量
https://support.microsoft.com/kb/310519
如何在Windows XP中管理环境变量
https://www.chem.gla.ac.uk/~louis/software/faq/q1.html
设置环境变量(For Windows 2000/NT),Louis J. Farrugia

3.3.2. 查找Python可执行文件

Besides using the automatically created start menu entry for the Python interpreter, you might want to start Python in the DOS prompt. To make this work, you need to set your %PATH% environment variable to include the directory of your Python distribution, delimited by a semicolon from other entries. An example variable could look like this (assuming the first two entries are Windows’ default):

C:\WINDOWS\system32;C:\WINDOWS;C:\Python25

Typing python on your command prompt will now fire up the Python interpreter. Thus, you can also execute your scripts with command line options, see 命令行 documentation.

3.3.3. 查找模块

Python通常将其库(以及您的site-packages文件夹)存储在安装目录中。因此,如果您已将Python安装到 C:\Python\ ,则默认库将驻留在 C:\Python\Lib\ 中,第三方模块存储在 C:\Python\Lib\site-packages\

This is how sys.path is populated on Windows:

  • 在开始时,添加一个空条目,该条目对应于当前目录。
  • 如果环境变量 PYTHONPATH 存在,如 环境变量 中所述,则接下来添加其条目。请注意,在Windows上,此变量中的路径必须用分号分隔,以区别于驱动器标识符中使用的冒号( C:\ 等)。
  • 附加的 “application paths” 可以同时添加到注册表` HKEY_CURRENT_USER`HKEY_LOCAL_MACHINE 分支下的:samp:\SOFTWARE\Python\PythonCore\{version}\PythonPath 中作为子键。以分号分隔的路径字符串作为默认值的子键将导致每个路径添加到 sys.path 。(请注意,所有已知的安装程序都只使用HKLM,因此HKCU通常为空。)
  • If the environment variable PYTHONHOME is set, it is assumed as “Python Home”. Otherwise, the path of the main Python executable is used to locate a “landmark file” (Lib\os.py) to deduce the “Python Home”. If a Python home is found, the relevant sub-directories added to sys.path (Lib, plat-win, etc) are based on that folder. Otherwise, the core Python path is constructed from the PythonPath stored in the registry.
  • 如果找不到Python Home,也没有指定 PYTHONPATH 环境变量,并且找不到注册表项,则使用具有相对条目的默认路径(例如 .\Lib; .\plat-win 等等)。

这一切的最终结果是:

  • 运行 python.exe ,或主Python目录中的任何其他.exe(安装版本,或直接来自PCbuild目录)时,推导出核心路径,并忽略注册表中的核心路径。始终读取注册表中的其他“应用程序路径”。
  • 当Python托管在另一个.exe(不同的目录,通过COM嵌入等)时,将不会推断出“Python Home”,因此使用了来自注册表的核心路径。始终读取注册表中的其他“应用程序路径”。
  • If Python can’t find its home and there is no registry (eg, frozen .exe, some very strange installation setup) you get a path with some default, but relative, paths.

3.3.4. Executing scripts

Python scripts (files with the extension .py) will be executed by python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw which will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.

You can also make all .py scripts execute with pythonw.exe, setting this through the usual facilities, for example (might require administrative rights):

  1. Launch a command prompt.

  2. Associate the correct file group with .py scripts:

    assoc .py=Python.File
    
  3. Redirect all Python files to the new executable:

    ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
    

3.4. 附加模块

尽管Python的目标是在所有平台中都可移植,但是Windows有一些独特的特性。在标准库和外部都有一些模块和代码片段在使用这些特性。

特定于Windows的标准模块记录在 Windows系统相关模块 中。

3.4.1. PyWin32

Mark Hammond 的 PyWin32 模块是一组用于高级Windows特定支持的模块。这包括以下实用程序:

PythonWin 是PyWin32附带的一个示例MFC应用程序。它是一个内置调试器的可嵌入IDE。

参见

Win32 How Do I…?
by Tim Golden
Python and COM
by David and Paul Boddie

3.4.2. Py2exe

Py2exe is a distutils extension (see 扩展 Distutils) which wraps Python scripts into executable Windows programs (*.exe files). When you have done this, you can distribute your application without requiring your users to install Python.

3.4.3. WConio

由于Python的高级终端处理层 curses 仅限于类Unix系统,因此Windows还有一个独立的库:用于Python的Windows 控制台 I/O .

WConio 是Turbo-C的 CONIO.H 装饰器,用于创建文本用户界面。

3.5. 在Windows上编译Python

If you want to compile CPython yourself, first thing you should do is get the source. You can download either the latest release’s source or just grab a fresh checkout.

For Microsoft Visual C++, which is the compiler with which official Python releases are built, the source tree contains solutions/project files. View the readme.txt in their respective directories:

Directory MSVC version Visual Studio version
PC/VC6/ 6.0 97
PC/VS7.1/ 7.1 2003
PC/VS8.0/ 8.0 2005
PCbuild/ 9.0 2008

Note that not all of these build directories are fully supported. Read the release notes to see which compiler version the official releases for your version are built with.

Check PC/readme.txt for general information on the build process.

有关扩展模块,请参阅 在Windows平台编译C和C++扩展

参见

Python + Windows + distutils + SWIG + gcc MinGW
或 “Creating Python extensions in C/C++ with SWIG and compiling them with MinGW gcc under Windows” 或 “Installing Python extension with distutils and without Microsoft Visual C++” by Sébastien Sauvage, 2003
MingW – Python extensions
by Trent Apted et al, 2007

3.6. Other resources

参见

Python Programming On Win32
“Help for Windows Programmers” by Mark Hammond and Andy Robinson, O’Reilly Media, 2000, ISBN 1-56592-621-8
A Python for Windows Tutorial
by Amanda Birmingham, 2004