Files
hustoj/web/python-docs/using/windows.html
2025-03-09 17:10:56 +08:00

984 lines
98 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>3. 在Windows上使用 Python &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="4. 在苹果系统上使用 Python" href="mac.html" />
<link rel="prev" title="2. 在类Unix环境下使用Python" href="unix.html" />
<link rel="canonical" href="https://docs.python.org/3/using/windows.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">3. 在Windows上使用 Python</a><ul>
<li><a class="reference internal" href="#the-full-installer">3.1. 完整安装程序</a><ul>
<li><a class="reference internal" href="#installation-steps">3.1.1. 安装步骤</a></li>
<li><a class="reference internal" href="#removing-the-max-path-limitation">3.1.2. 删除 MAX_PATH 限制</a></li>
<li><a class="reference internal" href="#installing-without-ui">3.1.3. 无UI 安装</a></li>
<li><a class="reference internal" href="#installing-without-downloading">3.1.4. 免下载安装</a></li>
<li><a class="reference internal" href="#modifying-an-install">3.1.5. 修改安装</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-microsoft-store-package">3.2. Microsoft Store包</a><ul>
<li><a class="reference internal" href="#known-issues">3.2.1. 已知的问题</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-nuget-org-packages">3.3. nuget.org 安装包</a></li>
<li><a class="reference internal" href="#the-embeddable-package">3.4. 可嵌入的包</a><ul>
<li><a class="reference internal" href="#python-application">3.4.1. Python 应用程序</a></li>
<li><a class="reference internal" href="#embedding-python">3.4.2. 嵌入Python</a></li>
</ul>
</li>
<li><a class="reference internal" href="#alternative-bundles">3.5. 替代捆绑包</a></li>
<li><a class="reference internal" href="#configuring-python">3.6. 配置Python</a><ul>
<li><a class="reference internal" href="#excursus-setting-environment-variables">3.6.1. 附录:设置环境变量</a></li>
<li><a class="reference internal" href="#finding-the-python-executable">3.6.2. 查找Python可执行文件</a></li>
</ul>
</li>
<li><a class="reference internal" href="#utf-8-mode">3.7. UTF-8 模式</a></li>
<li><a class="reference internal" href="#python-launcher-for-windows">3.8. 适用于Windows的Python启动器</a><ul>
<li><a class="reference internal" href="#getting-started">3.8.1. 入门</a><ul>
<li><a class="reference internal" href="#from-the-command-line">3.8.1.1. 从命令行</a></li>
<li><a class="reference internal" href="#virtual-environments">3.8.1.2. 从虚拟环境</a></li>
<li><a class="reference internal" href="#from-a-script">3.8.1.3. 从脚本</a></li>
<li><a class="reference internal" href="#from-file-associations">3.8.1.4. 从文件关联</a></li>
</ul>
</li>
<li><a class="reference internal" href="#shebang-lines">3.8.2. Shebang 行</a></li>
<li><a class="reference internal" href="#arguments-in-shebang-lines">3.8.3. shebang 行的参数</a></li>
<li><a class="reference internal" href="#customization">3.8.4. 自定义</a><ul>
<li><a class="reference internal" href="#customization-via-ini-files">3.8.4.1. 通过INI文件自定义</a></li>
<li><a class="reference internal" href="#customizing-default-python-versions">3.8.4.2. 自定义默认的Python版本</a></li>
</ul>
</li>
<li><a class="reference internal" href="#diagnostics">3.8.5. 诊断</a></li>
</ul>
</li>
<li><a class="reference internal" href="#finding-modules">3.9. 查找模块</a></li>
<li><a class="reference internal" href="#additional-modules">3.10. 附加模块</a><ul>
<li><a class="reference internal" href="#pywin32">3.10.1. PyWin32</a></li>
<li><a class="reference internal" href="#cx-freeze">3.10.2. cx_Freeze</a></li>
<li><a class="reference internal" href="#wconio">3.10.3. WConio</a></li>
</ul>
</li>
<li><a class="reference internal" href="#compiling-python-on-windows">3.11. 在Windows上编译Python</a></li>
<li><a class="reference internal" href="#other-platforms">3.12. 其他平台</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="unix.html"
title="上一章"><span class="section-number">2. </span>在类Unix环境下使用Python</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="mac.html"
title="下一章"><span class="section-number">4. </span>在苹果系统上使用 Python</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/using/windows.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/using/windows.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="mac.html" title="4. 在苹果系统上使用 Python"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="unix.html" title="2. 在类Unix环境下使用Python"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python安装和使用</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="using-python-on-windows">
<span id="using-on-windows"></span><h1><span class="section-number">3. </span>在Windows上使用 Python<a class="headerlink" href="#using-python-on-windows" title="永久链接至标题"></a></h1>
<p>本文档旨在概述在 Microsoft Windows 上使用 Python 时应了解的特定于 Windows 的行为。</p>
<p>与大多数UNIX系统和服务不同Windows系统没有预安装Python。多年来CPython 团队已经编译了每一个 <a class="reference external" href="https://www.python.org/download/releases/">发行版</a> 的Windows安装程序MSI 包已便Windows 用户下载和安装。这些安装程序主要用于每个用户单独安装Python时添加核心解释器和库。安装程序还可以为一台机器的所有用户安装并且可以为应用程序本地分发提供单独的zip文件。</p>
<p><span class="target" id="index-56"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0011"><strong>PEP 11</strong></a> 中所述Python 仅支持微软产品支持生命周期内的Windows 版本。这意味着 Python 3.8 支持Windows Vista和更新版本。如果需要Windows XP支持请安装Python 3.4。</p>
<p>Windows提供了许多不同的安装程序每个安装程序都有一定的优点和缺点。</p>
<p><a class="reference internal" href="#windows-full"><span class="std std-ref">完整安装程序</span></a> 内含所有组件对于使用Python 进行任何类型项目的开发人员而言,它是最佳选择。</p>
<p><a class="reference internal" href="#windows-store"><span class="std std-ref">Microsoft Store包</span></a> 是一个简单的Python 安装适用于运行脚本和包以及使用IDLE或其他开发环境。 它需要Windows 10但可以安全地安装而不会破坏其他程序。 它还提供了许多方便的命令来启动Python及其工具。</p>
<p><a class="reference internal" href="#windows-nuget"><span class="std std-ref">nuget.org 安装包</span></a> 是用于持续集成系统的轻量级安装。它可用于构建Python包或运行脚本但不可更新且没有用户界面工具。</p>
<p><a class="reference internal" href="#windows-embeddable"><span class="std std-ref">可嵌入的包</span></a> 是Python的最小安装包适合嵌入到更大的应用程序中。</p>
<section id="the-full-installer">
<span id="windows-full"></span><h2><span class="section-number">3.1. </span>完整安装程序<a class="headerlink" href="#the-full-installer" title="永久链接至标题"></a></h2>
<section id="installation-steps">
<h3><span class="section-number">3.1.1. </span>安装步骤<a class="headerlink" href="#installation-steps" title="永久链接至标题"></a></h3>
<p>四个 Python 3.8 安装程序可供下载 - 32位和64位版本的各有两个。 <em>web installer</em> (网络安装包)是一个小的初始化工具,它将在安装过程中,根据需要自动下载所需的组件。 <em>offline installer</em> 离线安装包内含默认安装所需的组件可选择功能仍需要Internet连接下载。请参阅 <a class="reference internal" href="#install-layout-option"><span class="std std-ref">免下载安装</span></a> 以了解在安装过程中避免下载的其他方法。</p>
<p>启动安装程序后,可以选择以下两个选项之一:</p>
<img alt="../_images/win_installer.png" src="../_images/win_installer.png" />
<p>如果选择“Install Now立即安装</p>
<ul class="simple">
<li><p><em></em> 需要成为管理员除非需要对C运行库进行系统更新或者为所有用户安装 <a class="reference internal" href="#launcher"><span class="std std-ref">适用于Windows的Python启动器</span></a> </p></li>
<li><p>Python将安装到您的用户目录中</p></li>
<li><p><a class="reference internal" href="#launcher"><span class="std std-ref">适用于Windows的Python启动器</span></a> 将根据第一页底部的选项安装</p></li>
<li><p>将安装标准库测试套件启动器和pip</p></li>
<li><p>如果选择将安装目录将添加到 <span class="target" id="index-57"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></p></li>
<li><p>快捷方式仅对当前用户可见</p></li>
</ul>
<p>选择“自定义安装”将允许您选择:要安装的功能、安装位置、其他选项或安装后的操作。如果要安装调试符号或二进制文件,您需要使用此选项。</p>
<p>如要为全部用户安装,应选择“自定义安装”。在这种情况下:</p>
<ul class="simple">
<li><p>您可能需要提供管理凭据或批准</p></li>
<li><p>Python 将安装到Program Files目录中</p></li>
<li><p><a class="reference internal" href="#launcher"><span class="std std-ref">适用于Windows的Python启动器</span></a> 将安装到Windows目录中</p></li>
<li><p>安装期间可以选择可选功能</p></li>
<li><p>标准库可以预编译为字节码</p></li>
<li><p>如果选中,安装目录将添加到系统 <span class="target" id="index-58"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></p></li>
<li><p>快捷方式所有用户可用</p></li>
</ul>
</section>
<section id="removing-the-max-path-limitation">
<span id="max-path"></span><h3><span class="section-number">3.1.2. </span>删除 MAX_PATH 限制<a class="headerlink" href="#removing-the-max-path-limitation" title="永久链接至标题"></a></h3>
<p>历史上Windows的路径长度限制为260个字符。这意味着长于此的路径将无法解决并导致错误。</p>
<p>在最新版本的 Windows 中,此限制可被扩展到大约 32,000 个字符。 但需要让管理员激活“启用 Win32 长路径”组策略,或在注册表键 <code class="docutils literal notranslate"><span class="pre">HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem</span></code> 中设置 <code class="docutils literal notranslate"><span class="pre">LongPathsEnabled</span></code><code class="docutils literal notranslate"><span class="pre">1</span></code></p>
<p>这允许 <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> 函数,<a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> 模块和大多数其他路径功能接受并返回长度超过 260 个字符的路径。</p>
<p>更改上述选项后,无需进一步配置。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>Python中启用了对长路径的支持。</p>
</div>
</section>
<section id="installing-without-ui">
<span id="install-quiet-option"></span><h3><span class="section-number">3.1.3. </span>无UI 安装<a class="headerlink" href="#installing-without-ui" title="永久链接至标题"></a></h3>
<p>安装程序UI中的所有选项也可以从命令行指定允许脚本安装程序在许多机器上复制安装而无需用户交互。还可以在不禁用UI的情况下设置这些选项以更改一些默认值。</p>
<p>要完全隐藏安装程序UI并静默安装Python请使用 <code class="docutils literal notranslate"><span class="pre">/quiet</span></code> 选项。要跳过用户交互但仍然显示进度和错误,请使用 <code class="docutils literal notranslate"><span class="pre">/passive</span></code> 选项。可以通过 <code class="docutils literal notranslate"><span class="pre">/uninstall</span></code> 选项立即开始删除Python - - 不会显示任何提示。</p>
<p>所有其他选项都传递为 <code class="docutils literal notranslate"><span class="pre">name=value</span></code> ,其中值通常是 <code class="docutils literal notranslate"><span class="pre">0</span></code> 来禁用某个特性, <code class="docutils literal notranslate"><span class="pre">1</span></code> 来启用某个特性或路径。可用选项的完整列表如下所示。</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 30%" />
<col style="width: 42%" />
<col style="width: 29%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>名称</p></th>
<th class="head"><p>描述</p></th>
<th class="head"><p>默认值</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>InstallAllUsers</p></td>
<td><p>为所有用户安装。</p></td>
<td><p>0</p></td>
</tr>
<tr class="row-odd"><td><p>TargetDir</p></td>
<td><p>安装目录</p></td>
<td><p>基于InstallAllUsers选择</p></td>
</tr>
<tr class="row-even"><td><p>DefaultAllUsersTargetDir</p></td>
<td><p>为所有用户安装时的默认安装路径</p></td>
<td><p><code class="file docutils literal notranslate"><span class="pre">%ProgramFiles%\Python</span> <span class="pre">X.Y</span></code><code class="file docutils literal notranslate"><span class="pre">%ProgramFiles(x86)%\Python</span> <span class="pre">X.Y</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>DefaultJustForMeTargetDir</p></td>
<td><p>仅为当前用户安装时的默认安装路径</p></td>
<td><p><code class="file docutils literal notranslate"><span class="pre">%LocalAppData%\Programs\PythonXY</span></code><code class="file docutils literal notranslate"><span class="pre">%LocalAppData%\Programs\PythonXY-32</span></code><code class="file docutils literal notranslate"><span class="pre">%LocalAppData%\Programs\PythonXY-64</span></code></p></td>
</tr>
<tr class="row-even"><td><p>DefaultCustomTargetDir</p></td>
<td><p>UI中显示的默认自定义安装目录</p></td>
<td><p>(空)</p></td>
</tr>
<tr class="row-odd"><td><p>AssociateFiles</p></td>
<td><p>如果还安装了启动器,则创建文件关联。</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-even"><td><p>CompileAll</p></td>
<td><p>将所有 <code class="docutils literal notranslate"><span class="pre">.py</span></code> 文件编译为 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code></p></td>
<td><p>0</p></td>
</tr>
<tr class="row-odd"><td><p>PrependPath</p></td>
<td><p>将install和Scripts目录添加到 <span class="target" id="index-59"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 以及将 <code class="docutils literal notranslate"><span class="pre">.PY</span></code> 添加到 <span class="target" id="index-60"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATHEXT</span></code></p></td>
<td><p>0</p></td>
</tr>
<tr class="row-even"><td><p>Shortcuts</p></td>
<td><p>如果已安装为解释器文档和IDLE创建快捷方式</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>Include_doc</p></td>
<td><p>安装Python手册</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-even"><td><p>Include_debug</p></td>
<td><p>安装调试二进制文件</p></td>
<td><p>0</p></td>
</tr>
<tr class="row-odd"><td><p>Include_dev</p></td>
<td><p>安装开发人员头文件和库</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-even"><td><p>Include_exe</p></td>
<td><p>安装 <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> 及相关文件</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>Include_launcher</p></td>
<td><p>安装 <a class="reference internal" href="#launcher"><span class="std std-ref">适用于Windows的Python启动器</span></a> .</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-even"><td><p>InstallLauncherAllUsers</p></td>
<td><p>为所有用户安装 <a class="reference internal" href="#launcher"><span class="std std-ref">适用于Windows的Python启动器</span></a></p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>Include_lib</p></td>
<td><p>安装标准库和扩展模块</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-even"><td><p>Include_pip</p></td>
<td><p>安装捆绑的pip和setuptools</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>Include_symbols</p></td>
<td><p>安装调试符号(<cite>*</cite>.pdb)</p></td>
<td><p>0</p></td>
</tr>
<tr class="row-even"><td><p>Include_tcltk</p></td>
<td><p>安装Tcl/Tk 支持和IDLE</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>Include_test</p></td>
<td><p>安装标准库测试套件</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-even"><td><p>Include_tools</p></td>
<td><p>安装实用程序脚本</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>LauncherOnly</p></td>
<td><p>仅安装启动器。这将覆盖大多数其他选项。</p></td>
<td><p>0</p></td>
</tr>
<tr class="row-even"><td><p>SimpleInstall</p></td>
<td><p>禁用大多数安装UI</p></td>
<td><p>0</p></td>
</tr>
<tr class="row-odd"><td><p>SimpleInstallDescription</p></td>
<td><p>使用简化安装UI时显示的自定义消息。</p></td>
<td><p>(空)</p></td>
</tr>
</tbody>
</table>
<p>例如要以静默方式全局安装默认的Python您可以在命令提示符&gt;)使用以下命令:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.8.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
</pre></div>
</div>
<p>要允许用户在没有测试套件的情况下轻松安装Python的个人副本可以使用以下命令提供快捷方式。这将显示一个简化的初始页面不允许自定义:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.8.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
SimpleInstall=1 SimpleInstallDescription=&quot;Just for me, no test suite.&quot;
</pre></div>
</div>
<p>(请注意,省略启动器也会省略文件关联,并且仅在全局安装包含启动器时才建议用于每用户安装。)</p>
<p>上面列出的选项也可以在一个名为 <code class="docutils literal notranslate"><span class="pre">unattend.xml</span></code> 的文件中与可执行文件一起提供。此文件指定选项和值的列表。作为属性提供的值,(如果可能)它将转换为数字。作为文本提供的值,始终保留为字符串。此示例文件设置与上一示例采用相同的选项:</p>
<div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="nt">&lt;Options&gt;</span>
<span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;InstallAllUsers&quot;</span><span class="w"> </span><span class="na">Value=</span><span class="s">&quot;no&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
<span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;Include_launcher&quot;</span><span class="w"> </span><span class="na">Value=</span><span class="s">&quot;0&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
<span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;Include_test&quot;</span><span class="w"> </span><span class="na">Value=</span><span class="s">&quot;no&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
<span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;SimpleInstall&quot;</span><span class="w"> </span><span class="na">Value=</span><span class="s">&quot;yes&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
<span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;SimpleInstallDescription&quot;</span><span class="nt">&gt;</span>Just<span class="w"> </span>for<span class="w"> </span>me,<span class="w"> </span>no<span class="w"> </span>test<span class="w"> </span>suite<span class="nt">&lt;/Option&gt;</span>
<span class="nt">&lt;/Options&gt;</span>
</pre></div>
</div>
</section>
<section id="installing-without-downloading">
<span id="install-layout-option"></span><h3><span class="section-number">3.1.4. </span>免下载安装<a class="headerlink" href="#installing-without-downloading" title="永久链接至标题"></a></h3>
<p>由于下载的初始安装包中未包含Python的某些可选功能如果选择安装这些功能可能需要Internet连接。为了避免这种需要可以按需下载所有可能的组件以创建一个完整的布局该布局将不再需要internet连接而不管所选择的特性是什么。请注意此下载可能比要求的要大但是如果要执行大量安装则拥有本地缓存的副本非常有用。</p>
<p>从命令提示符执行以下命令以下载所有可能的必需文件。请记住将 <code class="docutils literal notranslate"><span class="pre">python-3.8.0.exe</span></code> 替换为安装程序的实际名称,并在自己的目录中创建布局,以避免同名的文件之间发生冲突。</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.8.0.exe /layout [optional target directory]
</pre></div>
</div>
<p>您也可以指定 <code class="docutils literal notranslate"><span class="pre">/quiet</span></code> 选项来隐藏进度显示。</p>
</section>
<section id="modifying-an-install">
<h3><span class="section-number">3.1.5. </span>修改安装<a class="headerlink" href="#modifying-an-install" title="永久链接至标题"></a></h3>
<p>安装Python后您可以通过Windows中的“程序和功能”工具添加或删除功能。选择Python条目并选择“卸载/更改”以在维护模式下打开安装程序。</p>
<p>“修改” 允许您通过修改复选框来添加或删除功能 - 未更改的复选框将不会安装或删除任何内容。在此模式下无法更改某些选项例如安装目录要修改这些您需要完全删除然后重新安装Python。</p>
<p>“修复” 将使用当前设置验证应安装的所有文件,并替换已删除或修改的任何文件</p>
<p>“卸载” 将完全删除Python<a class="reference internal" href="#launcher"><span class="std std-ref">适用于Windows的Python启动器</span></a> 除外,它在“程序和功能”中有自己的条目。</p>
</section>
</section>
<section id="the-microsoft-store-package">
<span id="windows-store"></span><h2><span class="section-number">3.2. </span>Microsoft Store包<a class="headerlink" href="#the-microsoft-store-package" title="永久链接至标题"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">3.7.2 新版功能.</span></p>
</div>
<p>Microsoft Store 包是一个易于安装的 Python 解释器,主要针对在交互模式下使用,例如用于教学。</p>
<p>要安装此软件包请确保您拥有最新的Windows 10更新并在Microsoft Store应用程序中搜索 &quot;Python 3.8&quot; 。确保您选择的应用程序由 Python Software Foundation 发布并安装。</p>
<div class="admonition warning">
<p class="admonition-title">警告</p>
<p>Python将始终在Microsoft Store上免费提供。如果要求您付款则表示您没有选择正确的包。</p>
</div>
<p>安装完成后,可以在开始菜单中找到它来启动 Python。或者可以在命令提示符或 PowerShell 会话中输入 <code class="docutils literal notranslate"><span class="pre">python</span></code> 来启动。此外可以输入 <code class="docutils literal notranslate"><span class="pre">pip</span></code><code class="docutils literal notranslate"><span class="pre">idle</span></code> 来使用 pip 和 IDLE。IDLE 也在开始菜单中。</p>
<p>所有这三个命令也可以使用版本号后缀,例如, <code class="docutils literal notranslate"><span class="pre">python3.exe</span></code><code class="docutils literal notranslate"><span class="pre">python3.x.exe</span></code> 以及 <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> (其中 <code class="docutils literal notranslate"><span class="pre">3.x</span></code> 是您要启动的特定版本,例如 3.8 )。在 <code class="docutils literal notranslate"><span class="pre">设置--&gt;主页--&gt;应用和功能</span></code> 页面中,点选 <code class="docutils literal notranslate"><span class="pre">管理可选功能</span></code> 选择与每个命令关联的python版本。建议确保 <code class="docutils literal notranslate"><span class="pre">pip</span></code><code class="docutils literal notranslate"><span class="pre">idle</span></code> 与选择的 <code class="docutils literal notranslate"><span class="pre">python</span></code> 版本一致。</p>
<p>可以使用 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">venv</span></code> 创建虚拟环境并激活并正常使用。</p>
<p>如果你已经安装了另一个版本的Python并将它添加到你的 <code class="docutils literal notranslate"><span class="pre">PATH</span></code> 变量中,那么它将作为 <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> 而不是来自Microsoft Store的那个。要访问新安装请使用 <code class="docutils literal notranslate"><span class="pre">python3.exe</span></code><code class="docutils literal notranslate"><span class="pre">python3.x.exe</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">py.exe</span></code> 启动器将检测此 Python 安装版,但会优先使用来自传统安装器的安装版。</p>
<p>要删除Python请打开“设置”并使用“应用程序和功能”或者在“开始”中找到Python然后右键单击以选择“卸载”。卸载将删除该已安装Python程序中的所有软件包但不会删除任何虚拟环境</p>
<section id="known-issues">
<h3><span class="section-number">3.2.1. </span>已知的问题<a class="headerlink" href="#known-issues" title="永久链接至标题"></a></h3>
<p>由于Microsoft Store应用程序的限制Python脚本可能无法对共享位置<code class="docutils literal notranslate"><span class="pre">TEMP</span></code> )和注册表进行完全写入访问。相反,它将写入私人副本。如果脚本必须修改共享位置,则需要安装完整安装程序。</p>
<p>有关此限制的技术原理的更多细节,请查询 Microsoft 已打包完全可信应用的文档,当前位于 <a class="reference external" href="https://docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-behind-the-scenes">docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-behind-the-scenes</a></p>
</section>
</section>
<section id="the-nuget-org-packages">
<span id="windows-nuget"></span><h2><span class="section-number">3.3. </span>nuget.org 安装包<a class="headerlink" href="#the-nuget-org-packages" title="永久链接至标题"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">3.5.2 新版功能.</span></p>
</div>
<p>nuget.org 是一个精简的 Python 环境,用于在没有全局安装 Python 的系统的持续集成和构建。 虽然 nuget 是“.NET的包管理器”但是对于包含构建时工具的包来说它也可以很好地工作。</p>
<p>访问 <a class="reference external" href="https://www.nuget.org/">nuget.org</a> 获取有关使用 nuget 的最新信息。 下面的摘要对 Python 开发人员来说已经足够了。</p>
<p><code class="docutils literal notranslate"><span class="pre">nuget.exe</span></code> 命令行工具可以直接从 <code class="docutils literal notranslate"><span class="pre">https://aka.ms/nugetclidl</span></code> 下载,例如,使用 curl 或 PowerShell。 使用该工具安装 64 位或 32 位最新版本的 Python:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>nuget.exe install python -ExcludeVersion -OutputDirectory .
nuget.exe install pythonx86 -ExcludeVersion -OutputDirectory .
</pre></div>
</div>
<p>要选择特定版本,请添加 <code class="docutils literal notranslate"><span class="pre">-Version</span> <span class="pre">3.x.y</span></code> 。输出目录可以从 <code class="docutils literal notranslate"><span class="pre">.</span></code> 更改,包将安装到子目录中。默认情况下,子目录的名称与包的名称相同,如果没有 <code class="docutils literal notranslate"><span class="pre">-ExcludeVersion</span></code> 选项则此名称将包含已安装的特定版本。子目录里面是一个包含Python安装的 <code class="docutils literal notranslate"><span class="pre">tools</span></code> 目录:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># Without -ExcludeVersion
&gt; .\python.3.5.2\tools\python.exe -V
Python 3.5.2
# With -ExcludeVersion
&gt; .\python\tools\python.exe -V
Python 3.5.2
</pre></div>
</div>
<p>通常nuget 包不可升级,应该平行安装较新版本并使用完整路径引用。 或者,手动删除程序包目录并再次安装。 如果在构建之间不保留文件,许多 CI 系统将自动执行此操作。</p>
<p>除了 <code class="docutils literal notranslate"><span class="pre">tools</span></code> 目录外,还有一个 <code class="docutils literal notranslate"><span class="pre">build\native</span></code> 目录。 它包含一个 MSBuild 属性文件 <code class="docutils literal notranslate"><span class="pre">python.props</span></code>,可以在 C++ 项目中使用该文件来引用 Python 安装。 包含这些设置将自动在生成中使用标头和导入库。</p>
<p>nuget.org上的包信息页是 <a class="reference external" href="https://www.nuget.org/packages/python">www.nuget.org/packages/python</a> 对于64位版本和 <a class="reference external" href="https://www.nuget.org/packages/pythonx86">www.nuget.org/packages/pythonx86</a> 表示32位版本。</p>
</section>
<section id="the-embeddable-package">
<span id="windows-embeddable"></span><h2><span class="section-number">3.4. </span>可嵌入的包<a class="headerlink" href="#the-embeddable-package" title="永久链接至标题"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
<p>嵌入式发行版是一个包含最小 Python 环境的 ZIP 文件。 它旨在作为另一个应用程序的一部分,而不是由最终用户直接访问。</p>
<p>解压缩后,嵌入式发行版(几乎)与用户系统完全隔离,包括环境变量、系统注册表设置和已安装的软件包。标准库作为预先编译和优化的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件包含在ZIP中并提供了 <code class="docutils literal notranslate"><span class="pre">python3.dll</span></code> <code class="docutils literal notranslate"><span class="pre">python37.dll</span></code> <code class="docutils literal notranslate"><span class="pre">python.exe</span></code><code class="docutils literal notranslate"><span class="pre">pythonw.exe</span></code> 文件。不包括Tcl/tk包括所有依赖项如Idlepip和Python文档。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>嵌入式发行版不包括 <a class="reference external" href="https://www.microsoft.com/en-us/download/details.aspx?id=48145">Microsoft C Runtime</a> 应用程序安装程序负责提供此功能。运行时可能已经预先安装在用户的系统上或通过Windows Update自动安装并且可以通过在系统目录中找到 <code class="docutils literal notranslate"><span class="pre">ucrtbase.dll</span></code> 来检测</p>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>当在Windows 7上运行时, Python 3.8 需求KB2533623更新才能够安装.嵌入式分配器不能检查这次更新, 所以将在运行时出现问题. 最新版本的Windows包含次更新.</p>
</div>
<p>第三方软件包应该由应用程序与嵌入式发行版一起安装。这个发行版不支持像常规 Python 安装那样使用 pip 来管理依赖关系,不过可以小心地将 pip 包含进来并使用它进行自动更新。 通常,第三方包应该作为应用程序的一部分(“打包”)处理,以便开发人员在向用户提供更新之前能够确保与新版本兼容。</p>
<p>下面描述了这个发行版的两个推荐用例。</p>
<section id="python-application">
<h3><span class="section-number">3.4.1. </span>Python 应用程序<a class="headerlink" href="#python-application" title="永久链接至标题"></a></h3>
<p>用 Python 编写的应用程序并不一定要求用户了解这一事实。 在这种情况下,可以使用嵌入式发行版在安装包中包含 Python 的私有版本。 根据它应该有多透明(或者相反,它应该看起来有多专业),有两个选项。</p>
<p>使用专门的可执行文件作为启动程序需要一些编码,但为用户提供了最透明的体验。使用定制的启动器,没有明显的迹象表明程序是在 Python 上运行的:图标可以定制,公司和版本信息可以指定,文件关联可以正常运行。在大多数情况下,自定义启动程序应该只需使用硬编码的命令行就能调用 <code class="docutils literal notranslate"><span class="pre">Py_Main</span></code></p>
<p>更简单的方法是提供批处理文件或生成的快捷方式,使用所需的命令行参数直接调用 <code class="docutils literal notranslate"><span class="pre">python.exe</span></code><code class="docutils literal notranslate"><span class="pre">pythonw.exe</span></code>。在这种情况下,应用程序将显示为 Python 而不是其实际名称,并且用户可能无法将其与其他正在运行的 Python 进程或文件关联区分开来。</p>
<p>对于后一种方法,包应该与 Python 可执行文件一起作为目录安装,以确保它们在路径上可用。 使用专用的启动器,包可以位于其他位置,因为在启动应用程序之前有机会指定搜索路径。</p>
</section>
<section id="embedding-python">
<h3><span class="section-number">3.4.2. </span>嵌入Python<a class="headerlink" href="#embedding-python" title="永久链接至标题"></a></h3>
<p>用本地代码编写的应用程序通常需要某种形式的脚本语言嵌入式Python发行版可以用于此目的。通常应用程序的大部分都是本机代码某些部分将调用 <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> 或直接使用 <code class="docutils literal notranslate"><span class="pre">python3.dll</span></code> 。无论是哪种情况将嵌入的发行版解压缩到应用程序安装的子目录中就足以提供可加载的Python解释器。</p>
<p>与应用程序使用一样,包可以安装到任何位置,因为在初始化解释器之前有机会指定搜索路径。否则,使用嵌入式发行版和常规安装之间没有根本区别。</p>
</section>
</section>
<section id="alternative-bundles">
<h2><span class="section-number">3.5. </span>替代捆绑包<a class="headerlink" href="#alternative-bundles" title="永久链接至标题"></a></h2>
<p>除了标准的CPython发行版之外还有一些包含附加功能的修改包。以下是热门版本及其主要功能的列表</p>
<dl class="simple">
<dt><a class="reference external" href="https://www.activestate.com/activepython/">ActivePython</a></dt><dd><p>具有多平台兼容性的安装程序文档PyWin32</p>
</dd>
<dt><a class="reference external" href="https://www.anaconda.com/download/">Anaconda</a></dt><dd><p>流行的科学模块如numpyscipy和pandas<code class="docutils literal notranslate"><span class="pre">conda</span></code> 包管理器。</p>
</dd>
<dt><a class="reference external" href="https://www.enthought.com/product/canopy/">Canopy</a></dt><dd><p>具有编辑器和其他开发工具的“全面的Python分析环境”。</p>
</dd>
<dt><a class="reference external" href="https://winpython.github.io/">WinPython</a></dt><dd><p>特定于Windows的发行版包含用于构建包的预构建科学包和工具。</p>
</dd>
</dl>
<p>请注意这些软件包可能不包含最新版本的Python或其他库并且不由核心Python团队维护或支持。</p>
</section>
<section id="configuring-python">
<h2><span class="section-number">3.6. </span>配置Python<a class="headerlink" href="#configuring-python" title="永久链接至标题"></a></h2>
<p>要从命令提示符方便地运行Python您可以考虑在Windows中更改一些默认环境变量。虽然安装程序提供了为您配置PATH和PATHEXT变量的选项但这仅适用于单版本、全局安装。如果您经常使用多个版本的Python请考虑使用 <a class="reference internal" href="#launcher"><span class="std std-ref">适用于Windows的Python启动器</span></a></p>
<section id="excursus-setting-environment-variables">
<span id="setting-envvars"></span><h3><span class="section-number">3.6.1. </span>附录:设置环境变量<a class="headerlink" href="#excursus-setting-environment-variables" title="永久链接至标题"></a></h3>
<p>Windows允许在用户级别和系统级别永久配置环境变量或临时在命令提示符中配置环境变量。</p>
<p>要临时设置环境变量,请打开命令提示符并使用 <strong class="command">set</strong> 命令:</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\&gt;</span><span class="k">set</span> <span class="nv">PATH</span><span class="p">=</span>C:\Program Files\Python 3.8;<span class="nv">%PATH%</span>
<span class="gp">C:\&gt;</span><span class="k">set</span> <span class="nv">PYTHONPATH</span><span class="p">=</span><span class="nv">%PYTHONPATH%</span>;C:\My_python_lib
<span class="gp">C:\&gt;</span>python
</pre></div>
</div>
<p>这些环境变量的更改将应用​​于在该控制台中执行的任何其他命令,并且,由该控制台启动的任何应用程序都继承设这些设置。</p>
<p>在百分号中包含的变量名将被现有值替换,允许在开始或结束时添加新值。通过将包含 <strong class="program">python.exe</strong> 的目录添加到开头来修改 <span class="target" id="index-61"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 是确保启动正确版本的Python的常用方法。</p>
<p>要永久修改默认环境变量,请单击“开始”并搜索“编辑环境变量”,或打开系统属性的 <span class="guilabel">高级系统设置</span> ,然后单击 <span class="guilabel">环境变量</span> 按钮。在此对话框中,您可以添加或修改用户和系统变量。要更改系统变量,您需要对计算机进行无限制访问(即管理员权限)。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>Windows会将用户变量串联在系统变量 <em>之后</em> ,这可能会在修改 <span class="target" id="index-62"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 时导致意外结果。</p>
<p><span class="target" id="index-63"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> 变量被Python 2和Python 3的所有版本使用因此除非它只包含与所有已安装的Python版本兼容的代码否则不要永久配置此变量。</p>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference external" href="https://www.microsoft.com/en-us/wdsi/help/folder-variables">https://www.microsoft.com/en-us/wdsi/help/folder-variables</a></dt><dd><p>Windows NT 的环境变量</p>
</dd>
<dt><a class="reference external" href="https://technet.microsoft.com/en-us/library/cc754250.aspx">https://technet.microsoft.com/en-us/library/cc754250.aspx</a></dt><dd><p>用于临时修改环境变量的SET命令</p>
</dd>
<dt><a class="reference external" href="https://technet.microsoft.com/en-us/library/cc755104.aspx">https://technet.microsoft.com/en-us/library/cc755104.aspx</a></dt><dd><p>用于永久修改环境变量的SETX命令</p>
</dd>
<dt><a class="reference external" href="https://support.microsoft.com/en-us/help/310519/how-to-manage-environment-variables-in-windows-xp">https://support.microsoft.com/en-us/help/310519/how-to-manage-environment-variables-in-windows-xp</a></dt><dd><p>如何在Windows XP中管理环境变量</p>
</dd>
<dt><a class="reference external" href="https://www.chem.gla.ac.uk/~louis/software/faq/q1.html">https://www.chem.gla.ac.uk/~louis/software/faq/q1.html</a></dt><dd><p>设置环境变量For Windows 2000/NTLouis J. Farrugia</p>
</dd>
</dl>
</div>
</section>
<section id="finding-the-python-executable">
<span id="windows-path-mod"></span><h3><span class="section-number">3.6.2. </span>查找Python可执行文件<a class="headerlink" href="#finding-the-python-executable" title="永久链接至标题"></a></h3>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改.</span></p>
</div>
<p>除了使用自动创建的Python解释器的开始菜单项之外您可能还想在命令提示符下启动Python。安装程序有一个选项可以为您设置。</p>
<p>在安装程序的第一页上可以选择标记为“将Python添加到环境变量”的选项以使安装程序将安装位置添加到 <span class="target" id="index-64"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 。还添加了 <code class="file docutils literal notranslate"><span class="pre">Scripts\</span></code> 文件夹的位置。这允许你输入 <strong class="command">python</strong> 来运行解释器,并且 <strong class="command">pip</strong> 用于包安装程序。因此,您还可以使用命令行选项执行脚本,请参阅 <a class="reference internal" href="cmdline.html#using-on-cmdline"><span class="std std-ref">命令行</span></a> 文档。</p>
<p>如果在安装时未启用此选项,则始终可以重新运行安装程序,选择“修改”并启用它。或者,您可以使用 <a class="reference internal" href="#setting-envvars"><span class="std std-ref">附录:设置环境变量</span></a> 的方法手动修改 <span class="target" id="index-65"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 。您需要将Python安装目录添加到 <span class="target" id="index-66"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 环境变量中,该内容与其他条目用分号分隔。示例变量可能如下所示(假设前两个条目已经存在):</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.8
</pre></div>
</div>
</section>
</section>
<section id="utf-8-mode">
<span id="win-utf8-mode"></span><h2><span class="section-number">3.7. </span>UTF-8 模式<a class="headerlink" href="#utf-8-mode" title="永久链接至标题"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
<p>Windows 仍然使用传统编码格式作为系统的编码格式ANSI 代码页)。 Python 使用它作为文本文件默认的编码格式 (即 <a class="reference internal" href="../library/locale.html#locale.getpreferredencoding" title="locale.getpreferredencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getpreferredencoding()</span></code></a>)。</p>
<p>这可能会造成问题,因为因特网和大多数 Unix 系统包括 WSL (Windows Subsystem for Linux) 广泛使用 UTF-8。</p>
<p>你可以使用 UTF-8 模式将默认的文本编码格式改为 UTF-8。 要启用 UTF-8 模式可以通过 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> 命令行选项,或者 <code class="docutils literal notranslate"><span class="pre">PYTHONUTF8=1</span></code> 环境变量。 请参见 <span class="target" id="index-67"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> 了解如何启用 UTF-8 模式,并参见 <a class="reference internal" href="#setting-envvars"><span class="std std-ref">附录:设置环境变量</span></a> 了解如何修改环境变量。</p>
<p>当 UTF-8 模式被启用时:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/locale.html#locale.getpreferredencoding" title="locale.getpreferredencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getpreferredencoding()</span></code></a> 将返回 <code class="docutils literal notranslate"><span class="pre">'UTF-8'</span></code> 而不是系统的编码格式。 此函数可用来在许多场合下获取默认的文本编码格式,包括 <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, <code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.read_text()</span></code> 等等。</p></li>
<li><p><a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>, <a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a><a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> 都将使用 UTF-8 作为其文本编码格式。</p></li>
<li><p>你仍然可以通过 &quot;mbcs&quot; 编解码器来使用系统的编码格式。</p></li>
</ul>
<p>请注意添加 <code class="docutils literal notranslate"><span class="pre">PYTHONUTF8=1</span></code> 到默认环境变量将会影响你的系统中的所有 Python 3.7+ 应用。 如果你有任何 Python 3.7+ 应用仍然依赖于传统的系统编码格式,则推荐设置临时环境变量或使用 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> 命令行选项。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>即使在不启用 UTF-8 模式时Windows 版的 Python 也会在以下情况中默认使用 UTF-8</p>
<ul class="simple">
<li><p>控制台 I/O 包括标准 I/O (详情见 <span class="target" id="index-68"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0528"><strong>PEP 528</strong></a>)。</p></li>
<li><p>文件系统编码格式 (详情见 <span class="target" id="index-69"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0529"><strong>PEP 529</strong></a>)。</p></li>
</ul>
</div>
</section>
<section id="python-launcher-for-windows">
<span id="launcher"></span><h2><span class="section-number">3.8. </span>适用于Windows的Python启动器<a class="headerlink" href="#python-launcher-for-windows" title="永久链接至标题"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<p>用于Windows的Python启动器是一个实用程序可帮助定位和执行不同的Python版本。它允许脚本或命令行指示特定Python版本的首选项并将定位并执行该版本。</p>
<p><span class="target" id="index-70"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 变量不同启动器将正确选择最合适的Python版本。它更倾向于按用户安装而不是系统安装并按语言版本排序而不是使用最新安装的版本。</p>
<p>启动器最初是在 <span class="target" id="index-71"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0397"><strong>PEP 397</strong></a> 中指定的。</p>
<section id="getting-started">
<h3><span class="section-number">3.8.1. </span>入门<a class="headerlink" href="#getting-started" title="永久链接至标题"></a></h3>
<section id="from-the-command-line">
<h4><span class="section-number">3.8.1.1. </span>从命令行<a class="headerlink" href="#from-the-command-line" title="永久链接至标题"></a></h4>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改.</span></p>
</div>
<p>全局安装Python 3.3及更高版本将把启动器放在你的 <span class="target" id="index-72"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 上。启动程序与所有可用的Python版本兼容因此安装哪个版本无关紧要。要检查启动程序是否可用请在命令提示符中执行以下命令</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py
</pre></div>
</div>
<p>您应该会发现已安装的最新版本的Python已启动 - 它可以正常退出并且将指定的任何其他命令行参数直接发送到Python。</p>
<p>如果您安装了多个版本的Python例如2.7和 3.8 您会注意到Python 3.8 启动 - 如果要启动Python 2.7,尝试命令:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py -2.7
</pre></div>
</div>
<p>如果您想使用 Python 2.x 的最新版本,请尝试以下命令:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py -2
</pre></div>
</div>
<p>你会发现Python 2.x 的最新版本已启动。</p>
<p>如果您看到以下错误,则表明您没有安装启动器:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&#39;py&#39; is not recognized as an internal or external command,
operable program or batch file.
</pre></div>
</div>
<p>除非在安装时选择了该选项单个用户安装的Python不会将启动程序添加到 <span class="target" id="index-73"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></p>
</section>
<section id="virtual-environments">
<h4><span class="section-number">3.8.1.2. </span>从虚拟环境<a class="headerlink" href="#virtual-environments" title="永久链接至标题"></a></h4>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
<p>如果启动程序运行时没有明确的Python版本并且虚拟环境使用标准库创建 <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> 模块或外部 <code class="docutils literal notranslate"><span class="pre">virtualenv</span></code> 工具处于活动状态则启动程序将运行虚拟环境的解释器而不是全局的。要运行全局解释器请停用虚拟环境或显式指定全局Python版本。</p>
</section>
<section id="from-a-script">
<h4><span class="section-number">3.8.1.3. </span>从脚本<a class="headerlink" href="#from-a-script" title="永久链接至标题"></a></h4>
<p>让我们创建一个测试Python脚本 - 创建一个名为 <code class="docutils literal notranslate"><span class="pre">hello.py</span></code> 的文件,其中包含以下内容</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ch">#! python</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;hello from Python </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">version</span><span class="p">,))</span>
</pre></div>
</div>
<p>从hello.py所在的目录中执行以下命令</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py hello.py
</pre></div>
</div>
<p>您应该注意到最新的Python 2.x安装的版本号已打印出来。现在尝试将第一行更改为</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ch">#! python3</span>
</pre></div>
</div>
<p>现在重新执行该命令应该打印最新的Python 3.x信息。与上面的命令行示例一样你可以更明确的指定版本限定符。假设您安装了Python 2.6,请尝试将第一行更改为 <code class="docutils literal notranslate"><span class="pre">#!</span> <span class="pre">python2.6</span></code> 你会发现打印的2.6版本信息。</p>
<p>请注意与交互式使用不同裸“python”将使用您已安装的Python 2.x的最新版本。这是为了向后兼容及兼容Unix其中命令 <code class="docutils literal notranslate"><span class="pre">python</span></code> 通常是指Python 2。</p>
</section>
<section id="from-file-associations">
<h4><span class="section-number">3.8.1.4. </span>从文件关联<a class="headerlink" href="#from-file-associations" title="永久链接至标题"></a></h4>
<p>安装时应该将启动器与Python文件<code class="docutils literal notranslate"><span class="pre">.py</span></code>, <code class="docutils literal notranslate"><span class="pre">.pyw</span></code>, <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件相关联。这意味着当您从Windows资源管理器中双击其中一个文件时将使用启动程序因此您可以使用上述相同的工具让脚本指定应使用的版本。</p>
<p>这样做的主要好处是单个启动程序可以同时支持多个Python版本具体取决于第一行的内容。</p>
</section>
</section>
<section id="shebang-lines">
<h3><span class="section-number">3.8.2. </span>Shebang 行<a class="headerlink" href="#shebang-lines" title="永久链接至标题"></a></h3>
<p>如果脚本文件的第一行以 <code class="docutils literal notranslate"><span class="pre">#!</span></code> 开头,则称为 &quot;shebang&quot; 行。Linux和其他类Unix操作系统都有对这些行的本机支持它们通常在此类系统上用来指示应该如何执行脚本。这个启动器允许在Windows上对Python脚本使用相同的工具上面的示例演示了它们的使用。</p>
<p>为了允许Python脚本中的shebang行在Unix和Windows之间移植该启动器支持许多“虚拟”命令来指定要使用的解释器。支持的虚拟命令是</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">/usr/bin/env</span> <span class="pre">python</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">/usr/bin/python</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">/usr/local/bin/python</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">python</span></code></p></li>
</ul>
<p>例如,如果脚本开始的第一行为</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="ch">#! /usr/bin/python</span>
</pre></div>
</div>
<p>将找到并使用默认的Python。因为在Unix上编写的许多Python脚本已经有了这一行你应该发现这些脚本可以由启动器使用而无需修改。如果您在Windows上编写一个新脚本希望在Unix上有用那么您应该使用以 <code class="docutils literal notranslate"><span class="pre">/usr</span></code> 开头的一个shebang行。</p>
<p>任何上述虚拟命令都可以显式指定版本(可以仅为主要版本,也可以为主要版本加次要版本)作为后缀。 此外,可以通过在次要版本之后添加 “-32” 来请求 32 位版本。 例如 <code class="docutils literal notranslate"><span class="pre">/usr/bin/python2.7-32</span></code> 将请求使用 32 位 python 2.7。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能: </span>从 python 启动器 3.7 开始,可以通过 &quot;-64&quot; 后缀调用 64 位版本。 此外还可以指定一个主版本号加架构而不带次版本号 (即 <code class="docutils literal notranslate"><span class="pre">/usr/bin/python3-64</span></code>)。</p>
</div>
<p>shebang line的 <code class="docutils literal notranslate"><span class="pre">/usr/bin/env</span></code> 形式还有一个特殊属性。在寻找已安装的Python解释器之前此表单将搜索可执行文件 <span class="target" id="index-74"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 以获取Python可执行文件。这对应于Unix中 <code class="docutils literal notranslate"><span class="pre">env</span></code> 程序的行为,该程序将在 <span class="target" id="index-75"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 执行搜索。</p>
</section>
<section id="arguments-in-shebang-lines">
<h3><span class="section-number">3.8.3. </span>shebang 行的参数<a class="headerlink" href="#arguments-in-shebang-lines" title="永久链接至标题"></a></h3>
<p>shebang 行还可以指定要传递给Python解释器的其他选项。 举例来说,如果你有这样的 shebang 行:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="ch">#! /usr/bin/python -v</span>
</pre></div>
</div>
<p>那么 Python 将以 <code class="docutils literal notranslate"><span class="pre">-v</span></code> 选项启动</p>
</section>
<section id="customization">
<h3><span class="section-number">3.8.4. </span>自定义<a class="headerlink" href="#customization" title="永久链接至标题"></a></h3>
<section id="customization-via-ini-files">
<h4><span class="section-number">3.8.4.1. </span>通过INI文件自定义<a class="headerlink" href="#customization-via-ini-files" title="永久链接至标题"></a></h4>
<p>启动程序将搜索两个.ini文件 - 在当前用户的 &quot;application data&quot; 目录中搜索 <code class="docutils literal notranslate"><span class="pre">py.ini</span></code> (即通过使用 <code class="docutils literal notranslate"><span class="pre">CSIDL_LOCAL_APPDATA</span></code> 调用Windows函数 <code class="docutils literal notranslate"><span class="pre">SHGetFolderPath</span></code> 返回的目录)以及与启动器位于同一目录中的 <code class="docutils literal notranslate"><span class="pre">py.ini</span></code> 。相同的.ini文件既用于启动器的“控制台”版本即 py.exe也用于“windows”版本即pyw.exe</p>
<p>“应用程序目录”中指定的自定义优先于可执行文件旁边.ini文件的自定义因此对启动程序旁边的.ini文件不具有写访问权限的用户可以覆盖该全局.ini文件中的命令。</p>
</section>
<section id="customizing-default-python-versions">
<h4><span class="section-number">3.8.4.2. </span>自定义默认的Python版本<a class="headerlink" href="#customizing-default-python-versions" title="永久链接至标题"></a></h4>
<p>在某些情况下可以在命令中包含版本限定符以指定命令将使用哪个Python版本。版本限定符以主版本号开头可以选择后跟 ('.') 和次版本说明符。此外,可以通过添加 &quot;-32&quot; 或 “-64” 来指定是请求32位还是64位实现。</p>
<p>例如一个shebang 行的 <code class="docutils literal notranslate"><span class="pre">#!python</span></code> 行没有版本限定符,而 <code class="docutils literal notranslate"><span class="pre">#!python3</span></code> 有一个版本限定符,它只指定一个主版本。</p>
<p>如果在命令中找不到版本限定符,则可以设置环境变量 <span class="target" id="index-76"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PY_PYTHON</span></code> 以指定默认版本限定符。 如果未设置,则默认为 &quot;3&quot;。 该变量可以指定能通过命令行传递的任何值,比如 &quot;3&quot;, &quot;3.7&quot;, &quot;3.7-32&quot;&quot;3.7-64&quot;。 (请注意 &quot;-64&quot; 选项仅适用于 Python 3.7 或更高版本中包含的启动器。)</p>
<p>如果没有找到次版本限定符,则可以设置环境变量 <code class="docutils literal notranslate"><span class="pre">PY_PYTHON{major}</span></code> (其中 <code class="docutils literal notranslate"><span class="pre">{major}</span></code> 是上面确定的当前主要版本限定符以指定完整版本。如果没有找到这样的选项启动器将枚举已安装的Python版本并使用为主要版本找到的最新次要版本尽管不能保证但该版本可能是该系列中最新安装的版本。</p>
<p>在安装了相同major.minorPython版本的32位和64位的64位Windows上64位版本将始终是首选。对于启动程序的32位和64位实现都是如此 -- 这对于启动程序32位和64位都是正确的 -- 如果可用32位启动程序将倾向于执行指定版本的64位Python安装。这样就可以预测启动器的行为只知道PC上安装了哪些版本而不考虑它们的安装顺序不知道32位或64位版本的Python和相应的启动器是否是最后安装。如上所述可以在版本说明符上使用可选的“-32”或“-64”后缀来更改此行为。</p>
<p>示例:</p>
<ul class="simple">
<li><p>如果没有设置相关选项,命令 <code class="docutils literal notranslate"><span class="pre">python</span></code><code class="docutils literal notranslate"><span class="pre">python2</span></code> 将使用安装的最新Python 2.x版本命令 <code class="docutils literal notranslate"><span class="pre">python3</span></code> 将使用最新安装的Python 3.x.</p></li>
<li><p>命令 <code class="docutils literal notranslate"><span class="pre">python3.1</span></code><code class="docutils literal notranslate"><span class="pre">python2.7</span></code> 根本不会查阅任何选项,因为版本已完全指定。</p></li>
<li><p>如果 <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code> ,命令 <code class="docutils literal notranslate"><span class="pre">python</span></code><code class="docutils literal notranslate"><span class="pre">python3</span></code> 都将使用最新安装的Python 3版本。</p></li>
<li><p>如果 <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3.1-32</span></code> ,命令 <code class="docutils literal notranslate"><span class="pre">python</span></code> 将使用3.1的32位实现而命令 <code class="docutils literal notranslate"><span class="pre">python3</span></code> 将使用最新安装的PythonPY_PYTHON根本没有被视为指定了主要版本。</p></li>
<li><p>如果 <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code><code class="docutils literal notranslate"><span class="pre">PY_PYTHON3=3.1</span></code>,命令 <code class="docutils literal notranslate"><span class="pre">python</span></code><code class="docutils literal notranslate"><span class="pre">python3</span></code> 都将特别使用3.1</p></li>
</ul>
<p>除环境变量外,还可以在启动程序使用的.INI文件中配置相同的设置。 INI文件中的部分称为 <code class="docutils literal notranslate"><span class="pre">[defaults]</span></code> ,键名称将与没有前导 <code class="docutils literal notranslate"><span class="pre">PY_</span></code> 前缀的环境变量相同并注意INI文件中的键名不区分大小写环境变量的内容将覆盖INI文件中指定的内容。</p>
<p>例如:</p>
<ul class="simple">
<li><p>设置 <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3.1</span></code> 等同于包含以下内容的INI文件</p></li>
</ul>
<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[defaults]</span>
<span class="na">python</span><span class="o">=</span><span class="s">3.1</span>
</pre></div>
</div>
<ul class="simple">
<li><p>设置 <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code><code class="docutils literal notranslate"><span class="pre">PY_PYTHON3=3.1</span></code> 相当于包含以下内容的INI文件</p></li>
</ul>
<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[defaults]</span>
<span class="na">python</span><span class="o">=</span><span class="s">3</span>
<span class="na">python3</span><span class="o">=</span><span class="s">3.1</span>
</pre></div>
</div>
</section>
</section>
<section id="diagnostics">
<h3><span class="section-number">3.8.5. </span>诊断<a class="headerlink" href="#diagnostics" title="永久链接至标题"></a></h3>
<p>如果设置了环境变量 <code class="docutils literal notranslate"><span class="pre">PYLAUNCH_DEBUG</span></code> 任何值启动器将诊断信息打印到stderr控制台。虽然这些信息同时具有冗长 <em></em> 简洁性但它应该允许您查看Python的版本、选择特定版本的原因以及用于执行目标Python的确切命令行。</p>
</section>
</section>
<section id="finding-modules">
<span id="id1"></span><h2><span class="section-number">3.9. </span>查找模块<a class="headerlink" href="#finding-modules" title="永久链接至标题"></a></h2>
<p>Python通常将其库以及您的site-packages文件夹存储在安装目录中。因此如果您已将Python安装到 <code class="file docutils literal notranslate"><span class="pre">C:\Python\</span></code> ,则默认库将驻留在 <code class="file docutils literal notranslate"><span class="pre">C:\Python\Lib\</span></code> 中,第三方模块存储在 <code class="file docutils literal notranslate"><span class="pre">C:\Python\Lib\site-packages\</span></code></p>
<p>若要完全覆盖 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 请创建与DLL(<code class="docutils literal notranslate"><span class="pre">python37._pth</span></code>)或可执行文件(“python._pth`”)同名的 <code class="docutils literal notranslate"><span class="pre">._pth</span></code> 文件,并为要添加的每个路径指定一行 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 。基于DLL名称的文件覆盖基于可执行文件的文件如果需要可以为加载运行时的任何程序限制路径。</p>
<p>当文件存在时,将忽略所有注册表和环境变量,启用隔离模式,并且:除非文件中的一行指定 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code> ,否则不会导入 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 。以 <code class="docutils literal notranslate"><span class="pre"></span></code> 开头的空白路径和行将被忽略。每个路径可以是绝对的或相对于文件的位置。不允许使用除 <code class="docutils literal notranslate"><span class="pre">site</span></code> 以外的导入语句,并且不能指定任意代码。</p>
<p>请注意,当指定 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code> 时, <code class="docutils literal notranslate"><span class="pre">.pth</span></code> 文件(没有前导下划线)将由 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 模块正常处理。</p>
<p>当找不到 <code class="docutils literal notranslate"><span class="pre">._pth</span></code> 文件时, <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 是如何在Windows上填充的</p>
<ul class="simple">
<li><p>在开始时,添加一个空条目,该条目对应于当前目录。</p></li>
<li><p>如果环境变量 <span class="target" id="index-77"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> 存在,如 <a class="reference internal" href="cmdline.html#using-on-envvars"><span class="std std-ref">环境变量</span></a> 中所述则接下来添加其条目。请注意在Windows上此变量中的路径必须用分号分隔以区别于驱动器标识符中使用的冒号 <code class="docutils literal notranslate"><span class="pre">C:\</span></code> 等)。</p></li>
<li><p>额外的 &quot;应用程序路径&quot; 可以作为子键被同时添加到注册表 <code class="docutils literal notranslate"><span class="pre">HKEY_CURRENT_USER</span></code><code class="docutils literal notranslate"><span class="pre">HKEY_LOCAL_MACHINE</span></code> 分支下的 <code class="samp docutils literal notranslate"><span class="pre">\SOFTWARE\Python\PythonCore{version}\PythonPath</span></code> 中。 以分号分隔的路径字符串作为默认值的子键将导致每个路径都被添加到 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 中。 (请注意所有已知的安装程序都只使用 HKLM因此 HKCU 通常为空。)</p></li>
<li><p>如果设置了环境变量 <span class="target" id="index-78"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> ,则将其假定为 “Python 主目录” 。否则主Python可执行文件的路径用于定位 “landmark 文件” <code class="docutils literal notranslate"><span class="pre">Lib\os.py</span></code><code class="docutils literal notranslate"><span class="pre">pythonXY.zip</span></code> )以推断 ”Python 主目录“ 。如果找到了Python主目录则基于该文件夹将相关的子目录添加到 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> <code class="docutils literal notranslate"><span class="pre">Lib</span></code> , <code class="docutils literal notranslate"><span class="pre">plat-win</span></code>。否则核心Python路径是从存储在注册表中的PythonPath构造的。</p></li>
<li><p>如果找不到Python Home也没有指定 <span class="target" id="index-79"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> 环境变量,并且找不到注册表项,则使用具有相对条目的默认路径(例如 <code class="docutils literal notranslate"><span class="pre">.\Lib;</span> <span class="pre">.\plat-win</span></code> 等等)。</p></li>
</ul>
<p>如果在主可执行文件旁边或在可执行文件上一级的目录中找到 <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> 文件,则以下变体适用:</p>
<ul class="simple">
<li><p>如果 <code class="docutils literal notranslate"><span class="pre">home</span></code> 是一个绝对路径,并且 <span class="target" id="index-80"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> 未设置,则在推断起始位置时使用此路径而不是主可执行文件的路径。</p></li>
</ul>
<p>这一切的最终结果是:</p>
<ul class="simple">
<li><p>运行 <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> 或主Python目录中的任何其他.exe安装版本或直接来自PCbuild目录推导出核心路径并忽略注册表中的核心路径。始终读取注册表中的其他“应用程序路径”。</p></li>
<li><p>当Python托管在另一个.exe不同的目录通过COM嵌入等将不会推断出“Python Home”因此使用了来自注册表的核心路径。始终读取注册表中的其他“应用程序路径”。</p></li>
<li><p>如果Python找不到它的主目录并且没有注册表值冻结的.exe一些非常奇怪的安装设置那么你会得到一条带有一些默认但相对的路径的路径。</p></li>
</ul>
<p>对于那些想要将Python捆绑到其应用程序或发行版中的人以下建议将防止与其他安装冲突</p>
<ul class="simple">
<li><p>在您的可执行文件中包含一个 <code class="docutils literal notranslate"><span class="pre">._pth</span></code> 文件,其中包含目录。这将忽略注册表和环境变量中列出的路径,并忽略 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> ,除非列出 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code></p></li>
<li><p>如果你在自己的可执行文件中加载 <code class="file docutils literal notranslate"><span class="pre">python3.dll</span></code><code class="file docutils literal notranslate"><span class="pre">python37.dll</span></code> ,在 <a class="reference internal" href="../c-api/init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> 之前,要显式调用 <a class="reference internal" href="../c-api/init.html#c.Py_SetPath" title="Py_SetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetPath()</span></code></a> 或(至少) <a class="reference internal" href="../c-api/init.html#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a></p></li>
<li><p>清除 和/或 覆盖 <span class="target" id="index-81"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> 并在启动来自应用程序的 <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> 之前设置 <span class="target" id="index-82"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a></p></li>
<li><p>如果您不能使用前面的建议(例如,您是一个允许人们直接运行 <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> 的分发版),请确保安装目录中存在 landmark 文件 (<code class="file docutils literal notranslate"><span class="pre">Lib\os.py</span></code>)。 (请注意,在 ZIP 文件中不会检测到该文件,但会检测到正确命名的 ZIP 文件。)</p></li>
</ul>
<p>这些将确保系统范围安装中的文件不会优先于与应用程序捆绑在一起的标准库的副本。否则,用户可能会在使用您的应用程序时遇到问题请注意,第一个建议是最好的,因为其他建议可能仍然容易受到注册表和用户站点包中的非标准路径的影响。</p>
<div class="versionchanged">
<blockquote>
<div><span class="versionmodified changed">在 3.6 版更改: </span><ul class="simple">
<li><p>添加 <code class="docutils literal notranslate"><span class="pre">._pth</span></code> 文件支持并从 <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> 中删除 <code class="docutils literal notranslate"><span class="pre">applocal</span></code> 选项</p></li>
<li><p>当直接与可执行文件相邻时,添加 <code class="docutils literal notranslate"><span class="pre">pythonXX.zip</span></code> 作为潜在的 landmark 。</p></li>
</ul>
</div></blockquote>
</div>
<div class="deprecated">
<blockquote>
<div><span class="versionmodified deprecated">3.6 版后已移除: </span><p><code class="docutils literal notranslate"><span class="pre">Modules</span></code> (不是 <code class="docutils literal notranslate"><span class="pre">PythonPath</span></code> )下的注册表中指定的模块可以通过以下方式导入 <a class="reference internal" href="../library/importlib.html#importlib.machinery.WindowsRegistryFinder" title="importlib.machinery.WindowsRegistryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.WindowsRegistryFinder</span></code></a> 。在Windows上此查找程序在3.6.0及更早版本的可用,但可能需要在将来显式添加到 <a class="reference internal" href="../library/sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a></p>
</div></blockquote>
</div>
</section>
<section id="additional-modules">
<h2><span class="section-number">3.10. </span>附加模块<a class="headerlink" href="#additional-modules" title="永久链接至标题"></a></h2>
<p>尽管Python的目标是在所有平台中都可移植但是Windows有一些独特的特性。在标准库和外部都有一些模块和代码片段在使用这些特性。</p>
<p>特定于Windows的标准模块记录在 <a class="reference internal" href="../library/windows.html#mswin-specific-services"><span class="std std-ref">Windows系统相关模块</span></a> 中。</p>
<section id="pywin32">
<h3><span class="section-number">3.10.1. </span>PyWin32<a class="headerlink" href="#pywin32" title="永久链接至标题"></a></h3>
<p>Mark Hammond 的 <a class="reference external" href="https://pypi.org/project/pywin32">PyWin32</a> 模块是一组用于高级Windows特定支持的模块。这包括以下实用程序</p>
<ul class="simple">
<li><p><a class="reference external" href="https://docs.microsoft.com/en-us/windows/desktop/com/component-object-model--com--portal">组件对象模型</a> (COM)</p></li>
<li><p>Win32 API 调用</p></li>
<li><p>注册</p></li>
<li><p>事件日志</p></li>
<li><p><a class="reference external" href="https://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx">Microsoft Foundation Classes</a> (MFC) 用户界面</p></li>
</ul>
<p><a class="reference external" href="https://web.archive.org/web/20060524042422/https://www.python.org/windows/pythonwin/">PythonWin</a> 是PyWin32附带的一个示例MFC应用程序。它是一个内置调试器的可嵌入IDE。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference external" href="http://timgolden.me.uk/python/win32_how_do_i.html">Win32 How Do I...?</a></dt><dd><p>Tim Golden 著</p>
</dd>
<dt><a class="reference external" href="http://www.boddie.org.uk/python/COM.html">Python and COM</a></dt><dd><p>David 和 Paul Boddie 著</p>
</dd>
</dl>
</div>
</section>
<section id="cx-freeze">
<h3><span class="section-number">3.10.2. </span>cx_Freeze<a class="headerlink" href="#cx-freeze" title="永久链接至标题"></a></h3>
<p><a class="reference external" href="https://cx-freeze.readthedocs.io/en/latest/">cx_Freeze</a> 是一个 <a class="reference internal" href="../library/distutils.html#module-distutils" title="distutils: Support for building and installing Python modules into an existing Python installation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">distutils</span></code></a> 的扩展 (参见 <a class="reference internal" href="../distutils/extending.html#extending-distutils"><span class="std std-ref">扩展 Distutils</span></a>),它将 Python 脚本包装成可执行的 Windows 程序 (<code class="file docutils literal notranslate"><em><span class="pre">*</span></em><span class="pre">.exe</span></code> 文件)。 完成此操作后,你就可以分发你的应用程序而无需用户安装 Python。</p>
</section>
<section id="wconio">
<h3><span class="section-number">3.10.3. </span>WConio<a class="headerlink" href="#wconio" title="永久链接至标题"></a></h3>
<p>由于Python的高级终端处理层 <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> 仅限于类Unix系统因此Windows还有一个独立的库用于Python的Windows 控制台 I/O .</p>
<p><a class="reference external" href="http://newcenturycomputers.net/projects/wconio.html">WConio</a> 是Turbo-C的 <code class="file docutils literal notranslate"><span class="pre">CONIO.H</span></code> 装饰器,用于创建文本用户界面。</p>
</section>
</section>
<section id="compiling-python-on-windows">
<h2><span class="section-number">3.11. </span>在Windows上编译Python<a class="headerlink" href="#compiling-python-on-windows" title="永久链接至标题"></a></h2>
<p>如果你想自己编译CPython首先要做的是获取 <a class="reference external" href="https://www.python.org/downloads/source/">source</a>. 。您可以下载最新版本的源代码,也可以重新签出 <a class="reference external" href="https://devguide.python.org/setup/#getting-the-source-code">checkout</a>. 。</p>
<p>源代码树包含Microsoft Visual Studio 2015的构建解决方案和项目文件它是用于构建官方Python版本的编译器。这些文件位于 <code class="file docutils literal notranslate"><span class="pre">PCbuild</span></code> 目录中。</p>
<p>检查 <code class="file docutils literal notranslate"><span class="pre">PCbuild/readme.txt</span></code> 以获取有关构建过程的一般信息。</p>
<p>有关扩展模块,请参阅 <a class="reference internal" href="../extending/windows.html#building-on-windows"><span class="std std-ref">在Windows平台编译C和C++扩展</span></a></p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference external" href="http://sebsauvage.net/python/mingw.html">Python + Windows + distutils + SWIG + gcc MinGW</a></dt><dd><p>&quot;Creating Python extensions in C/C++ with SWIG and compiling them with MinGW gcc under Windows&quot;&quot;Installing Python extension with distutils and without Microsoft Visual C++&quot; by Sébastien Sauvage, 2003</p>
</dd>
</dl>
</div>
</section>
<section id="other-platforms">
<h2><span class="section-number">3.12. </span>其他平台<a class="headerlink" href="#other-platforms" title="永久链接至标题"></a></h2>
<p>随着Python的不断发展不再支持以前曾经支持的一些平台由于缺少用户或开发人员。检查 <span class="target" id="index-83"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0011"><strong>PEP 11</strong></a> 了解所有不支持的平台的详细信息。</p>
<ul class="simple">
<li><p><a class="reference external" href="http://pythonce.sourceforge.net/">Windows CE</a> 仍然受支持。</p></li>
<li><p><a class="reference external" href="https://cygwin.com/">Cygwin</a> 安装包也提供安装Python解释器 (cf. <a class="reference external" href="ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/release/python">Cygwin package source</a>, <a class="reference external" href="http://www.tishler.net/jason/software/python/">Maintainer releases</a>)</p></li>
</ul>
<p>有关具有预编译安装程序平台的详细信息,请参阅 <a class="reference external" href="https://www.python.org/downloads/windows/">Python for Windows</a></p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">3. 在Windows上使用 Python</a><ul>
<li><a class="reference internal" href="#the-full-installer">3.1. 完整安装程序</a><ul>
<li><a class="reference internal" href="#installation-steps">3.1.1. 安装步骤</a></li>
<li><a class="reference internal" href="#removing-the-max-path-limitation">3.1.2. 删除 MAX_PATH 限制</a></li>
<li><a class="reference internal" href="#installing-without-ui">3.1.3. 无UI 安装</a></li>
<li><a class="reference internal" href="#installing-without-downloading">3.1.4. 免下载安装</a></li>
<li><a class="reference internal" href="#modifying-an-install">3.1.5. 修改安装</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-microsoft-store-package">3.2. Microsoft Store包</a><ul>
<li><a class="reference internal" href="#known-issues">3.2.1. 已知的问题</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-nuget-org-packages">3.3. nuget.org 安装包</a></li>
<li><a class="reference internal" href="#the-embeddable-package">3.4. 可嵌入的包</a><ul>
<li><a class="reference internal" href="#python-application">3.4.1. Python 应用程序</a></li>
<li><a class="reference internal" href="#embedding-python">3.4.2. 嵌入Python</a></li>
</ul>
</li>
<li><a class="reference internal" href="#alternative-bundles">3.5. 替代捆绑包</a></li>
<li><a class="reference internal" href="#configuring-python">3.6. 配置Python</a><ul>
<li><a class="reference internal" href="#excursus-setting-environment-variables">3.6.1. 附录:设置环境变量</a></li>
<li><a class="reference internal" href="#finding-the-python-executable">3.6.2. 查找Python可执行文件</a></li>
</ul>
</li>
<li><a class="reference internal" href="#utf-8-mode">3.7. UTF-8 模式</a></li>
<li><a class="reference internal" href="#python-launcher-for-windows">3.8. 适用于Windows的Python启动器</a><ul>
<li><a class="reference internal" href="#getting-started">3.8.1. 入门</a><ul>
<li><a class="reference internal" href="#from-the-command-line">3.8.1.1. 从命令行</a></li>
<li><a class="reference internal" href="#virtual-environments">3.8.1.2. 从虚拟环境</a></li>
<li><a class="reference internal" href="#from-a-script">3.8.1.3. 从脚本</a></li>
<li><a class="reference internal" href="#from-file-associations">3.8.1.4. 从文件关联</a></li>
</ul>
</li>
<li><a class="reference internal" href="#shebang-lines">3.8.2. Shebang 行</a></li>
<li><a class="reference internal" href="#arguments-in-shebang-lines">3.8.3. shebang 行的参数</a></li>
<li><a class="reference internal" href="#customization">3.8.4. 自定义</a><ul>
<li><a class="reference internal" href="#customization-via-ini-files">3.8.4.1. 通过INI文件自定义</a></li>
<li><a class="reference internal" href="#customizing-default-python-versions">3.8.4.2. 自定义默认的Python版本</a></li>
</ul>
</li>
<li><a class="reference internal" href="#diagnostics">3.8.5. 诊断</a></li>
</ul>
</li>
<li><a class="reference internal" href="#finding-modules">3.9. 查找模块</a></li>
<li><a class="reference internal" href="#additional-modules">3.10. 附加模块</a><ul>
<li><a class="reference internal" href="#pywin32">3.10.1. PyWin32</a></li>
<li><a class="reference internal" href="#cx-freeze">3.10.2. cx_Freeze</a></li>
<li><a class="reference internal" href="#wconio">3.10.3. WConio</a></li>
</ul>
</li>
<li><a class="reference internal" href="#compiling-python-on-windows">3.11. 在Windows上编译Python</a></li>
<li><a class="reference internal" href="#other-platforms">3.12. 其他平台</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="unix.html"
title="上一章"><span class="section-number">2. </span>在类Unix环境下使用Python</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="mac.html"
title="下一章"><span class="section-number">4. </span>在苹果系统上使用 Python</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/using/windows.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="mac.html" title="4. 在苹果系统上使用 Python"
>下一页</a> |</li>
<li class="right" >
<a href="unix.html" title="2. 在类Unix环境下使用Python"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python安装和使用</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>