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

351 lines
23 KiB
HTML
Raw Permalink 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>2. 使用 Python 的解释器 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="3. Python 速览" href="introduction.html" />
<link rel="prev" title="1. 课前甜点" href="appetite.html" />
<link rel="canonical" href="https://docs.python.org/3/tutorial/interpreter.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. 使用 Python 的解释器</a><ul>
<li><a class="reference internal" href="#invoking-the-interpreter">2.1. 唤出解释器</a><ul>
<li><a class="reference internal" href="#argument-passing">2.1.1. 传入参数</a></li>
<li><a class="reference internal" href="#interactive-mode">2.1.2. 交互模式</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-interpreter-and-its-environment">2.2. 解释器的运行环境</a><ul>
<li><a class="reference internal" href="#source-code-encoding">2.2.1. 源文件的字符编码</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="appetite.html"
title="上一章"><span class="section-number">1. </span>课前甜点</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="introduction.html"
title="下一章"><span class="section-number">3. </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/tutorial/interpreter.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/interpreter.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="introduction.html" title="3. Python 速览"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="appetite.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> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 教程</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="using-the-python-interpreter">
<span id="tut-using"></span><h1><span class="section-number">2. </span>使用 Python 的解释器<a class="headerlink" href="#using-the-python-interpreter" title="永久链接至标题"></a></h1>
<section id="invoking-the-interpreter">
<span id="tut-invoking"></span><h2><span class="section-number">2.1. </span>唤出解释器<a class="headerlink" href="#invoking-the-interpreter" title="永久链接至标题"></a></h2>
<p>Python 解释器在其被使用的机器上通常安装为 <code class="file docutils literal notranslate"><span class="pre">/usr/local/bin/python3.8</span></code>;将 <code class="file docutils literal notranslate"><span class="pre">/usr/local/bin</span></code> 加入你的 Unix 终端的搜索路径就可以通过键入以下命令来启动它:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>python3.8
</pre></div>
</div>
<p>这样,就可以在 shell 中运行 Python 了 <a class="footnote-reference brackets" href="#id2" id="id1">1</a> 。因为可以选择安装目录,解释器也有可能安装在别的位置;如果还不明白,就去问问身边的 Python 大神或系统管理员。(例如,常见备选路径还有 <code class="file docutils literal notranslate"><span class="pre">/usr/local/python</span></code>。)</p>
<p>在 Windows 机器上当你从 <a class="reference internal" href="../using/windows.html#windows-store"><span class="std std-ref">Microsoft Store</span></a> 安装 Python 之后,<code class="file docutils literal notranslate"><span class="pre">python3.8</span></code> 命令将可使用。 如果你安装了 <a class="reference internal" href="../using/windows.html#launcher"><span class="std std-ref">py.exe 启动器</span></a>,你将可以使用 <code class="file docutils literal notranslate"><span class="pre">py</span></code> 命令。 参阅 <a class="reference internal" href="../using/windows.html#setting-envvars"><span class="std std-ref">附录:设置环境变量</span></a> 了解其他启动 Python 的方式。</p>
<p>在主提示符中输入文件结束符Unix 里是 <kbd class="kbd docutils literal notranslate">Control-D</kbd>Windows 里是 <kbd class="kbd docutils literal notranslate">Control-Z</kbd>),就会退出解释器,退出状态码为 0。如果不能退出还可以输入这个命令<code class="docutils literal notranslate"><span class="pre">quit()</span></code></p>
<p>在支持 <a class="reference external" href="https://tiswww.case.edu/php/chet/readline/rltop.html">GNU Readline</a> 库的系统中,解释器的行编辑功能包括交互式编辑、历史替换、代码补全等。检测是否支持命令行编辑最快速的方式是,在首次出现 Python 提示符时,输入 <kbd class="kbd docutils literal notranslate">Control-P</kbd>。听到“哔”提示音,说明支持行编辑;请参阅附录 <a class="reference internal" href="interactive.html#tut-interacting"><span class="std std-ref">交互式编辑和编辑历史</span></a>,了解功能键。如果没有反应,或回显了 <code class="docutils literal notranslate"><span class="pre">^P</span></code>,则说明不支持行编辑;只能用退格键删除当前行的字符。</p>
<p>解释器的操作方式类似 Unix Shell用与 tty 设备关联的标准输入调用时,可以交互式地读取和执行命令;以文件名参数,或标准输入文件调用时,则读取并执行文件中的 <em>脚本</em></p>
<p>启动解释器的另一种方式是 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-c</span> <span class="pre">command</span> <span class="pre">[arg]</span> <span class="pre">...</span></code>,这与 shell 的 <a class="reference internal" href="../using/cmdline.html#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> 选项类似,其中,<em>command</em> 需换成要执行的语句。由于 Python 语句经常包含空格等被 shell 特殊对待的字符,一般情况下,建议用单引号标注整个 <em>command</em></p>
<p>Python 模块也可以当作脚本使用。输入:<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">module</span> <span class="pre">[arg]</span> <span class="pre">...</span></code>,会执行 <em>module</em> 的源文件,这跟在命令行把路径写全了一样。</p>
<p>在交互模式下运行脚本文件,只要在脚本名称参数前,加上选项 <a class="reference internal" href="../using/cmdline.html#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="../using/cmdline.html#using-on-general"><span class="std std-ref">命令行与环境</span></a></p>
<section id="argument-passing">
<span id="tut-argpassing"></span><h3><span class="section-number">2.1.1. </span>传入参数<a class="headerlink" href="#argument-passing" title="永久链接至标题"></a></h3>
<p>解释器读取命令行参数,把脚本名与其他参数转化为字符串列表存到 <code class="docutils literal notranslate"><span class="pre">sys</span></code> 模块的 <code class="docutils literal notranslate"><span class="pre">argv</span></code> 变量里。执行 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">sys</span></code>,可以导入这个模块,并访问该列表。该列表最少有一个元素;未给定输入参数时,<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> 是空字符串。给定脚本名是 <code class="docutils literal notranslate"><span class="pre">'-'</span></code> (标准输入)时,<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="../using/cmdline.html#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> <em>command</em> 时,<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code><code class="docutils literal notranslate"><span class="pre">'-c'</span></code>。如果使用选项 <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a> <em>module</em><code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> 就是包含目录的模块全名。解释器不处理 <a class="reference internal" href="../using/cmdline.html#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> <em>command</em><a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a> <em>module</em> 之后的选项,而是直接留在 <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code> 中由命令或模块来处理。</p>
</section>
<section id="interactive-mode">
<span id="tut-interactive"></span><h3><span class="section-number">2.1.2. </span>交互模式<a class="headerlink" href="#interactive-mode" title="永久链接至标题"></a></h3>
<p>在终端tty输入并执行指令时解释器在 <em>交互模式interactive mode</em> 中运行。在这种模式中,会显示 <em>主提示符</em>,提示输入下一条指令,主提示符通常用三个大于号(<code class="docutils literal notranslate"><span class="pre">&gt;&gt;&gt;</span></code>)表示;输入连续行时,显示 <em>次要提示符</em>,默认是三个点(<code class="docutils literal notranslate"><span class="pre">...</span></code>)。进入解释器时,首先显示欢迎信息、版本信息、版权声明,然后才是提示符:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python3.8
<span class="go">Python 3.8 (default, Sep 16 2015, 09:25:04)</span>
<span class="go">[GCC 4.8.2] on linux</span>
<span class="go">Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>输入多行架构的语句时,要用连续行。以 <a class="reference internal" href="../reference/compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a> 为例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">the_world_is_flat</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="n">the_world_is_flat</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Be careful not to fall off!&quot;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">Be careful not to fall off!</span>
</pre></div>
</div>
<p>交互模式的内容详见 <a class="reference internal" href="appendix.html#tut-interac"><span class="std std-ref">交互模式</span></a></p>
</section>
</section>
<section id="the-interpreter-and-its-environment">
<span id="tut-interp"></span><h2><span class="section-number">2.2. </span>解释器的运行环境<a class="headerlink" href="#the-interpreter-and-its-environment" title="永久链接至标题"></a></h2>
<section id="source-code-encoding">
<span id="tut-source-encoding"></span><h3><span class="section-number">2.2.1. </span>源文件的字符编码<a class="headerlink" href="#source-code-encoding" title="永久链接至标题"></a></h3>
<p>默认情况下Python 源码文件的编码是 UTF-8。这种编码支持世界上大多数语言的字符可以用于字符串字面值、变量、函数名及注释 —— 尽管标准库只用常规的 ASCII 字符作为变量名或函数名,可移植代码都应遵守此约定。要正确显示这些字符,编辑器必须能识别 UTF-8 编码,而且必须使用支持文件中所有字符的字体。</p>
<p>如果不使用默认编码,则要声明文件的编码,文件的 <em>第一</em> 行要写成特殊注释。句法如下:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># -*- coding: encoding -*-</span>
</pre></div>
</div>
<p>其中,<em>encoding</em> 可以是 Python 支持的任意一种 <a class="reference internal" href="../library/codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a></p>
<p>比如,声明使用 Windows-1252 编码,源码文件要写成:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># -*- coding: cp1252 -*-</span>
</pre></div>
</div>
<p><em>第一行</em> 的规则也有一种例外情况,源码以 <a class="reference internal" href="appendix.html#tut-scripts"><span class="std std-ref">UNIX &quot;shebang&quot;</span></a> 开头。此时,编码声明要写在文件的第二行。例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3</span>
<span class="c1"># -*- coding: cp1252 -*-</span>
</pre></div>
</div>
<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>Unix 系统中,为了不与同时安装的 Python 2.x 冲突Python 3.x 解释器默认安装的执行文件名不是 <code class="docutils literal notranslate"><span class="pre">python</span></code></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="#">2. 使用 Python 的解释器</a><ul>
<li><a class="reference internal" href="#invoking-the-interpreter">2.1. 唤出解释器</a><ul>
<li><a class="reference internal" href="#argument-passing">2.1.1. 传入参数</a></li>
<li><a class="reference internal" href="#interactive-mode">2.1.2. 交互模式</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-interpreter-and-its-environment">2.2. 解释器的运行环境</a><ul>
<li><a class="reference internal" href="#source-code-encoding">2.2.1. 源文件的字符编码</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="appetite.html"
title="上一章"><span class="section-number">1. </span>课前甜点</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="introduction.html"
title="下一章"><span class="section-number">3. </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/tutorial/interpreter.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="introduction.html" title="3. Python 速览"
>下一页</a> |</li>
<li class="right" >
<a href="appetite.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> &#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>