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

338 lines
21 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>16. 附录 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="Python安装和使用" href="../using/index.html" />
<link rel="prev" title="15. 浮点算术:争议和限制" href="floatingpoint.html" />
<link rel="canonical" href="https://docs.python.org/3/tutorial/appendix.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="#">16. 附录</a><ul>
<li><a class="reference internal" href="#interactive-mode">16.1. 交互模式</a><ul>
<li><a class="reference internal" href="#error-handling">16.1.1. 错误处理</a></li>
<li><a class="reference internal" href="#executable-python-scripts">16.1.2. 可执行的Python脚本</a></li>
<li><a class="reference internal" href="#the-interactive-startup-file">16.1.3. 交互式启动文件</a></li>
<li><a class="reference internal" href="#the-customization-modules">16.1.4. 定制模块</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="floatingpoint.html"
title="上一章"><span class="section-number">15. </span>浮点算术:争议和限制</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="../using/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/tutorial/appendix.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/tutorial/appendix.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="../using/index.html" title="Python安装和使用"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="floatingpoint.html" title="15. 浮点算术:争议和限制"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 教程</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="appendix">
<span id="tut-appendix"></span><h1><span class="section-number">16. </span>附录<a class="headerlink" href="#appendix" title="永久链接至标题"></a></h1>
<section id="interactive-mode">
<span id="tut-interac"></span><h2><span class="section-number">16.1. </span>交互模式<a class="headerlink" href="#interactive-mode" title="永久链接至标题"></a></h2>
<section id="error-handling">
<span id="tut-error"></span><h3><span class="section-number">16.1.1. </span>错误处理<a class="headerlink" href="#error-handling" title="永久链接至标题"></a></h3>
<p>当发生错误时,解释器会打印错误信息和错误堆栈。在交互模式下,将返回到主命令提示符;如果输入内容来自文件,在打印错误堆栈之后,程序会以非零状态退出。(这里所说的错误不包括 <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> 语句中由 <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> 所捕获的异常。)有些错误是无条件致命的,会导致程序以非零状态退出;比如内部逻辑矛盾或内存耗尽。所有错误信息都会被写入标准错误流;而命令的正常输出则被写入标准输出流。</p>
<p>将中断字符(通常为 <kbd class="kbd docutils literal notranslate">Control-C</kbd><kbd class="kbd docutils literal notranslate">Delete</kbd> )键入主要或辅助提示会取消输入并返回主提示符。 <a class="footnote-reference brackets" href="#id2" id="id1">1</a> 在执行命令时键入中断引发的 <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> 异常,可以由 <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> 语句处理。</p>
</section>
<section id="executable-python-scripts">
<span id="tut-scripts"></span><h3><span class="section-number">16.1.2. </span>可执行的Python脚本<a class="headerlink" href="#executable-python-scripts" title="永久链接至标题"></a></h3>
<p>在BSD等类Unix系统上Python脚本可以直接执行就像shell脚本一样第一行添加:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3.5</span>
</pre></div>
</div>
<p>(假设解释器位于用户的 <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> )脚本的开头,并将文件设置为可执行。 <code class="docutils literal notranslate"><span class="pre">#!</span></code> 必须是文件的前两个字符。在某些平台上第一行必须以Unix样式的行结尾<code class="docutils literal notranslate"><span class="pre">'\n'</span></code>结束而不是以Windows<code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code>)行结尾。请注意,散列或磅字符 <code class="docutils literal notranslate"><span class="pre">'#'</span></code> 在Python中代表注释开始。</p>
<p>可以使用 <strong class="program">chmod</strong> 命令为脚本提供可执行模式或权限。</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>myscript.py
</pre></div>
</div>
<p>在Windows系统上没有“可执行模式”的概念。 Python安装程序自动将 <code class="docutils literal notranslate"><span class="pre">.py</span></code> 文件与 <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> 相关联这样双击Python文件就会将其作为脚本运行。扩展也可以是 <code class="docutils literal notranslate"><span class="pre">.pyw</span></code> ,在这种情况下,会隐藏通常出现的控制台窗口。</p>
</section>
<section id="the-interactive-startup-file">
<span id="tut-startup"></span><h3><span class="section-number">16.1.3. </span>交互式启动文件<a class="headerlink" href="#the-interactive-startup-file" title="永久链接至标题"></a></h3>
<p>当您以交互方式使用Python时每次启动解释器时都会执行一些标准命令这通常很方便。您可以通过将名为 <span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a> 的环境变量设置为包含启动命令的文件名来实现。这类似于Unix shell的 <code class="file docutils literal notranslate"><span class="pre">.profile</span></code> 功能。</p>
<p>This file is only read in interactive sessions, not when Python reads commands
from a script, and not when <code class="file docutils literal notranslate"><span class="pre">/dev/tty</span></code> is given as the explicit source of
commands (which otherwise behaves like an interactive session). It is executed
in the same namespace where interactive commands are executed, so that objects
that it defines or imports can be used without qualification in the interactive
session. You can also change the prompts <code class="docutils literal notranslate"><span class="pre">sys.ps1</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.ps2</span></code> in this
file.</p>
<p>如果你想从当前目录中读取一个额外的启动文件,你可以使用像 <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">os.path.isfile('.pythonrc.py'):</span> <span class="pre">exec(open('.pythonrc.py').read())</span></code> 这样的代码在全局启动文件中对它进行编程。如果要在脚本中使用启动文件,则必须在脚本中显式执行此操作:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;PYTHONSTARTUP&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">filename</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="k">as</span> <span class="n">fobj</span><span class="p">:</span>
<span class="n">startup_file</span> <span class="o">=</span> <span class="n">fobj</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">exec</span><span class="p">(</span><span class="n">startup_file</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="the-customization-modules">
<span id="tut-customize"></span><h3><span class="section-number">16.1.4. </span>定制模块<a class="headerlink" href="#the-customization-modules" title="永久链接至标题"></a></h3>
<p>Python提供了两个钩子来让你自定义它<code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code><code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code>。要查看其工作原理首先需要找到用户site-packages目录的位置。启动Python并运行此代码:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">site</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">site</span><span class="o">.</span><span class="n">getusersitepackages</span><span class="p">()</span>
<span class="go">&#39;/home/user/.local/lib/python3.5/site-packages&#39;</span>
</pre></div>
</div>
<p>现在,您可以在该目录中创建一个名为 <code class="file docutils literal notranslate"><span class="pre">usercustomize.py</span></code> 的文件并将所需内容放入其中。它会影响Python的每次启动除非它以 <a class="reference internal" href="../using/cmdline.html#cmdoption-s"><code class="xref std std-option docutils literal notranslate"><span class="pre">-s</span></code></a> 选项启动,以禁用自动导入。</p>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code> 以相同的方式工作,但通常由计算机管理员在全局 site-packages 目录中创建,并在 <code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</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 class="rubric">备注</p>
<dl class="footnote brackets">
<dt class="label" id="id2"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>GNU Readline 包的问题可能会阻止这种情况。</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="#">16. 附录</a><ul>
<li><a class="reference internal" href="#interactive-mode">16.1. 交互模式</a><ul>
<li><a class="reference internal" href="#error-handling">16.1.1. 错误处理</a></li>
<li><a class="reference internal" href="#executable-python-scripts">16.1.2. 可执行的Python脚本</a></li>
<li><a class="reference internal" href="#the-interactive-startup-file">16.1.3. 交互式启动文件</a></li>
<li><a class="reference internal" href="#the-customization-modules">16.1.4. 定制模块</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="floatingpoint.html"
title="上一章"><span class="section-number">15. </span>浮点算术:争议和限制</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="../using/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/tutorial/appendix.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="../using/index.html" title="Python安装和使用"
>下一页</a> |</li>
<li class="right" >
<a href="floatingpoint.html" title="15. 浮点算术:争议和限制"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 教程</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>