增加python文档
This commit is contained in:
975
web/python-docs/using/cmdline.html
Normal file
975
web/python-docs/using/cmdline.html
Normal file
@@ -0,0 +1,975 @@
|
||||
|
||||
<!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>1. 命令行与环境 — 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="2. 在类Unix环境下使用Python" href="unix.html" />
|
||||
<link rel="prev" title="Python安装和使用" href="index.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/using/cmdline.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="#">1. 命令行与环境</a><ul>
|
||||
<li><a class="reference internal" href="#command-line">1.1. 命令行</a><ul>
|
||||
<li><a class="reference internal" href="#interface-options">1.1.1. 接口选项</a></li>
|
||||
<li><a class="reference internal" href="#generic-options">1.1.2. 通用选项</a></li>
|
||||
<li><a class="reference internal" href="#miscellaneous-options">1.1.3. 其他选项</a></li>
|
||||
<li><a class="reference internal" href="#options-you-shouldn-t-use">1.1.4. 不应当使用的选项</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#environment-variables">1.2. 环境变量</a><ul>
|
||||
<li><a class="reference internal" href="#debug-mode-variables">1.2.1. 调试模式变量</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="index.html"
|
||||
title="上一章">Python安装和使用</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="unix.html"
|
||||
title="下一章"><span class="section-number">2. </span>在类Unix环境下使用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/cmdline.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/cmdline.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="unix.html" title="2. 在类Unix环境下使用Python"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="index.html" title="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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python安装和使用</a> »</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="command-line-and-environment">
|
||||
<span id="using-on-general"></span><h1><span class="section-number">1. </span>命令行与环境<a class="headerlink" href="#command-line-and-environment" title="永久链接至标题">¶</a></h1>
|
||||
<p>为获取各种设置信息,CPython 解析器会扫描命令行与环境。</p>
|
||||
<div class="impl-detail compound">
|
||||
<p><strong>CPython implementation detail:</strong> 其他实现的命令行方案可能会有所不同。 详见 <a class="reference internal" href="../reference/introduction.html#implementations"><span class="std std-ref">其他实现</span></a>。</p>
|
||||
</div>
|
||||
<section id="command-line">
|
||||
<span id="using-on-cmdline"></span><h2><span class="section-number">1.1. </span>命令行<a class="headerlink" href="#command-line" title="永久链接至标题">¶</a></h2>
|
||||
<p>调用 Python 时,可以指定下列任意选项:</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span><span class="o">[</span>-bBdEhiIOqsSuvVWx?<span class="o">]</span><span class="w"> </span><span class="o">[</span>-c<span class="w"> </span><span class="nb">command</span><span class="w"> </span><span class="p">|</span><span class="w"> </span>-m<span class="w"> </span>module-name<span class="w"> </span><span class="p">|</span><span class="w"> </span>script<span class="w"> </span><span class="p">|</span><span class="w"> </span>-<span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="o">[</span>args<span class="o">]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>最常见的用例是启动时执行脚本:</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>myscript.py
|
||||
</pre></div>
|
||||
</div>
|
||||
<section id="interface-options">
|
||||
<span id="using-on-interface-options"></span><h3><span class="section-number">1.1.1. </span>接口选项<a class="headerlink" href="#interface-options" title="永久链接至标题">¶</a></h3>
|
||||
<p>解释器接口类似于 UNIX shell,但提供了额外的调用方法:</p>
|
||||
<ul class="simple">
|
||||
<li><p>用连接到 tty 设备的标准输入调用时,会提示输入并执行命令,输入 EOF (文件结束符,UNIX 中按 <kbd class="kbd docutils literal notranslate">Ctrl-D</kbd>,Windows 中按 <kbd class="kbd docutils literal notranslate">Ctrl-Z, Enter</kbd>)时终止。</p></li>
|
||||
<li><p>用文件名参数或以标准输入文件调用时,读取,并执行该脚本文件。</p></li>
|
||||
<li><p>用目录名参数调用时,从该目录读取、执行适当名称的脚本。</p></li>
|
||||
<li><p>用 <code class="docutils literal notranslate"><span class="pre">-c</span> <span class="pre">command</span></code> 调用时,执行 <em>command</em> 表示的 Python 语句。<em>command</em> 可以包含用换行符分隔的多条语句。注意,前导空白字符在 Python 语句中非常重要!</p></li>
|
||||
<li><p>用 <code class="docutils literal notranslate"><span class="pre">-m</span> <span class="pre">module-name</span></code> 调用时,在 Python 模块路径中查找指定的模块,并将其作为脚本执行。</p></li>
|
||||
</ul>
|
||||
<p>非交互模式下,先解析全部输入,再执行。</p>
|
||||
<p>接口选项会终结解释器读入的选项列表,所有后续参数都在 <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 里 -- 注意,首个元素,即下标为零的元素(<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code>)是表示程序来源的字符串。</p>
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-c">
|
||||
<code class="sig-name descname">-c</code><code class="sig-prename descclassname"> <command></code><a class="headerlink" href="#cmdoption-c" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>执行 <em>command</em> 中的 Python 代码。<em>command</em> 可以是一条语句,也可以是用换行符分隔的多条语句,其中,前导空白字符与普通模块代码中的作用一样。</p>
|
||||
<p>使用此选项时,<a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 的首个元素为 <code class="docutils literal notranslate"><span class="pre">"-c"</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> 开头(让该目录中的模块作为顶层模块导入)。</p>
|
||||
<p class="audit-hook">引发一个 <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.run_command</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">command</span></code>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-m">
|
||||
<code class="sig-name descname">-m</code><code class="sig-prename descclassname"> <module-name></code><a class="headerlink" href="#cmdoption-m" title="永久链接至目标">¶</a></dt>
|
||||
<dd><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> 中搜索指定模块,并以 <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> 模块执行其内容。</p>
|
||||
<p>该参数是 <em>模块名</em>,请勿输入文件扩展名(<code class="docutils literal notranslate"><span class="pre">.py</span></code>)。模块名应为有效的绝对 Python 模块名,但本实现对此不作强制要求(例如,允许使用含连字符 <code class="docutils literal notranslate"><span class="pre">-</span></code> 的名称)。</p>
|
||||
<p>包名称(包括命名空间包)也允许使用。使用包名称而不是普通模块名时,解释器把 <code class="docutils literal notranslate"><span class="pre"><pkg>.__main__</span></code> 作为主模块执行。此行为特意被设计为与作为脚本参数传递给解释器的目录和 zip 文件的处理方式类似。</p>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">注解</p>
|
||||
<p>此选项不适用于内置模块和以 C 编写的扩展模块,因为它们并没有对应的 Python 模块文件。 但是它仍然适用于预编译的模块,即使没有可用的初始源文件。</p>
|
||||
</div>
|
||||
<p>如果给出此选项,<a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 的首个元素将为模块文件的完整路径 (在定位模块文件期间,首个元素将设为 <code class="docutils literal notranslate"><span class="pre">"-m"</span></code>)。 与 <a class="reference internal" href="#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> 选项一样,当前目录将被加入 <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> 的开头。</p>
|
||||
<p><a class="reference internal" href="#id2"><code class="xref std std-option docutils literal notranslate"><span class="pre">-I</span></code></a> 选项可用来在隔离模式下运行脚本,此模式中 <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> 既不包含当前目录也不包含用户的 site-packages 目录。 所有 <span class="target" id="index-90"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHON*</span></code> 环境变量也会被忽略。</p>
|
||||
<p>许多标准库模块都包含在执行时,以脚本方式调用的代码。例如 <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code></a> 模块:</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>timeit<span class="w"> </span>-s<span class="w"> </span><span class="s1">'setup here'</span><span class="w"> </span><span class="s1">'benchmarked code here'</span>
|
||||
python<span class="w"> </span>-m<span class="w"> </span>timeit<span class="w"> </span>-h<span class="w"> </span><span class="c1"># for details</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p class="audit-hook">引发一个 <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.run_module</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">module-name</span></code>。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><a class="reference internal" href="../library/runpy.html#runpy.run_module" title="runpy.run_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">runpy.run_module()</span></code></a></dt><dd><p>Python 代码可以直接使用的等效功能</p>
|
||||
</dd>
|
||||
</dl>
|
||||
<p><span class="target" id="index-91"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0338"><strong>PEP 338</strong></a> -- 将模块作为脚本执行</p>
|
||||
</div>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.1 版更改: </span>提供包名称来运行 <code class="docutils literal notranslate"><span class="pre">__main__</span></code> 子模块。</p>
|
||||
</div>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.4 版更改: </span>同样支持命名空间包</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<span class="target" id="cmdarg-dash"></span><dl class="describe">
|
||||
<dt>
|
||||
<code class="sig-name descname">-</code></dt>
|
||||
<dd><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="#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a>。</p>
|
||||
<p>使用此选项时,<a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 的第一个元素是 <code class="docutils literal notranslate"><span class="pre">"-"</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> 开头。</p>
|
||||
<p class="audit-hook" id="audit_event_cpython_run_stdin_1">引发一个不带参数的 <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.run_stdin</span></code>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<span class="target" id="cmdarg-script"></span><dl class="describe">
|
||||
<dt>
|
||||
<code class="sig-name descname"><script></code></dt>
|
||||
<dd><p>执行 <em>script</em> 中的 Python 代码,该参数应为(绝对或相对)文件系统路径,指向 Python 文件、包含 <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> 文件的目录,或包含 <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> 文件的 zip 文件。</p>
|
||||
<p>给出此选项时,<a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 的第一个元素就是在命令行中指定的脚本名称。</p>
|
||||
<p>如果脚本名称直接指向 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> 的开头,并且把该文件当作 <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> 模块来执行。</p>
|
||||
<p>如果脚本名称指向目录或 zip 文件,则把脚本名加入 <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">__main__.py</span></code> 文件当作 <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> 模块来执行。</p>
|
||||
<p><a class="reference internal" href="#id2"><code class="xref std std-option docutils literal notranslate"><span class="pre">-I</span></code></a> 选项以隔离模式运行脚本,此模式中,<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> 既不包含脚本目录,也不包含用户的 site-packages 目录,还会忽略所有 <span class="target" id="index-92"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHON*</span></code> 环境变量。</p>
|
||||
<p class="audit-hook" id="audit_event_cpython_run_file_0">引发一个 <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.run_file</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">filename</span></code>。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><a class="reference internal" href="../library/runpy.html#runpy.run_path" title="runpy.run_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">runpy.run_path()</span></code></a></dt><dd><p>Python 代码可以直接使用的等效功能</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<p>未给出接口选项时,使用 <a class="reference internal" href="#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a>,<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> 为空字符串 (<code class="docutils literal notranslate"><span class="pre">""</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> 的开头。 此外,如果系统支持,还能自动启用 tab 补全和历史编辑(参见 <a class="reference internal" href="../library/site.html#rlcompleter-config"><span class="std std-ref">Readline 配置</span></a>)。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<p><a class="reference internal" href="../tutorial/interpreter.html#tut-invoking"><span class="std std-ref">唤出解释器</span></a></p>
|
||||
</div>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.4 版更改: </span>自动启用 tab 补全和历史编辑。</p>
|
||||
</div>
|
||||
</section>
|
||||
<section id="generic-options">
|
||||
<h3><span class="section-number">1.1.2. </span>通用选项<a class="headerlink" href="#generic-options" title="永久链接至标题">¶</a></h3>
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption">
|
||||
<code class="sig-name descname">-?</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption" title="永久链接至目标">¶</a></dt>
|
||||
<dt id="cmdoption-h">
|
||||
<code class="sig-name descname">-h</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-h" title="永久链接至目标">¶</a></dt>
|
||||
<dt id="cmdoption-help">
|
||||
<code class="sig-name descname">--help</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-help" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>输出所有命令行选项的简介。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-v">
|
||||
<code class="sig-name descname">-V</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-v" title="永久链接至目标">¶</a></dt>
|
||||
<dt id="cmdoption-version">
|
||||
<code class="sig-name descname">--version</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-version" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>输出 Python 版本号并退出。示例如下:</p>
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Python 3.8.0b2+
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>输入两次 <code class="docutils literal notranslate"><span class="pre">V</span></code> 选项时,输出更多构建信息,例如:</p>
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Python 3.8.0b2+ (3.8:0c076caaa8, Apr 20 2019, 21:55:00)
|
||||
[GCC 6.2.0 20161005]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.6 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-VV</span></code> 选项。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
</section>
|
||||
<section id="miscellaneous-options">
|
||||
<span id="using-on-misc-options"></span><h3><span class="section-number">1.1.3. </span>其他选项<a class="headerlink" href="#miscellaneous-options" title="永久链接至标题">¶</a></h3>
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-b">
|
||||
<code class="sig-name descname">-b</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-b" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>用 <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 与 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 或 <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> 对比, 或对比 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 与 <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> 时,会发出警告。重复给出该选项(<code class="xref std std-option docutils literal notranslate"><span class="pre">-bb</span></code>)时会报错。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.5 版更改: </span>影响 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 与 <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> 的比较。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="id1">
|
||||
<code class="sig-name descname">-B</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#id1" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>给出此选项时,Python 不在导入源模块时写入 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件。另请参阅 <span class="target" id="index-93"></span><a class="reference internal" href="#envvar-PYTHONDONTWRITEBYTECODE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDONTWRITEBYTECODE</span></code></a>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-check-hash-based-pycs">
|
||||
<code class="sig-name descname">--check-hash-based-pycs</code><code class="sig-prename descclassname"> default|always|never</code><a class="headerlink" href="#cmdoption-check-hash-based-pycs" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>控制基于哈希值的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件的验证行为。 参见 <a class="reference internal" href="../reference/import.html#pyc-invalidation"><span class="std std-ref">已缓存字节码的失效</span></a>。 当设为 <code class="docutils literal notranslate"><span class="pre">default</span></code> 时,已选定和未选定的基于哈希值的字节码缓存文件将根据其默认语义进行验证。 当设为 <code class="docutils literal notranslate"><span class="pre">always</span></code> 时,所有基于哈希值的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件,不论是已选定还是未选定的都将根据其对应的源文件进行验证。 当设为 <code class="docutils literal notranslate"><span class="pre">never</span></code> 时,基于哈希值的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件将不会根据其对应的源文件进行验证。</p>
|
||||
<p>基于时间戳的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件的语义不会受此选项影响。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-d">
|
||||
<code class="sig-name descname">-d</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-d" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>开启解析器调试输出(限专家使用,依赖于编译选项)。 另请参阅 <span class="target" id="index-94"></span><a class="reference internal" href="#envvar-PYTHONDEBUG"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDEBUG</span></code></a>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-e">
|
||||
<code class="sig-name descname">-E</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-e" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>忽略所有 <span class="target" id="index-95"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHON*</span></code> 环境变量,例如,已设置的 <span class="target" id="index-96"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> 和 <span class="target" id="index-97"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-i">
|
||||
<code class="sig-name descname">-i</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-i" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>脚本是第一个参数,或使用 <a class="reference internal" href="#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> 时,即便 <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> 不是终端,执行脚本或命令后,也会进入交互模式。不读取 <span class="target" id="index-98"></span><a class="reference internal" href="#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a> 文件。</p>
|
||||
<p>本选项用于,脚本触发异常时,检查全局变量或堆栈回溯。 详见 <span class="target" id="index-99"></span><a class="reference internal" href="#envvar-PYTHONINSPECT"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONINSPECT</span></code></a>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="id2">
|
||||
<code class="sig-name descname">-I</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#id2" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>在隔离模式下运行 Python。 这将同时应用 -E 和 -s。 在隔离模式下 <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> 既不包含脚本所在目录也不包含用户的 site-packages 目录。 所有 <span class="target" id="index-100"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHON*</span></code> 环境变量也会被忽略。 还可以施加更进一步的限制以防止用户注入恶意代码。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.4 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-o">
|
||||
<code class="sig-name descname">-O</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-o" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>移除 assert 语句以及任何以 <a class="reference internal" href="../library/constants.html#__debug__" title="__debug__"><code class="xref py py-const docutils literal notranslate"><span class="pre">__debug__</span></code></a> 的值作为条件的代码。 通过在 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 扩展名之前添加 <code class="docutils literal notranslate"><span class="pre">.opt-1</span></code> 来扩充已编译文件 (<a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a>) 的文件名 (参见 <span class="target" id="index-101"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a>)。 另请参阅 <span class="target" id="index-102"></span><a class="reference internal" href="#envvar-PYTHONOPTIMIZE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONOPTIMIZE</span></code></a>。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.5 版更改: </span>依据 <span class="target" id="index-103"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> 修改 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件名。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-oo">
|
||||
<code class="sig-name descname">-OO</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-oo" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>在启用 <a class="reference internal" href="#cmdoption-o"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a> 的同时丢弃文档字符串。 通过在 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 扩展名之前添加 <code class="docutils literal notranslate"><span class="pre">.opt-2</span></code> 来扩展已编译文件 (<a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a>) 的文件名 (参见 <span class="target" id="index-104"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a>)。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.5 版更改: </span>依据 <span class="target" id="index-105"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> 修改 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件名。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-q">
|
||||
<code class="sig-name descname">-q</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-q" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>即使在交互模式下也不显示版权和版本信息。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-r">
|
||||
<code class="sig-name descname">-R</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-r" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>开启哈希随机化。 此选项权 <span class="target" id="index-106"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> 环境变量设置为 <code class="docutils literal notranslate"><span class="pre">0</span></code> 时起作用,因为哈希随机化是默认启用的。</p>
|
||||
<p>在Python的早期版本中,此选项启用哈希随机化,将 str 和 bytes 的对象 <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> 的值 "加盐" 为不可预测的随机值。虽然它们在单个Python进程中保持不变,但是在重复调用的Python进程之间它们是不可预测的。</p>
|
||||
<p>哈希随机化旨在针对由精心选择的输入引起的拒绝服务攻击提供防护,这种输入利用了构造 dict 在最坏情况下的性能即 O(n^2) 复杂度。 详情请参阅 <a class="reference external" href="http://www.ocert.org/advisories/ocert-2011-003.html">http://www.ocert.org/advisories/ocert-2011-003.html</a>。</p>
|
||||
<p><span class="target" id="index-107"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> 允许你为哈希种子密码设置一个固定值。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.7 版更改: </span>此选项不会再被忽略。</p>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.2.3 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-s">
|
||||
<code class="sig-name descname">-s</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-s" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>不要将 <a class="reference internal" href="../library/site.html#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">用户</span> <span class="pre">site-packages</span> <span class="pre">目录</span></code></a> 添加到 <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>。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<p><span class="target" id="index-108"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0370"><strong>PEP 370</strong></a> -- 分用户的 site-packages 目录</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="id3">
|
||||
<code class="sig-name descname">-S</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#id3" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>禁用 <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> 的导入及其所附带的基于站点对 <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> 的操作。 如果 <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> 会在稍后被显式地导入也会禁用这些操作 (如果你希望触发它们则应调用 <a class="reference internal" href="../library/site.html#site.main" title="site.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">site.main()</span></code></a>)。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-u">
|
||||
<code class="sig-name descname">-u</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-u" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>强制 stdout 和 stderr 流不使用缓冲。 此选项对 stdin 流无影响。</p>
|
||||
<p>另请参阅 <span class="target" id="index-109"></span><a class="reference internal" href="#envvar-PYTHONUNBUFFERED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUNBUFFERED</span></code></a>。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.7 版更改: </span>stdout 和 stderr 流在文本层现在不使用缓冲。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="id4">
|
||||
<code class="sig-name descname">-v</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#id4" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>每当一个模块被初始化时打印一条信息,显示其加载位置(文件名或内置模块)。 当重复给出时 (<code class="xref std std-option docutils literal notranslate"><span class="pre">-vv</span></code>),为搜索模块时所检查的每个文件都打印一条消息。 此外还提供退出时有关模块清理的信息A。 另请参阅 <span class="target" id="index-110"></span><a class="reference internal" href="#envvar-PYTHONVERBOSE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONVERBOSE</span></code></a>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<span class="target" id="using-on-warnings"></span><dl class="cmdoption">
|
||||
<dt id="cmdoption-w">
|
||||
<code class="sig-name descname">-W</code><code class="sig-prename descclassname"> arg</code><a class="headerlink" href="#cmdoption-w" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>警告控制。 Python 的警告机制在默认情况下会向 <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> 打印警告消息。 典型的警告消息具有如下形式:</p>
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>file:line: category: message
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>默认情况下,每个警告都对于其发生所在的每个源行都会打印一次。 此选项可控制警告打印的频繁程度。</p>
|
||||
<p>可以给出多个 <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> 选项;当某个警告能与多个选项匹配时,将执行最后一个匹配选项的操作。 无效的 <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> 选项将被忽略(但是,在发出第一个警告时会打印有关无效选项的警告消息)。</p>
|
||||
<p>警告也可以使用 <span class="target" id="index-111"></span><a class="reference internal" href="#envvar-PYTHONWARNINGS"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONWARNINGS</span></code></a> 环境变量以及使用 <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> 模块在 Python 程序内部进行控制。</p>
|
||||
<p>最简单的设置是将某个特定操作无条件地应用于进程所发出所有警告 (即使是在默认情况下会忽略的那些警告):</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>-Wdefault<span class="w"> </span><span class="c1"># Warn once per call location</span>
|
||||
-Werror<span class="w"> </span><span class="c1"># Convert to exceptions</span>
|
||||
-Walways<span class="w"> </span><span class="c1"># Warn every time</span>
|
||||
-Wmodule<span class="w"> </span><span class="c1"># Warn once per calling module</span>
|
||||
-Wonce<span class="w"> </span><span class="c1"># Warn once per Python process</span>
|
||||
-Wignore<span class="w"> </span><span class="c1"># Never warn</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>操作名称可以根据需要进行缩写 (例如 <code class="docutils literal notranslate"><span class="pre">-Wi</span></code>, <code class="docutils literal notranslate"><span class="pre">-Wd</span></code>, <code class="docutils literal notranslate"><span class="pre">-Wa</span></code>, <code class="docutils literal notranslate"><span class="pre">-We</span></code>),解释器将会把它们解析为适当的操作名称。</p>
|
||||
<p>请参阅 <a class="reference internal" href="../library/warnings.html#warning-filter"><span class="std std-ref">警告过滤器</span></a> 和 <a class="reference internal" href="../library/warnings.html#describing-warning-filters"><span class="std std-ref">警告过滤器的介绍</span></a> 了解更多细节。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-x">
|
||||
<code class="sig-name descname">-x</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-x" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>跳过源中第一行,以允许使用非 Unix 形式的 <code class="docutils literal notranslate"><span class="pre">#!cmd</span></code>。 这适用于 DOS 专属的破解操作。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="cmdoption">
|
||||
<dt id="id5">
|
||||
<code class="sig-name descname">-X</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#id5" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>保留用于各种具体实现专属的选项。 CPython 目前定义了下列可用的值:</p>
|
||||
<ul class="simple">
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">faulthandler</span></code> 启用 <a class="reference internal" href="../library/faulthandler.html#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a>;</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showrefcount</span></code> 当程序结束或在交互解释器中的每条语句之后输出总引用计数和已使用内存块计数。 此选项仅在调试版本中有效。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">tracemalloc</span></code> 使用 <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> 模块启动对 Python 内存分配的跟踪。 默认情况下,只有最近的帧会保存在跟踪的回溯信息中。 使用 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">tracemalloc=NFRAME</span></code> 以启动限定回溯 <em>NFRAME</em> 帧的跟踪。 请参阅 <a class="reference internal" href="../library/tracemalloc.html#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">tracemalloc.start()</span></code></a> 了解详情。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showalloccount</span></code> 当程序结束时输出每种类型的已分配对象的总数。 此选项仅当 Python 在定义了 <code class="docutils literal notranslate"><span class="pre">COUNT_ALLOCS</span></code> 后构建时才会生效。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">int_max_str_digits</span></code> 将配置 <a class="reference internal" href="../library/stdtypes.html#int-max-str-digits"><span class="std std-ref">整数字符串转换长度限制</span></a>。 另请参阅 <span class="target" id="index-112"></span><a class="reference internal" href="#envvar-PYTHONINTMAXSTRDIGITS"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONINTMAXSTRDIGITS</span></code></a>。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">importtime</span></code> 显示每次导入耗费的时间。 它会显示模块名称,累计时间(包括嵌套的导入)和自身时间(排除嵌套的导入)。 请注意它的输出在多线程应用程序中可能会出错。 典型用法如 <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-X</span> <span class="pre">importtime</span> <span class="pre">-c</span> <span class="pre">'import</span> <span class="pre">asyncio'</span></code>。 另请参阅 <span class="target" id="index-113"></span><a class="reference internal" href="#envvar-PYTHONPROFILEIMPORTTIME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPROFILEIMPORTTIME</span></code></a>。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code>: 启用 CPython 的“开发模式”,引入额外的运行时检测,这些检测因开销过大而无法默认启用。 如果代码是正确的则它不会比默认输出更详细:新增警告只会在发现问题时才会发出。 开发模式的作用效果:</p>
|
||||
<ul>
|
||||
<li><p>添加 <code class="docutils literal notranslate"><span class="pre">default</span></code> 警告过滤器,即 <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> <code class="docutils literal notranslate"><span class="pre">default</span></code>。</p></li>
|
||||
<li><p>在内存分配器上安装调试钩子:参见 <a class="reference internal" href="../c-api/memory.html#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a> C 函数。</p></li>
|
||||
<li><p>启用 <a class="reference internal" href="../library/faulthandler.html#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a> 模块以在发生崩溃时转储 Python 回溯信息。</p></li>
|
||||
<li><p>启用 <a class="reference internal" href="../library/asyncio-dev.html#asyncio-debug-mode"><span class="std std-ref">asyncio 调试模式</span></a>。</p></li>
|
||||
<li><p>将 <a class="reference internal" href="../library/sys.html#sys.flags" title="sys.flags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.flags</span></code></a> 的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">dev_mode</span></code> 属性设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p></li>
|
||||
<li><p><a class="reference internal" href="../library/io.html#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.IOBase</span></code></a> 析构函数记录 <code class="docutils literal notranslate"><span class="pre">close()</span></code> 异常。</p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> 为操作系统接口启用 UTF-8 模式,覆盖默认的区域感知模式。 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8=0</span></code> 显式地禁用 UTF-8 模式(即使在它应当被自动激活的时候)。 请参阅 <span class="target" id="index-114"></span><a class="reference internal" href="#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> 了解详情。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">pycache_prefix=PATH</span></code> 允许将 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件写入以给定目录为根的并行树,而不是代码树。另见 <span class="target" id="index-115"></span><a class="reference internal" href="#envvar-PYTHONPYCACHEPREFIX"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPYCACHEPREFIX</span></code></a> 。</p></li>
|
||||
</ul>
|
||||
<p>它还允许传入任意值并通过 <a class="reference internal" href="../library/sys.html#sys._xoptions" title="sys._xoptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys._xoptions</span></code></a> 字典来提取这些值。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.2 版更改: </span>增加了 <a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> 选项。</p>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.3 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">faulthandler</span></code> 选项。</p>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.4 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showrefcount</span></code> 与 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">tracemalloc</span></code> 选项。</p>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.6 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showalloccount</span></code> 选项。</p>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.7 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">importtime</span></code>, <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code> 与 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> 选项。</p>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.8 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">pycache_prefix</span></code> 选项。 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code> 选项现在在 <a class="reference internal" href="../library/io.html#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.IOBase</span></code></a> 析构函数中记录 <code class="docutils literal notranslate"><span class="pre">close()</span></code> 异常。</p>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.8.14 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">int_max_str_digits</span></code> 选项。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
</section>
|
||||
<section id="options-you-shouldn-t-use">
|
||||
<h3><span class="section-number">1.1.4. </span>不应当使用的选项<a class="headerlink" href="#options-you-shouldn-t-use" title="永久链接至标题">¶</a></h3>
|
||||
<dl class="cmdoption">
|
||||
<dt id="cmdoption-j">
|
||||
<code class="sig-name descname">-J</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-j" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>保留给 <a class="reference external" href="http://www.jython.org/">Jython</a> 使用。</p>
|
||||
</dd></dl>
|
||||
|
||||
</section>
|
||||
</section>
|
||||
<section id="environment-variables">
|
||||
<span id="using-on-envvars"></span><h2><span class="section-number">1.2. </span>环境变量<a class="headerlink" href="#environment-variables" title="永久链接至标题">¶</a></h2>
|
||||
<p>这些环境变量会影响 Python 的行为,它们是在命令行开关之前被处理的,但 -E 或 -I 除外。 根据约定,当存在冲突时命令行开关会覆盖环境变量的设置。</p>
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONHOME">
|
||||
<code class="sig-name descname">PYTHONHOME</code><a class="headerlink" href="#envvar-PYTHONHOME" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>更改标准 Python 库的位置。 默认情况下库是在 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/lib/python</span><em><span class="pre">version</span></em></code> 和 <code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em><span class="pre">/lib/python</span><em><span class="pre">version</span></em></code> 中搜索,其中 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em></code> 和 <code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em></code> 是由安装位置确定的目录,默认都位于 <code class="file docutils literal notranslate"><span class="pre">/usr/local</span></code>。</p>
|
||||
<p>当 <span class="target" id="index-116"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> 被设为单个目录时,它的值会同时替代 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em></code> 和 <code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em></code>。 要为两者指定不同的值,请将 <span class="target" id="index-117"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> 设为 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">:</span><em><span class="pre">exec_prefix</span></em></code>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONPATH">
|
||||
<code class="sig-name descname">PYTHONPATH</code><a class="headerlink" href="#envvar-PYTHONPATH" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>增加模块文件默认搜索路径。 所用格式与终端的 <span class="target" id="index-118"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 相同:一个或多个由 <a class="reference internal" href="../library/os.html#os.pathsep" title="os.pathsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.pathsep</span></code></a> 分隔的目录路径名称(例如 Unix 上用冒号而在 Windows 上用分号)。 默认忽略不存在的目录。</p>
|
||||
<p>除了普通目录之外,单个 <span class="target" id="index-119"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> 条目可以引用包含纯Python模块的zip文件(源代码或编译形式)。无法从zip文件导入扩展模块。</p>
|
||||
<p>默认索引路径依赖于安装路径,但通常都是以 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/lib/python</span><em><span class="pre">version</span></em></code> 开始 (参见上文中的 <span class="target" id="index-120"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a>)。 它 <em>总是</em> 会被添加到 <span class="target" id="index-121"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a>。</p>
|
||||
<p>有一个附加目录将被插入到索引路径的 <span class="target" id="index-122"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> 之前,正如上文中 <a class="reference internal" href="#using-on-interface-options"><span class="std std-ref">接口选项</span></a> 所描述的。 搜索路径可以在 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> 来进行操作。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONSTARTUP">
|
||||
<code class="sig-name descname">PYTHONSTARTUP</code><a class="headerlink" href="#envvar-PYTHONSTARTUP" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>这如果是一个可读文件的名称,该文件中的 Python 命令会在交互模式的首个提示符显示之前被执行。 该文件会在与交互式命令执行所在的同一命名空间中被执行,因此其中所定义或导入的对象可以在交互式会话中无限制地使用。 你还可以在这个文件中修改提示符 <a class="reference internal" href="../library/sys.html#sys.ps1" title="sys.ps1"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.ps1</span></code></a> 和 <a class="reference internal" href="../library/sys.html#sys.ps2" title="sys.ps2"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.ps2</span></code></a> 以及钩子 <a class="reference internal" href="../library/sys.html#sys.__interactivehook__" title="sys.__interactivehook__"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.__interactivehook__</span></code></a>。</p>
|
||||
<p class="audit-hook">使用 <code class="docutils literal notranslate"><span class="pre">filename</span></code> 参数会引发 <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.run_startup</span></code> 。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONOPTIMIZE">
|
||||
<code class="sig-name descname">PYTHONOPTIMIZE</code><a class="headerlink" href="#envvar-PYTHONOPTIMIZE" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>这如果被设为一个非空字符串,它就相当于指定 <a class="reference internal" href="#cmdoption-o"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a> 选项。 如果设为一个整数,则它就相当于多次指定 <a class="reference internal" href="#cmdoption-o"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONBREAKPOINT">
|
||||
<code class="sig-name descname">PYTHONBREAKPOINT</code><a class="headerlink" href="#envvar-PYTHONBREAKPOINT" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>此变量如果被设定,它会使用加点号的路径标记一个可调用对象。 包含该可调用对象的模块将被导入,随后该可调用对象将由 <a class="reference internal" href="../library/sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a> 的默认实现来运行,后者自身将由内置的 <a class="reference internal" href="../library/functions.html#breakpoint" title="breakpoint"><code class="xref py py-func docutils literal notranslate"><span class="pre">breakpoint()</span></code></a> 来调用。 如果未设定,或设定为空字符串,则它相当于值 "pdb.set_trace"。 将此变量设为字符串 "0" 会导致 <a class="reference internal" href="../library/sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a> 的默认实现不做任何事而直接返回。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONDEBUG">
|
||||
<code class="sig-name descname">PYTHONDEBUG</code><a class="headerlink" href="#envvar-PYTHONDEBUG" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>此变量如果被设为一个非空字符串,它就相当于指定 <a class="reference internal" href="#cmdoption-d"><code class="xref std std-option docutils literal notranslate"><span class="pre">-d</span></code></a> 选项。 如果设为一个整数,则它就相当于多次指定 <a class="reference internal" href="#cmdoption-d"><code class="xref std std-option docutils literal notranslate"><span class="pre">-d</span></code></a>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONINSPECT">
|
||||
<code class="sig-name descname">PYTHONINSPECT</code><a class="headerlink" href="#envvar-PYTHONINSPECT" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>此变量如果被设为一个非空字符串,它就相当于指定 <a class="reference internal" href="#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a> 选项。</p>
|
||||
<p>此变量也可由 Python 代码使用 <a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> 来修改以在程序终结时强制检查模式。</p>
|
||||
<p class="audit-hook" id="audit_event_cpython_run_stdin_2">引发一个不带参数的 <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.run_stdin</span></code>。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.8.20 版更改: </span>Emits audit events.</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONUNBUFFERED">
|
||||
<code class="sig-name descname">PYTHONUNBUFFERED</code><a class="headerlink" href="#envvar-PYTHONUNBUFFERED" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>此变量如果被设为一个非空字符串,它就相当于指定 <a class="reference internal" href="#cmdoption-u"><code class="xref std std-option docutils literal notranslate"><span class="pre">-u</span></code></a> 选项。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONVERBOSE">
|
||||
<code class="sig-name descname">PYTHONVERBOSE</code><a class="headerlink" href="#envvar-PYTHONVERBOSE" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>此变量如果被设为一个非空字符串,它就相当于指定 <a class="reference internal" href="#id4"><code class="xref std std-option docutils literal notranslate"><span class="pre">-v</span></code></a> 选项。 如果设为一个整数,则它就相当于多次指定 <a class="reference internal" href="#id4"><code class="xref std std-option docutils literal notranslate"><span class="pre">-v</span></code></a>。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONCASEOK">
|
||||
<code class="sig-name descname">PYTHONCASEOK</code><a class="headerlink" href="#envvar-PYTHONCASEOK" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设置此变量,Python 将忽略 <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> 语句中的大小写。 这仅在 Windows 和 OS X 上有效。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONDONTWRITEBYTECODE">
|
||||
<code class="sig-name descname">PYTHONDONTWRITEBYTECODE</code><a class="headerlink" href="#envvar-PYTHONDONTWRITEBYTECODE" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>此变量如果被设为一个非空字符串,Python 将不会尝试在导入源模块时写入 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件。 这相当于指定 <a class="reference internal" href="#id1"><code class="xref std std-option docutils literal notranslate"><span class="pre">-B</span></code></a> 选项。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONPYCACHEPREFIX">
|
||||
<code class="sig-name descname">PYTHONPYCACHEPREFIX</code><a class="headerlink" href="#envvar-PYTHONPYCACHEPREFIX" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设置了此选项,Python将在镜像目录树中的此路径中写入 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件,而不是源树中的 <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code> 目录中。这相当于指定 <a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">pycache_prefix=PATH</span></code> 选项。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.8 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONHASHSEED">
|
||||
<code class="sig-name descname">PYTHONHASHSEED</code><a class="headerlink" href="#envvar-PYTHONHASHSEED" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果此变量未设置或设为 <code class="docutils literal notranslate"><span class="pre">random</span></code>,将使用一个随机值作为 str 和 bytes 对象哈希运算的种子。</p>
|
||||
<p>如果 <span class="target" id="index-123"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> 被设为一个整数值,它将被作为固定的种子数用来生成哈希随机化所涵盖的类型的 hash() 结果。</p>
|
||||
<p>它的目的是允许可复现的哈希运算,例如用于解释器本身的自我检测,或允许一组 python 进程共享哈希值。</p>
|
||||
<p>该整数必须为一个 [0,4294967295] 范围内的十进制数。 指定数值 0 将禁用哈希随机化。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.2.3 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONINTMAXSTRDIGITS">
|
||||
<code class="sig-name descname">PYTHONINTMAXSTRDIGITS</code><a class="headerlink" href="#envvar-PYTHONINTMAXSTRDIGITS" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果将此变量设为一个整数,它会被用来配置解释器的全局 <a class="reference internal" href="../library/stdtypes.html#int-max-str-digits"><span class="std std-ref">整数字符串转换长度限制</span></a>。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.8.14 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONIOENCODING">
|
||||
<code class="sig-name descname">PYTHONIOENCODING</code><a class="headerlink" href="#envvar-PYTHONIOENCODING" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果此变量在运行解释器之前被设置,它会覆盖通过 <code class="docutils literal notranslate"><span class="pre">encodingname:errorhandler</span></code> 语法设置的 stdin/stdout/stderr 所用编码。 <code class="docutils literal notranslate"><span class="pre">encodingname</span></code> 和 <code class="docutils literal notranslate"><span class="pre">:errorhandler</span></code> 部分都是可选项,与在 <a class="reference internal" href="../library/stdtypes.html#str.encode" title="str.encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">str.encode()</span></code></a> 中的含义相同。</p>
|
||||
<p>对于 stderr,<code class="docutils literal notranslate"><span class="pre">:errorhandler</span></code> 部分会被忽略;处理程序将总是为 <code class="docutils literal notranslate"><span class="pre">'backslashreplace'</span></code>。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.4 版更改: </span>“encodingname” 部分现在是可选的。</p>
|
||||
</div>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.6 版更改: </span>在 Windows 上,对于交互式控制台缓冲区会忽略此变量所指定的编码,除非还指定了 <span class="target" id="index-124"></span><a class="reference internal" href="#envvar-PYTHONLEGACYWINDOWSSTDIO"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONLEGACYWINDOWSSTDIO</span></code></a>。 通过标准流重定向的文件和管道则不受其影响。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONNOUSERSITE">
|
||||
<code class="sig-name descname">PYTHONNOUSERSITE</code><a class="headerlink" href="#envvar-PYTHONNOUSERSITE" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设置了此变量,Python 将不会把 <a class="reference internal" href="../library/site.html#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">用户</span> <span class="pre">site-packages</span> <span class="pre">目录</span></code></a> 添加到 <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>。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<p><span class="target" id="index-125"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0370"><strong>PEP 370</strong></a> -- 分用户的 site-packages 目录</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONUSERBASE">
|
||||
<code class="sig-name descname">PYTHONUSERBASE</code><a class="headerlink" href="#envvar-PYTHONUSERBASE" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>定义 <a class="reference internal" href="../library/site.html#site.USER_BASE" title="site.USER_BASE"><code class="xref py py-data docutils literal notranslate"><span class="pre">用户基准目录</span></code></a>,它会在执行 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">install</span> <span class="pre">--user</span></code> 时被用来计算 <a class="reference internal" href="../library/site.html#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">用户</span> <span class="pre">site-packages</span> <span class="pre">目录</span></code></a> 的路径以及 <a class="reference internal" href="../install/index.html#inst-alt-install-user"><span class="std std-ref">Distutils 安装路径</span></a>。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<p><span class="target" id="index-126"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0370"><strong>PEP 370</strong></a> -- 分用户的 site-packages 目录</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONEXECUTABLE">
|
||||
<code class="sig-name descname">PYTHONEXECUTABLE</code><a class="headerlink" href="#envvar-PYTHONEXECUTABLE" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设置了此环境变量,则 <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> 将被设为此变量的值而不是通过 C 运行时所获得的值。 仅在 Mac OS X 上起作用。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONWARNINGS">
|
||||
<code class="sig-name descname">PYTHONWARNINGS</code><a class="headerlink" href="#envvar-PYTHONWARNINGS" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>此变量等价于 <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> 选项。 如果被设为一个以逗号分隔的字符串,它就相当于多次指定 <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a>,列表中后出现的过滤器优先级会高于列表中先出现的。</p>
|
||||
<p>最简单的设置是将某个特定操作无条件地应用于进程所发出所有警告 (即使是在默认情况下会忽略的那些警告):</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="nv">PYTHONWARNINGS</span><span class="o">=</span>default<span class="w"> </span><span class="c1"># Warn once per call location</span>
|
||||
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>error<span class="w"> </span><span class="c1"># Convert to exceptions</span>
|
||||
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>always<span class="w"> </span><span class="c1"># Warn every time</span>
|
||||
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>module<span class="w"> </span><span class="c1"># Warn once per calling module</span>
|
||||
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>once<span class="w"> </span><span class="c1"># Warn once per Python process</span>
|
||||
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>ignore<span class="w"> </span><span class="c1"># Never warn</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>请参阅 <a class="reference internal" href="../library/warnings.html#warning-filter"><span class="std std-ref">警告过滤器</span></a> 和 <a class="reference internal" href="../library/warnings.html#describing-warning-filters"><span class="std std-ref">警告过滤器的介绍</span></a> 了解更多细节。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONFAULTHANDLER">
|
||||
<code class="sig-name descname">PYTHONFAULTHANDLER</code><a class="headerlink" href="#envvar-PYTHONFAULTHANDLER" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果此环境变量被设为一个非空字符串,<a class="reference internal" href="../library/faulthandler.html#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">faulthandler.enable()</span></code></a> 会在启动时被调用:为 <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGSEGV</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGFPE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGABRT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGBUS</span></code> 和 <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGILL</span></code> 等信号安装一个处理器以转储 Python 回溯信息。 此变量等价于 <a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> 选项。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONTRACEMALLOC">
|
||||
<code class="sig-name descname">PYTHONTRACEMALLOC</code><a class="headerlink" href="#envvar-PYTHONTRACEMALLOC" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果此环境变量被设为一个非空字符串,则会使用 <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> 模块启动对 Python 内存分配的跟踪。 该变量的值是保存于跟踪的回溯信息中的最大帧数。 例如,<code class="docutils literal notranslate"><span class="pre">PYTHONTRACEMALLOC=1</span></code> 只保存最近的帧。 请参阅 <a class="reference internal" href="../library/tracemalloc.html#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">tracemalloc.start()</span></code></a> 了解详情。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.4 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONPROFILEIMPORTTIME">
|
||||
<code class="sig-name descname">PYTHONPROFILEIMPORTTIME</code><a class="headerlink" href="#envvar-PYTHONPROFILEIMPORTTIME" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果此变量被设为一个非空字符串,Python 将显示每次导入花费了多长时间。 此变量完全等价于在命令行为设置 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">importtime</span></code>。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONASYNCIODEBUG">
|
||||
<code class="sig-name descname">PYTHONASYNCIODEBUG</code><a class="headerlink" href="#envvar-PYTHONASYNCIODEBUG" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果此变量被设为一个非空字符串,则会启用 <a class="reference internal" href="../library/asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> 模块的 <a class="reference internal" href="../library/asyncio-dev.html#asyncio-debug-mode"><span class="std std-ref">调试模式</span></a>。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.4 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONMALLOC">
|
||||
<code class="sig-name descname">PYTHONMALLOC</code><a class="headerlink" href="#envvar-PYTHONMALLOC" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>设置 Python 内存分配器和/或安装调试钩子。</p>
|
||||
<p>设置 Python 所使用的内存分配器族群:</p>
|
||||
<ul class="simple">
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">default</span></code>: 使用 <a class="reference internal" href="../c-api/memory.html#default-memory-allocators"><span class="std std-ref">默认内存分配器</span></a>。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code>: 对所有域 (<a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_RAW" title="PYMEM_DOMAIN_RAW"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_RAW</span></code></a>, <a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_MEM</span></code></a>, <a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_OBJ</span></code></a>) 使用 C 库的 <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> 函数。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">pymalloc</span></code>: 对 <a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_MEM</span></code></a> 和 <a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_OBJ</span></code></a> 域使用 <a class="reference internal" href="../c-api/memory.html#pymalloc"><span class="std std-ref">pymalloc 分配器</span></a> 而对 <a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_RAW" title="PYMEM_DOMAIN_RAW"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_RAW</span></code></a> 域使用 <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> 函数。</p></li>
|
||||
</ul>
|
||||
<p>安装调试钩子:</p>
|
||||
<ul class="simple">
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">debug</span></code>: 在 <a class="reference internal" href="../c-api/memory.html#default-memory-allocators"><span class="std std-ref">默认内存分配器</span></a> 之上安装调试钩子。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">malloc_debug</span></code>: 与 <code class="docutils literal notranslate"><span class="pre">malloc</span></code> 相同但还会安装调试钩子。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">pymalloc_debug</span></code>: 与 <code class="docutils literal notranslate"><span class="pre">pymalloc</span></code> 相同但还会安装调试钩子。</p></li>
|
||||
</ul>
|
||||
<p>请参阅 <a class="reference internal" href="../c-api/memory.html#default-memory-allocators"><span class="std std-ref">默认内存分配器</span></a> 以及 <a class="reference internal" href="../c-api/memory.html#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a> 函数(在 Python 内存分配器之上安装调试钩子)。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.7 版更改: </span>增加了 <code class="docutils literal notranslate"><span class="pre">"default"</span></code> 分配器。</p>
|
||||
</div>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.6 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONMALLOCSTATS">
|
||||
<code class="sig-name descname">PYTHONMALLOCSTATS</code><a class="headerlink" href="#envvar-PYTHONMALLOCSTATS" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设为一个非空字符串,Python 将在每次创建新的 pymalloc 对象区域以及在关闭时打印 <a class="reference internal" href="../c-api/memory.html#pymalloc"><span class="std std-ref">pymalloc 内存分配器</span></a> 的统计信息。</p>
|
||||
<p>如果 <span class="target" id="index-127"></span><a class="reference internal" href="#envvar-PYTHONMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONMALLOC</span></code></a> 环境变量被用来强制开启 C 库的 <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> 分配器,或者如果 Python 的配置不支持 <code class="docutils literal notranslate"><span class="pre">pymalloc</span></code>,则此变量将被忽略。</p>
|
||||
<div class="versionchanged">
|
||||
<p><span class="versionmodified changed">在 3.6 版更改: </span>此变量现在也可以被用于在发布模式下编译的 Python。 如果它被设置为一个空字符串则将没有任何效果。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONLEGACYWINDOWSFSENCODING">
|
||||
<code class="sig-name descname">PYTHONLEGACYWINDOWSFSENCODING</code><a class="headerlink" href="#envvar-PYTHONLEGACYWINDOWSFSENCODING" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设为一个非空字符串,则默认的文件系统编码和错误模式将分别恢复为它们在 3.6 版之前的值 'mbcs' 和 'replace'。 否则会使用新的默认值 'utf-8' 和 'surrogatepass'。</p>
|
||||
<p>这也可以在运行时通过 <a class="reference internal" href="../library/sys.html#sys._enablelegacywindowsfsencoding" title="sys._enablelegacywindowsfsencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys._enablelegacywindowsfsencoding()</span></code></a> 来启用。</p>
|
||||
<p class="availability"><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">可用性</span></a>: Windows。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.6 新版功能: </span>更多详情请参阅 <span class="target" id="index-128"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0529"><strong>PEP 529</strong></a>。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONLEGACYWINDOWSSTDIO">
|
||||
<code class="sig-name descname">PYTHONLEGACYWINDOWSSTDIO</code><a class="headerlink" href="#envvar-PYTHONLEGACYWINDOWSSTDIO" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设为一个非空字符串,则不使用新的控制台读取器和写入器。 这意味着 Unicode 字符将根据活动控制台的代码页进行编码,而不是使用 utf-8。</p>
|
||||
<p>如果标准流被重定向(到文件或管道)而不是指向控制台缓冲区则该变量会被忽略。</p>
|
||||
<p class="availability"><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">可用性</span></a>: Windows。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.6 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONCOERCECLOCALE">
|
||||
<code class="sig-name descname">PYTHONCOERCECLOCALE</code><a class="headerlink" href="#envvar-PYTHONCOERCECLOCALE" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果值设为 <code class="docutils literal notranslate"><span class="pre">0</span></code>,将导致主 Python 命令行应用跳过将传统的基于 ASCII 的 C 与 POSIX 区域设置强制转换为更强大的基于 UTF-8 的替代方案。</p>
|
||||
<p>如果此变量 <em>未被</em> 设置(或被设为 <code class="docutils literal notranslate"><span class="pre">0</span></code> 以外的值),则覆盖环境变量的 <code class="docutils literal notranslate"><span class="pre">LC_ALL</span></code> 区域选项也不会被设置,并且报告给 <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> 类别的当前区域选项或者为默认的 <code class="docutils literal notranslate"><span class="pre">C</span></code> 区域,或者为显式指明的基于 ASCII 的 <code class="docutils literal notranslate"><span class="pre">POSIX</span></code> 区域,然后 Python CLI 将在加载解释器运行时之前尝试为 <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> 类别按指定的顺序配置下列区域选项:</p>
|
||||
<ul class="simple">
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">C.UTF-8</span></code></p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">C.utf8</span></code></p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">UTF-8</span></code></p></li>
|
||||
</ul>
|
||||
<p>如果成功设置了以上区域类别中的一个,则初始化 Python 运行时之前也将在当前进程环境中相应地设置 <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> 环境变量。 这会确保除了解释器本身和运行于同一进程中的其他可感知区域选项的组件 (例如 GNU <code class="docutils literal notranslate"><span class="pre">readline</span></code> 库) 之外,还能在子进程 (无论这些进程是否在运行 Python 解释器) 以及在查询环境而非当前 C 区域的操作 (例如 Python 自己的 <a class="reference internal" href="../library/locale.html#locale.getdefaultlocale" title="locale.getdefaultlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getdefaultlocale()</span></code></a>) 中看到更新的设置。</p>
|
||||
<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> 启用 <code class="docutils literal notranslate"><span class="pre">surrogateescape</span></code> <a class="reference internal" href="../library/codecs.html#error-handlers"><span class="std std-ref">错误处理器</span></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> 会继续使用 <code class="docutils literal notranslate"><span class="pre">backslashreplace</span></code> 如同在任何其他区域选项中一样)。 这种流处理行为可以按通常方式使用 <span class="target" id="index-129"></span><a class="reference internal" href="#envvar-PYTHONIOENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONIOENCODING</span></code></a> 来覆盖。</p>
|
||||
<p>出于调试目的,如果激活了区域强制转换,或者如果当 Python 运行时被初始化时某个 <em>应该</em> 触发强制转换的区域选项仍处于激活状态则设置 <code class="docutils literal notranslate"><span class="pre">PYTHONCOERCECLOCALE=warn</span></code> 将导致 Python 在 <code class="docutils literal notranslate"><span class="pre">stderr</span></code> 上发出警告消息。</p>
|
||||
<p>还要注意,即使在区域转换转换被禁用,或者在其无法找到合适的目标区域时,默认 <span class="target" id="index-130"></span><a class="reference internal" href="#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> 仍将在传统的基于 ASCII 的区域中被激活。 必须同时禁用这两项特性以强制解释器使用 <code class="docutils literal notranslate"><span class="pre">ASCII</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">UTF-8</span></code> 作为系统接口。</p>
|
||||
<p class="availability"><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">可用性</span></a>: *nix。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.7 新版功能: </span>请参阅 <span class="target" id="index-131"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0538"><strong>PEP 538</strong></a> 了解详情。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONDEVMODE">
|
||||
<code class="sig-name descname">PYTHONDEVMODE</code><a class="headerlink" href="#envvar-PYTHONDEVMODE" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果此环境变量被设为一个非空字符串,则会启用 CPython “开发模式”。 参见 <a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">dev</span></code> 选项。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONUTF8">
|
||||
<code class="sig-name descname">PYTHONUTF8</code><a class="headerlink" href="#envvar-PYTHONUTF8" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设为 <code class="docutils literal notranslate"><span class="pre">1</span></code>,则会启用解释器的 UTF-8 模式,将 <code class="docutils literal notranslate"><span class="pre">UTF-8</span></code> 用作系统接口的文本编码,无论当前区域选项如何设置。</p>
|
||||
<p>这意味着:</p>
|
||||
<blockquote>
|
||||
<div><ul class="simple">
|
||||
<li><p><a class="reference internal" href="../library/sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a> 将返回 <code class="docutils literal notranslate"><span class="pre">'UTF-8'</span></code> (本地编码会被忽略)。</p></li>
|
||||
<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> (本地编码会被忽略,并且该函数的 <code class="docutils literal notranslate"><span class="pre">do_setlocale</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 用作它们的文本编码,并且为 <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> 启用 <code class="docutils literal notranslate"><span class="pre">surrogateescape</span></code> <a class="reference internal" href="../library/codecs.html#error-handlers"><span class="std std-ref">错误处理器</span></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> 会继续使用 <code class="docutils literal notranslate"><span class="pre">backslashreplace</span></code> 如同在默认的局部感知模式下一样)</p></li>
|
||||
</ul>
|
||||
</div></blockquote>
|
||||
<p>作为低层级 API 发生改变的结果,其他高层级 API 也会表现出不同的默认行为:</p>
|
||||
<blockquote>
|
||||
<div><ul class="simple">
|
||||
<li><p>命令行参数,环境变量和文件名会使用 UTF-8 编码来解码为文本。</p></li>
|
||||
<li><p><a class="reference internal" href="../library/os.html#os.fsdecode" title="os.fsdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsdecode()</span></code></a> 和 <a class="reference internal" href="../library/os.html#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsencode()</span></code></a> 会使用 UTF-8 编码。</p></li>
|
||||
<li><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/io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a> 和 <a class="reference internal" href="../library/codecs.html#codecs.open" title="codecs.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.open()</span></code></a> 默认会使用 UTF-8 编码。 但是,它们默认仍将使用 strict 错误处理器,因此试图在文本模式下打开二进制文件将可能引发异常,而不是生成无意义的数据。</p></li>
|
||||
</ul>
|
||||
</div></blockquote>
|
||||
<p>请注意 UTF-8 模式下的标准流设置可以被 <span class="target" id="index-132"></span><a class="reference internal" href="#envvar-PYTHONIOENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONIOENCODING</span></code></a> 所覆盖(在默认的区域感知模式下也同样如此)。</p>
|
||||
<p>如果设置为“0”,则解释器以其默认的区域识别模式运行。</p>
|
||||
<p>设置任何其他非空字符串会在解释器初始化期间导致错误。</p>
|
||||
<p>如果根本未设置此环境变量,则解释器默认使用当前区域设置,<em>除非</em> 当前区域被标识为基于 ASCII 的旧式区域设置(如 <span class="target" id="index-133"></span><a class="reference internal" href="#envvar-PYTHONCOERCECLOCALE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONCOERCECLOCALE</span></code></a> 所述),并且区域强制转换被禁用或失败。 在此类旧式区域设置中,解释器将默认启用 UTF-8 模式,除非显式地指定不这样做。</p>
|
||||
<p>也可以使用 <a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">utf8</span></code> 选项。</p>
|
||||
<div class="versionadded">
|
||||
<p><span class="versionmodified added">3.7 新版功能: </span>有关更多详细信息,请参阅 <span class="target" id="index-134"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0540"><strong>PEP 540</strong></a> 。</p>
|
||||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<section id="debug-mode-variables">
|
||||
<h3><span class="section-number">1.2.1. </span>调试模式变量<a class="headerlink" href="#debug-mode-variables" title="永久链接至标题">¶</a></h3>
|
||||
<p>设置这些变量只会在Python的调试版本中产生影响。</p>
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONTHREADDEBUG">
|
||||
<code class="sig-name descname">PYTHONTHREADDEBUG</code><a class="headerlink" href="#envvar-PYTHONTHREADDEBUG" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设置,Python将打印线程调试信息。</p>
|
||||
<p>需要使用 <code class="docutils literal notranslate"><span class="pre">--with-pydebug</span></code> 构建选项配置Python。</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="envvar">
|
||||
<dt id="envvar-PYTHONDUMPREFS">
|
||||
<code class="sig-name descname">PYTHONDUMPREFS</code><a class="headerlink" href="#envvar-PYTHONDUMPREFS" title="永久链接至目标">¶</a></dt>
|
||||
<dd><p>如果设置,Python 将在关闭解释器后转储仍存活的对象和引用计数。</p>
|
||||
<p>需要使用 <code class="docutils literal notranslate"><span class="pre">--with-trace-refs</span></code> 构建选项配置Python。</p>
|
||||
</dd></dl>
|
||||
|
||||
</section>
|
||||
</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="#">1. 命令行与环境</a><ul>
|
||||
<li><a class="reference internal" href="#command-line">1.1. 命令行</a><ul>
|
||||
<li><a class="reference internal" href="#interface-options">1.1.1. 接口选项</a></li>
|
||||
<li><a class="reference internal" href="#generic-options">1.1.2. 通用选项</a></li>
|
||||
<li><a class="reference internal" href="#miscellaneous-options">1.1.3. 其他选项</a></li>
|
||||
<li><a class="reference internal" href="#options-you-shouldn-t-use">1.1.4. 不应当使用的选项</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#environment-variables">1.2. 环境变量</a><ul>
|
||||
<li><a class="reference internal" href="#debug-mode-variables">1.2.1. 调试模式变量</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="index.html"
|
||||
title="上一章">Python安装和使用</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="unix.html"
|
||||
title="下一章"><span class="section-number">2. </span>在类Unix环境下使用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/cmdline.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="unix.html" title="2. 在类Unix环境下使用Python"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="index.html" title="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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" >Python安装和使用</a> »</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">
|
||||
© <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>
|
||||
254
web/python-docs/using/editors.html
Normal file
254
web/python-docs/using/editors.html
Normal file
@@ -0,0 +1,254 @@
|
||||
|
||||
<!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>5. 编辑器和集成开发环境 — 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="Python语言参考" href="../reference/index.html" />
|
||||
<link rel="prev" title="4. 在苹果系统上使用 Python" href="mac.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/using/editors.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>
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="mac.html"
|
||||
title="上一章"><span class="section-number">4. </span>在苹果系统上使用 Python</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="../reference/index.html"
|
||||
title="下一章">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/editors.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/editors.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="../reference/index.html" title="Python语言参考"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="mac.html" title="4. 在苹果系统上使用 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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python安装和使用</a> »</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="editors-and-ides">
|
||||
<span id="editors"></span><h1><span class="section-number">5. </span>编辑器和集成开发环境<a class="headerlink" href="#editors-and-ides" title="永久链接至标题">¶</a></h1>
|
||||
<p>有很多支持Python编程语言的集成开发环境。大多数编辑器和集成开发环境支持语法高亮,调试工具和 <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a> 检查。</p>
|
||||
<p>请访问 <a class="reference external" href="https://wiki.python.org/moin/PythonEditors">Python Editors</a> 和 <a class="reference external" href="https://wiki.python.org/moin/IntegratedDevelopmentEnvironments">Integrated Development Environments</a> 以获取完整列表。</p>
|
||||
</section>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||||
<div class="sphinxsidebarwrapper">
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="mac.html"
|
||||
title="上一章"><span class="section-number">4. </span>在苹果系统上使用 Python</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="../reference/index.html"
|
||||
title="下一章">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/editors.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="../reference/index.html" title="Python语言参考"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="mac.html" title="4. 在苹果系统上使用 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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" >Python安装和使用</a> »</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">
|
||||
© <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>
|
||||
351
web/python-docs/using/index.html
Normal file
351
web/python-docs/using/index.html
Normal file
@@ -0,0 +1,351 @@
|
||||
|
||||
<!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>Python安装和使用 — 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="1. 命令行与环境" href="cmdline.html" />
|
||||
<link rel="prev" title="16. 附录" href="../tutorial/appendix.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/using/index.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>
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="../tutorial/appendix.html"
|
||||
title="上一章"><span class="section-number">16. </span>附录</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="cmdline.html"
|
||||
title="下一章"><span class="section-number">1. </span>命令行与环境</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/index.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/index.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="cmdline.html" title="1. 命令行与环境"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="../tutorial/appendix.html" title="16. 附录"
|
||||
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> »</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> »
|
||||
</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="python-setup-and-usage">
|
||||
<span id="using-index"></span><h1>Python安装和使用<a class="headerlink" href="#python-setup-and-usage" title="永久链接至标题">¶</a></h1>
|
||||
<p>这一部分文档专门介绍关于在不同平台上设置Python环境、调用解释器以及让使用Python更容易的一些事情的有用信息。</p>
|
||||
<div class="toctree-wrapper compound">
|
||||
<ul>
|
||||
<li class="toctree-l1"><a class="reference internal" href="cmdline.html">1. 命令行与环境</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="cmdline.html#command-line">1.1. 命令行</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="cmdline.html#interface-options">1.1.1. 接口选项</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="cmdline.html#generic-options">1.1.2. 通用选项</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="cmdline.html#miscellaneous-options">1.1.3. 其他选项</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="cmdline.html#options-you-shouldn-t-use">1.1.4. 不应当使用的选项</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="cmdline.html#environment-variables">1.2. 环境变量</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="cmdline.html#debug-mode-variables">1.2.1. 调试模式变量</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="unix.html">2. 在类Unix环境下使用Python</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="unix.html#getting-and-installing-the-latest-version-of-python">2.1. 获得并安装Python的最新版本</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="unix.html#on-linux">2.1.1. 在Linux中</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="unix.html#on-freebsd-and-openbsd">2.1.2. 在FreeBSD和OpenBSD上</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="unix.html#on-opensolaris">2.1.3. 在OpenSolaris系统上</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="unix.html#building-python">2.2. 构建Python</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="unix.html#python-related-paths-and-files">2.3. 与Python相关的路径和文件</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="unix.html#miscellaneous">2.4. 杂项</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="windows.html">3. 在Windows上使用 Python</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#the-full-installer">3.1. 完整安装程序</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#installation-steps">3.1.1. 安装步骤</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#removing-the-max-path-limitation">3.1.2. 删除 MAX_PATH 限制</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#installing-without-ui">3.1.3. 无UI 安装</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#installing-without-downloading">3.1.4. 免下载安装</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#modifying-an-install">3.1.5. 修改安装</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#the-microsoft-store-package">3.2. Microsoft Store包</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#known-issues">3.2.1. 已知的问题</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#the-nuget-org-packages">3.3. nuget.org 安装包</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#the-embeddable-package">3.4. 可嵌入的包</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#python-application">3.4.1. Python 应用程序</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#embedding-python">3.4.2. 嵌入Python</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#alternative-bundles">3.5. 替代捆绑包</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#configuring-python">3.6. 配置Python</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#excursus-setting-environment-variables">3.6.1. 附录:设置环境变量</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#finding-the-python-executable">3.6.2. 查找Python可执行文件</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#utf-8-mode">3.7. UTF-8 模式</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#python-launcher-for-windows">3.8. 适用于Windows的Python启动器</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#getting-started">3.8.1. 入门</a><ul>
|
||||
<li class="toctree-l4"><a class="reference internal" href="windows.html#from-the-command-line">3.8.1.1. 从命令行</a></li>
|
||||
<li class="toctree-l4"><a class="reference internal" href="windows.html#virtual-environments">3.8.1.2. 从虚拟环境</a></li>
|
||||
<li class="toctree-l4"><a class="reference internal" href="windows.html#from-a-script">3.8.1.3. 从脚本</a></li>
|
||||
<li class="toctree-l4"><a class="reference internal" href="windows.html#from-file-associations">3.8.1.4. 从文件关联</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#shebang-lines">3.8.2. Shebang 行</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#arguments-in-shebang-lines">3.8.3. shebang 行的参数</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#customization">3.8.4. 自定义</a><ul>
|
||||
<li class="toctree-l4"><a class="reference internal" href="windows.html#customization-via-ini-files">3.8.4.1. 通过INI文件自定义</a></li>
|
||||
<li class="toctree-l4"><a class="reference internal" href="windows.html#customizing-default-python-versions">3.8.4.2. 自定义默认的Python版本</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#diagnostics">3.8.5. 诊断</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#finding-modules">3.9. 查找模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#additional-modules">3.10. 附加模块</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#pywin32">3.10.1. PyWin32</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#cx-freeze">3.10.2. cx_Freeze</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="windows.html#wconio">3.10.3. WConio</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#compiling-python-on-windows">3.11. 在Windows上编译Python</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="windows.html#other-platforms">3.12. 其他平台</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="mac.html">4. 在苹果系统上使用 Python</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="mac.html#getting-and-installing-macpython">4.1. 获取和安装 MacPython</a><ul>
|
||||
<li class="toctree-l3"><a class="reference internal" href="mac.html#how-to-run-a-python-script">4.1.1. 如何运行 Python 脚本</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="mac.html#running-scripts-with-a-gui">4.1.2. 运行有图形界面的脚本</a></li>
|
||||
<li class="toctree-l3"><a class="reference internal" href="mac.html#configuration">4.1.3. 配置</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="mac.html#the-ide">4.2. IDE</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="mac.html#installing-additional-python-packages">4.3. 安装额外的 Python 包</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="mac.html#gui-programming-on-the-mac">4.4. Mac 上的图形界面编程</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="mac.html#distributing-python-applications-on-the-mac">4.5. 在 Mac 上分发 Python 应用程序</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="mac.html#other-resources">4.6. 其他资源</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="editors.html">5. 编辑器和集成开发环境</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||||
<div class="sphinxsidebarwrapper">
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="../tutorial/appendix.html"
|
||||
title="上一章"><span class="section-number">16. </span>附录</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="cmdline.html"
|
||||
title="下一章"><span class="section-number">1. </span>命令行与环境</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/index.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="cmdline.html" title="1. 命令行与环境"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="../tutorial/appendix.html" title="16. 附录"
|
||||
>上一页</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> »</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> »
|
||||
</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">
|
||||
© <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>
|
||||
358
web/python-docs/using/mac.html
Normal file
358
web/python-docs/using/mac.html
Normal file
@@ -0,0 +1,358 @@
|
||||
|
||||
<!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>4. 在苹果系统上使用 Python — 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="5. 编辑器和集成开发环境" href="editors.html" />
|
||||
<link rel="prev" title="3. 在Windows上使用 Python" href="windows.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/using/mac.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="#">4. 在苹果系统上使用 Python</a><ul>
|
||||
<li><a class="reference internal" href="#getting-and-installing-macpython">4.1. 获取和安装 MacPython</a><ul>
|
||||
<li><a class="reference internal" href="#how-to-run-a-python-script">4.1.1. 如何运行 Python 脚本</a></li>
|
||||
<li><a class="reference internal" href="#running-scripts-with-a-gui">4.1.2. 运行有图形界面的脚本</a></li>
|
||||
<li><a class="reference internal" href="#configuration">4.1.3. 配置</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#the-ide">4.2. IDE</a></li>
|
||||
<li><a class="reference internal" href="#installing-additional-python-packages">4.3. 安装额外的 Python 包</a></li>
|
||||
<li><a class="reference internal" href="#gui-programming-on-the-mac">4.4. Mac 上的图形界面编程</a></li>
|
||||
<li><a class="reference internal" href="#distributing-python-applications-on-the-mac">4.5. 在 Mac 上分发 Python 应用程序</a></li>
|
||||
<li><a class="reference internal" href="#other-resources">4.6. 其他资源</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="windows.html"
|
||||
title="上一章"><span class="section-number">3. </span>在Windows上使用 Python</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="editors.html"
|
||||
title="下一章"><span class="section-number">5. </span>编辑器和集成开发环境</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/mac.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/mac.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="editors.html" title="5. 编辑器和集成开发环境"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="windows.html" title="3. 在Windows上使用 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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python安装和使用</a> »</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-a-macintosh">
|
||||
<span id="using-on-mac"></span><h1><span class="section-number">4. </span>在苹果系统上使用 Python<a class="headerlink" href="#using-python-on-a-macintosh" title="永久链接至标题">¶</a></h1>
|
||||
<dl class="field-list simple">
|
||||
<dt class="field-odd">作者</dt>
|
||||
<dd class="field-odd"><p>Bob Savage <<a class="reference external" href="mailto:bobsavage%40mac.com">bobsavage<span>@</span>mac<span>.</span>com</a>></p>
|
||||
</dd>
|
||||
</dl>
|
||||
<p>运行 Mac OS X 的 Macintosh上 的 Python 原则上与任何其他 Unix 平台上的 Python 非常相似,但是还有一些额外的功能,例如 IDE 和包管理器,值得一提。</p>
|
||||
<section id="getting-and-installing-macpython">
|
||||
<span id="getting-osx"></span><h2><span class="section-number">4.1. </span>获取和安装 MacPython<a class="headerlink" href="#getting-and-installing-macpython" title="永久链接至标题">¶</a></h2>
|
||||
<p>Mac OS X 10.8 附带 Apple 预安装的 Python 2.7 。 如果你愿意,可以从 Python 网站( <a class="reference external" href="https://www.python.org">https://www.python.org</a> )安装最新版本的 Python 3 。 Python 的当前“通用二进制”版本可以在 Mac 的新 Intel 和传统 PPC CPU 上本地运行。</p>
|
||||
<p>你安装后得到的东西有:</p>
|
||||
<ul class="simple">
|
||||
<li><p>会有一个 <code class="file docutils literal notranslate"><span class="pre">Python</span> <span class="pre">3.8</span></code> 文件夹在你的 <code class="file docutils literal notranslate"><span class="pre">Applications</span></code> 文件夹中。 在这里你可以找到 IDLE,它是作为官方 Python 发行版标准组成部分的开发环境;以及 PythonLauncher,它负责处理在 Finder 中双击 Python 脚本的操作。</p></li>
|
||||
<li><p>框架 <code class="file docutils literal notranslate"><span class="pre">/Library/Frameworks/Python.framework</span></code> ,包括 Python 可执行文件和库。安装程序将此位置添加到 shell 路径。 要卸载 MacPython ,你可以简单地移除这三个项目。 Python 可执行文件的符号链接放在 /usr/local/bin/ 中。</p></li>
|
||||
</ul>
|
||||
<p>Apple 提供的 Python 版本分别安装在 <code class="file docutils literal notranslate"><span class="pre">/System/Library/Frameworks/Python.framework</span></code> 和 <code class="file docutils literal notranslate"><span class="pre">/usr/bin/python</span></code> 中。 你永远不应修改或删除这些内容,因为它们由 Apple 控制并由 Apple 或第三方软件使用。 请记住,如果你选择从 python.org 安装较新的 Python 版本,那么你的计算机上将安装两个不同但都有用的 Python ,因此你的路径和用法与你想要执行的操作一致非常重要。</p>
|
||||
<p>IDLE 包含一个帮助菜单,允许你访问 Python 文档。 如果您是 Python 的新手,你应该开始阅读该文档中的教程介绍。</p>
|
||||
<p>如果你熟悉其他 Unix 平台上的 Python ,那么你应该阅读有关从 Unix shell 运行 Python 脚本的部分。</p>
|
||||
<section id="how-to-run-a-python-script">
|
||||
<h3><span class="section-number">4.1.1. </span>如何运行 Python 脚本<a class="headerlink" href="#how-to-run-a-python-script" title="永久链接至标题">¶</a></h3>
|
||||
<p>在 Mac OS X 上开始使用 Python 的最佳方法是通过 IDLE 集成开发环境,参见 <a class="reference internal" href="#ide"><span class="std std-ref">IDE</span></a> 部分,并在 IDE 运行时使用“帮助”菜单。</p>
|
||||
<p>如果要从终端窗口命令行或 Finder 运行 Python 脚本,首先需要一个编辑器来创建脚本。 Mac OS X 附带了许多标准的 Unix 命令行编辑器,如 <strong class="program">vim</strong> 和 <strong class="program">emacs</strong> 。 如果你想要一个更 Mac 化的编辑器,那么:program:<cite>BBEdit</cite> 或来自 Bare Bones Software 的 <strong class="program">TextWrangler</strong> (参见 <a class="reference external" href="http://www.barebones.com/products/bbedit/index.html">http://www.barebones.com/products/bbedit/index.html</a> )是不错的选择 ,就如 <strong class="program">TextMate`(参见 https://macromates.com/ )。 其他编辑器包括 :program:`Gvim</strong> ( <a class="reference external" href="http://macvim-dev.github.io/macvim/">http://macvim-dev.github.io/macvim/</a> )和 <strong class="program">Aquamacs</strong> ( <a class="reference external" href="http://aquamacs.org/">http://aquamacs.org/</a> )。</p>
|
||||
<p>要从终端窗口运行脚本,必须确保:file:<cite>/usr/local/bin</cite> 位于 shell 搜索路径中。</p>
|
||||
<p>要从 Finder 运行你的脚本,你有两个选择:</p>
|
||||
<ul class="simple">
|
||||
<li><p>把脚本拖拽到 <strong class="program">PythonLauncher</strong></p></li>
|
||||
<li><p>选择 <strong class="program">PythonLauncher</strong> 作为通过 finder Info 窗口打开脚本(或任何 .py 脚本)的默认应用程序,然后双击脚本。 <strong class="program">PythonLauncher</strong> 有各种首选项来控制脚本的启动方式。 拖拽方式允许你为一次调用更改这些选项,或使用其“首选项”菜单全局更改内容。</p></li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="running-scripts-with-a-gui">
|
||||
<span id="osx-gui-scripts"></span><h3><span class="section-number">4.1.2. </span>运行有图形界面的脚本<a class="headerlink" href="#running-scripts-with-a-gui" title="永久链接至标题">¶</a></h3>
|
||||
<p>对于旧版本的 Python ,你需要注意一个 Mac OS X 的怪异之处:与 Aqua 窗口管理器通信的程序(换而言之,任何具有图形界面的程序)需要以特殊方式运行。 使用 <strong class="program">pythonw</strong> 而不是 <strong class="program">python</strong> 来启动这样的脚本。</p>
|
||||
<p>对于Python 3.8,可以使用 <strong class="program">python</strong> 或 <strong class="program">pythonw</strong></p>
|
||||
</section>
|
||||
<section id="configuration">
|
||||
<h3><span class="section-number">4.1.3. </span>配置<a class="headerlink" href="#configuration" title="永久链接至标题">¶</a></h3>
|
||||
<p>OS X 上的 Python 遵循所有标准的 Unix 环境变量,例如 <span class="target" id="index-2"></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> ,但是为 Finder 启动的程序设置这些变量是非标准的,因为 Finder 在启动时不读取你的 <code class="file docutils literal notranslate"><span class="pre">.profile</span></code> 或 <code class="file docutils literal notranslate"><span class="pre">.cshrc</span></code> 。你需要创建一个文件 <code class="file docutils literal notranslate"><span class="pre">~/.MacOSX/environment.plist</span></code> 。 有关详细信息,请参阅 Apple 的技术文档 QA1067 。</p>
|
||||
<p>更多关于在 MacPython 中安装 Python 包的信息,参阅 <a class="reference internal" href="#mac-package-manager"><span class="std std-ref">安装额外的 Python 包</span></a> 部分。</p>
|
||||
</section>
|
||||
</section>
|
||||
<section id="the-ide">
|
||||
<span id="ide"></span><h2><span class="section-number">4.2. </span>IDE<a class="headerlink" href="#the-ide" title="永久链接至标题">¶</a></h2>
|
||||
<p>MacPython 附带标准的 IDLE 开发环境。 有关使用 IDLE 的详细介绍,请访问 <a class="reference external" href="http://www.hashcollision.org/hkn/python/idle_intro/index.html">http://www.hashcollision.org/hkn/python/idle_intro/index.html</a> 。</p>
|
||||
</section>
|
||||
<section id="installing-additional-python-packages">
|
||||
<span id="mac-package-manager"></span><h2><span class="section-number">4.3. </span>安装额外的 Python 包<a class="headerlink" href="#installing-additional-python-packages" title="永久链接至标题">¶</a></h2>
|
||||
<p>有几个方法可以安装额外的 Python 包:</p>
|
||||
<ul class="simple">
|
||||
<li><p>可以通过标准的 Python distutils 模式( <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">install</span></code> )安装软件包。</p></li>
|
||||
<li><p>许多包也可以通过 <strong class="program">setuptools</strong> 扩展或 <strong class="program">pip</strong> 包装器安装,请参阅 <a class="reference external" href="https://pip.pypa.io/">https://pip.pypa.io/</a> 。</p></li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="gui-programming-on-the-mac">
|
||||
<h2><span class="section-number">4.4. </span>Mac 上的图形界面编程<a class="headerlink" href="#gui-programming-on-the-mac" title="永久链接至标题">¶</a></h2>
|
||||
<p>使用 Python 在 Mac 上构建 GUI 应用程序有多种选择。</p>
|
||||
<p><em>PyObjC</em> 是一个 Python 到 Apple 的 Objective-C/Cocoa 框架的绑定,这是大多数现代 Mac 开发的基础。 有关 PyObjC 的信息,请访问 <a class="reference external" href="https://pypi.org/project/pyobjc/">https://pypi.org/project/pyobjc/</a>。</p>
|
||||
<p>标准的 Python GUI 工具包是 <a class="reference internal" href="../library/tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> ,基于跨平台的 Tk 工具包( <a class="reference external" href="https://www.tcl.tk">https://www.tcl.tk</a> )。 Apple 的 OS X 捆绑了 Aqua 原生版本的 Tk ,最新版本可以从 <a class="reference external" href="https://www.activestate.com">https://www.activestate.com</a> 下载和安装;它也可以从源代码构建。</p>
|
||||
<p><em>wxPython</em> 是另一种流行的跨平台 GUI 工具包,可在 Mac OS X 上本机运行。软件包和文档可从 <a class="reference external" href="https://www.wxpython.org">https://www.wxpython.org</a> 获得。</p>
|
||||
<p><em>PyQt</em> 是另一种流行的跨平台 GUI 工具包,可在 Mac OS X 上本机运行。更多信息可在 <a class="reference external" href="https://riverbankcomputing.com/software/pyqt/intro">https://riverbankcomputing.com/software/pyqt/intro</a> 上找到。</p>
|
||||
</section>
|
||||
<section id="distributing-python-applications-on-the-mac">
|
||||
<h2><span class="section-number">4.5. </span>在 Mac 上分发 Python 应用程序<a class="headerlink" href="#distributing-python-applications-on-the-mac" title="永久链接至标题">¶</a></h2>
|
||||
<p>在 Mac 上部署独立 Python 应用程序的标准工具是 <strong class="program">py2app</strong> 。有关安装和使用 py2app 的更多信息,请访问 <a class="reference external" href="http://undefined.org/python/#py2app">http://undefined.org/python/#py2app</a> 。</p>
|
||||
</section>
|
||||
<section id="other-resources">
|
||||
<h2><span class="section-number">4.6. </span>其他资源<a class="headerlink" href="#other-resources" title="永久链接至标题">¶</a></h2>
|
||||
<p>MacPython 邮件列表是 Mac 上 Python 用户和开发人员的优秀支持资源:</p>
|
||||
<p><a class="reference external" href="https://www.python.org/community/sigs/current/pythonmac-sig/">https://www.python.org/community/sigs/current/pythonmac-sig/</a></p>
|
||||
<p>另一个有用的资源是 MacPython wiki :</p>
|
||||
<p><a class="reference external" href="https://wiki.python.org/moin/MacPython">https://wiki.python.org/moin/MacPython</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="#">4. 在苹果系统上使用 Python</a><ul>
|
||||
<li><a class="reference internal" href="#getting-and-installing-macpython">4.1. 获取和安装 MacPython</a><ul>
|
||||
<li><a class="reference internal" href="#how-to-run-a-python-script">4.1.1. 如何运行 Python 脚本</a></li>
|
||||
<li><a class="reference internal" href="#running-scripts-with-a-gui">4.1.2. 运行有图形界面的脚本</a></li>
|
||||
<li><a class="reference internal" href="#configuration">4.1.3. 配置</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#the-ide">4.2. IDE</a></li>
|
||||
<li><a class="reference internal" href="#installing-additional-python-packages">4.3. 安装额外的 Python 包</a></li>
|
||||
<li><a class="reference internal" href="#gui-programming-on-the-mac">4.4. Mac 上的图形界面编程</a></li>
|
||||
<li><a class="reference internal" href="#distributing-python-applications-on-the-mac">4.5. 在 Mac 上分发 Python 应用程序</a></li>
|
||||
<li><a class="reference internal" href="#other-resources">4.6. 其他资源</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="windows.html"
|
||||
title="上一章"><span class="section-number">3. </span>在Windows上使用 Python</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="editors.html"
|
||||
title="下一章"><span class="section-number">5. </span>编辑器和集成开发环境</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/mac.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="editors.html" title="5. 编辑器和集成开发环境"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="windows.html" title="3. 在Windows上使用 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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" >Python安装和使用</a> »</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">
|
||||
© <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>
|
||||
387
web/python-docs/using/unix.html
Normal file
387
web/python-docs/using/unix.html
Normal file
@@ -0,0 +1,387 @@
|
||||
|
||||
<!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>2. 在类Unix环境下使用Python — 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="3. 在Windows上使用 Python" href="windows.html" />
|
||||
<link rel="prev" title="1. 命令行与环境" href="cmdline.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/using/unix.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="#">2. 在类Unix环境下使用Python</a><ul>
|
||||
<li><a class="reference internal" href="#getting-and-installing-the-latest-version-of-python">2.1. 获得并安装Python的最新版本</a><ul>
|
||||
<li><a class="reference internal" href="#on-linux">2.1.1. 在Linux中</a></li>
|
||||
<li><a class="reference internal" href="#on-freebsd-and-openbsd">2.1.2. 在FreeBSD和OpenBSD上</a></li>
|
||||
<li><a class="reference internal" href="#on-opensolaris">2.1.3. 在OpenSolaris系统上</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#building-python">2.2. 构建Python</a></li>
|
||||
<li><a class="reference internal" href="#python-related-paths-and-files">2.3. 与Python相关的路径和文件</a></li>
|
||||
<li><a class="reference internal" href="#miscellaneous">2.4. 杂项</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="cmdline.html"
|
||||
title="上一章"><span class="section-number">1. </span>命令行与环境</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="windows.html"
|
||||
title="下一章"><span class="section-number">3. </span>在Windows上使用 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/unix.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/unix.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="windows.html" title="3. 在Windows上使用 Python"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="cmdline.html" title="1. 命令行与环境"
|
||||
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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python安装和使用</a> »</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-unix-platforms">
|
||||
<span id="using-on-unix"></span><h1><span class="section-number">2. </span>在类Unix环境下使用Python<a class="headerlink" href="#using-python-on-unix-platforms" title="永久链接至标题">¶</a></h1>
|
||||
<section id="getting-and-installing-the-latest-version-of-python">
|
||||
<h2><span class="section-number">2.1. </span>获得并安装Python的最新版本<a class="headerlink" href="#getting-and-installing-the-latest-version-of-python" title="永久链接至标题">¶</a></h2>
|
||||
<section id="on-linux">
|
||||
<h3><span class="section-number">2.1.1. </span>在Linux中<a class="headerlink" href="#on-linux" title="永久链接至标题">¶</a></h3>
|
||||
<p>Python预装在大多数Linux发行版上,并作为一个包提供给所有其他用户。 但是,您可能想要使用的某些功能在发行版提供的软件包中不可用。这时您可以从源代码轻松编译最新版本的Python。</p>
|
||||
<p>如果Python没有预先安装并且不在发行版提供的库中,您可以轻松地为自己使用的发行版创建包。 参阅以下链接:</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><a class="reference external" href="https://www.debian.org/doc/manuals/maint-guide/first.en.html">https://www.debian.org/doc/manuals/maint-guide/first.en.html</a></dt><dd><p>对于Debian用户</p>
|
||||
</dd>
|
||||
<dt><a class="reference external" href="https://en.opensuse.org/Portal:Packaging">https://en.opensuse.org/Portal:Packaging</a></dt><dd><p>对于OpenSuse用户</p>
|
||||
</dd>
|
||||
<dt><a class="reference external" href="https://docs-old.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-creating-rpms.html">https://docs-old.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-creating-rpms.html</a></dt><dd><p>对于Fedora用户</p>
|
||||
</dd>
|
||||
<dt><a class="reference external" href="http://www.slackbook.org/html/package-management-making-packages.html">http://www.slackbook.org/html/package-management-making-packages.html</a></dt><dd><p>对于Slackware用户</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="on-freebsd-and-openbsd">
|
||||
<h3><span class="section-number">2.1.2. </span>在FreeBSD和OpenBSD上<a class="headerlink" href="#on-freebsd-and-openbsd" title="永久链接至标题">¶</a></h3>
|
||||
<ul>
|
||||
<li><p>FreeBSD用户,使用以下命令添加包:</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>pkg<span class="w"> </span>install<span class="w"> </span>python3
|
||||
</pre></div>
|
||||
</div>
|
||||
</li>
|
||||
<li><p>OpenBSD用户,使用以下命令添加包:</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>pkg_add<span class="w"> </span>-r<span class="w"> </span>python
|
||||
|
||||
pkg_add<span class="w"> </span>ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/<insert<span class="w"> </span>your<span class="w"> </span>architecture<span class="w"> </span>here>/python-<version>.tgz
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>例如:i386用户获取Python 2.5.1的可用版本:</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>pkg_add<span class="w"> </span>ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/i386/python-2.5.1p2.tgz
|
||||
</pre></div>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="on-opensolaris">
|
||||
<h3><span class="section-number">2.1.3. </span>在OpenSolaris系统上<a class="headerlink" href="#on-opensolaris" title="永久链接至标题">¶</a></h3>
|
||||
<p>你可以从 <a class="reference external" href="https://www.opencsw.org/">OpenCSW</a> 获取、安装及使用各种版本的Python。比如 <code class="docutils literal notranslate"><span class="pre">pkgutil</span> <span class="pre">-i</span> <span class="pre">python27</span></code> 。</p>
|
||||
</section>
|
||||
</section>
|
||||
<section id="building-python">
|
||||
<span id="building-python-on-unix"></span><h2><span class="section-number">2.2. </span>构建Python<a class="headerlink" href="#building-python" 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">clone</a> 。 (如果你想要制作补丁,则需要克隆代码。)</p>
|
||||
<p>构建过程由常用命令组成:</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>./configure
|
||||
make
|
||||
make<span class="w"> </span>install
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>特定Unix平台的配置选项和注意事项通常记录在Python源代码的根目录下的 <a class="reference external" href="https://github.com/python/cpython/tree/3.8/README.rst">README.rst</a> 文件中。</p>
|
||||
<div class="admonition warning">
|
||||
<p class="admonition-title">警告</p>
|
||||
<p><code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">install</span></code> 可以覆盖或伪装 <code class="file docutils literal notranslate"><span class="pre">python3</span></code> 二进制文件。因此,建议使用 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">altinstall</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">install</span></code> ,因为后者只安装了 <code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em><span class="pre">/bin/python</span><em><span class="pre">version</span></em></code> 。</p>
|
||||
</div>
|
||||
</section>
|
||||
<section id="python-related-paths-and-files">
|
||||
<h2><span class="section-number">2.3. </span>与Python相关的路径和文件<a class="headerlink" href="#python-related-paths-and-files" title="永久链接至标题">¶</a></h2>
|
||||
<p>这取决于本地安装惯例; <span class="target" id="index-6"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">prefix</span></code> ( <code class="docutils literal notranslate"><span class="pre">${prefix}</span></code> )和 <span class="target" id="index-7"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">exec_prefix</span></code> ( <code class="docutils literal notranslate"><span class="pre">${exec_prefix}</span></code> ) 取决于安装,应解释为GNU软件;它们可能相同。</p>
|
||||
<p>例如,在大多数Linux系统上,两者的默认值是 <code class="file docutils literal notranslate"><span class="pre">/usr</span></code> 。</p>
|
||||
<table class="docutils align-default">
|
||||
<colgroup>
|
||||
<col style="width: 53%" />
|
||||
<col style="width: 47%" />
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr class="row-odd"><th class="head"><p>文件/目录</p></th>
|
||||
<th class="head"><p>含义</p></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr class="row-even"><td><p><code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em><span class="pre">/bin/python3</span></code></p></td>
|
||||
<td><p>解释器的推荐位置</p></td>
|
||||
</tr>
|
||||
<tr class="row-odd"><td><p><code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/lib/python</span><em><span class="pre">version</span></em></code>,
|
||||
<code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em><span class="pre">/lib/python</span><em><span class="pre">version</span></em></code></p></td>
|
||||
<td><p>包含标准模块的目录的推荐位置</p></td>
|
||||
</tr>
|
||||
<tr class="row-even"><td><p><code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/include/python</span><em><span class="pre">version</span></em></code>,
|
||||
<code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em><span class="pre">/include/python</span><em><span class="pre">version</span></em></code></p></td>
|
||||
<td><p>包含开发Python扩展和嵌入解释器所需的include文件的目录的推荐位置</p></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<section id="miscellaneous">
|
||||
<h2><span class="section-number">2.4. </span>杂项<a class="headerlink" href="#miscellaneous" title="永久链接至标题">¶</a></h2>
|
||||
<p>要在Unix上使用Python脚本,需要添加可执行权限,例如:</p>
|
||||
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>chmod<span class="w"> </span>+x<span class="w"> </span>script
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>并在脚本的顶部放置一个合适的Shebang线。一个很好的选择通常是:</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>将在整个 <span class="target" id="index-8"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 中搜索Python解释器。但是,某些Unix系统可能没有 <strong class="program">env</strong> 命令,因此可能需要将 <code class="docutils literal notranslate"><span class="pre">/usr/bin/python3</span></code> 硬编码为解释器路径。</p>
|
||||
<p>要在Python脚本中使用shell命令,请查看 <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></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="#">2. 在类Unix环境下使用Python</a><ul>
|
||||
<li><a class="reference internal" href="#getting-and-installing-the-latest-version-of-python">2.1. 获得并安装Python的最新版本</a><ul>
|
||||
<li><a class="reference internal" href="#on-linux">2.1.1. 在Linux中</a></li>
|
||||
<li><a class="reference internal" href="#on-freebsd-and-openbsd">2.1.2. 在FreeBSD和OpenBSD上</a></li>
|
||||
<li><a class="reference internal" href="#on-opensolaris">2.1.3. 在OpenSolaris系统上</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#building-python">2.2. 构建Python</a></li>
|
||||
<li><a class="reference internal" href="#python-related-paths-and-files">2.3. 与Python相关的路径和文件</a></li>
|
||||
<li><a class="reference internal" href="#miscellaneous">2.4. 杂项</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="cmdline.html"
|
||||
title="上一章"><span class="section-number">1. </span>命令行与环境</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="windows.html"
|
||||
title="下一章"><span class="section-number">3. </span>在Windows上使用 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/unix.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="windows.html" title="3. 在Windows上使用 Python"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="cmdline.html" title="1. 命令行与环境"
|
||||
>上一页</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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" >Python安装和使用</a> »</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">
|
||||
© <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>
|
||||
984
web/python-docs/using/windows.html
Normal file
984
web/python-docs/using/windows.html
Normal file
@@ -0,0 +1,984 @@
|
||||
|
||||
<!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 — 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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python安装和使用</a> »</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,您可以(在命令提示符>)使用以下命令:</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="Just for me, no test suite."
|
||||
</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"><Options></span>
|
||||
<span class="w"> </span><span class="nt"><Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">"InstallAllUsers"</span><span class="w"> </span><span class="na">Value=</span><span class="s">"no"</span><span class="w"> </span><span class="nt">/></span>
|
||||
<span class="w"> </span><span class="nt"><Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">"Include_launcher"</span><span class="w"> </span><span class="na">Value=</span><span class="s">"0"</span><span class="w"> </span><span class="nt">/></span>
|
||||
<span class="w"> </span><span class="nt"><Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">"Include_test"</span><span class="w"> </span><span class="na">Value=</span><span class="s">"no"</span><span class="w"> </span><span class="nt">/></span>
|
||||
<span class="w"> </span><span class="nt"><Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">"SimpleInstall"</span><span class="w"> </span><span class="na">Value=</span><span class="s">"yes"</span><span class="w"> </span><span class="nt">/></span>
|
||||
<span class="w"> </span><span class="nt"><Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">"SimpleInstallDescription"</span><span class="nt">></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"></Option></span>
|
||||
<span class="nt"></Options></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应用程序中搜索 "Python 3.8" 。确保您选择的应用程序由 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">设置-->主页-->应用和功能</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
|
||||
> .\python.3.5.2\tools\python.exe -V
|
||||
Python 3.5.2
|
||||
|
||||
# With -ExcludeVersion
|
||||
> .\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(包括所有依赖项,如Idle),pip和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>流行的科学模块(如numpy,scipy和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:\></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:\></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:\></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/NT),Louis 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>你仍然可以通过 "mbcs" 编解码器来使用系统的编码格式。</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>'py' 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">"hello from Python </span><span class="si">%s</span><span class="se">\n</span><span class="s2">"</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> 开头,则称为 "shebang" 行。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 开始,可以通过 "-64" 后缀调用 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文件 - 在当前用户的 "application data" 目录中搜索 <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版本。版本限定符以主版本号开头,可以选择后跟 ('.') 和次版本说明符。此外,可以通过添加 "-32" 或 “-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> 以指定默认版本限定符。 如果未设置,则默认为 "3"。 该变量可以指定能通过命令行传递的任何值,比如 "3", "3.7", "3.7-32" 或 "3.7-64"。 (请注意 "-64" 选项仅适用于 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.minor)Python版本的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> 将使用最新安装的Python(PY_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>额外的 "应用程序路径" 可以作为子键被同时添加到注册表 <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>或 "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</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> »</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> »
|
||||
</li>
|
||||
|
||||
<li class="nav-item nav-item-1"><a href="index.html" >Python安装和使用</a> »</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">
|
||||
© <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>
|
||||
Reference in New Issue
Block a user