📄 node8.html
字号:
<P>这样不会从局域语义表中导入模块名(如上所示, <code>fibo</code>没有定义)。<P>There is even a variant to import all names that a module defines:<P>这样可以导入所有除了以下划线(<code>_</code>)开头的命名。<P><div class="verbatim"><pre>
>>> from fibo import *
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377</pre></div><P>This imports all names except those beginning with an underscore
(<code>_</code>).<P>这样可以导入所有除了以下划线(<code>_</code>)开头的命名。<P><H2><A NAME="SECTION008110000000000000000"></A><A NAME="searchPath"></A><BR>6.1.1 模块搜索路径 The Module Search Path </H2><P><a id='l2h-24' xml:id='l2h-24'></a>
When a module named <tt class="module">spam</tt> is imported, the interpreter searches
for a file named <span class="file">spam.py</span> in the current directory,
and then in the list of directories specified by
the environment variable <a class="envvar" id='l2h-25' xml:id='l2h-25'>PYTHONPATH</a>. This has the same syntax as
the shell variable <a class="envvar" id='l2h-26' xml:id='l2h-26'>PATH</a>, that is, a list of
directory names. When <a class="envvar" id='l2h-27' xml:id='l2h-27'>PYTHONPATH</a> is not set, or when the file
is not found there, the search continues in an installation-dependent
default path; on <span class="Unix">Unix</span>, this is usually <span class="file">.:/usr/local/lib/python</span>.<P>导入一个叫 <tt class="module">spam</tt> 的模块时,解释器先在当前目录中搜索名为 <span class="file">spam.py</span> 的文件,然后在环境变量 <a class="envvar" id='l2h-28' xml:id='l2h-28'>PYTHONPATH</a> 表示的目录列表中搜索,然后是环境变量 <a class="envvar" id='l2h-29' xml:id='l2h-29'>PATH</a> 中的路径列表。如果 <a class="envvar" id='l2h-30' xml:id='l2h-30'>PYTHONPATH</a> 没有设置,或者文件没有找到,接下来搜索安装目录,在 <span class="Unix">Unix</span>中,通常是 <span class="file">.:/usr/local/lib/python</span>。<P>Actually, modules are searched in the list of directories given by the
variable <code>sys.path</code> which is initialized from the directory
containing the input script (or the current directory),
<a class="envvar" id='l2h-31' xml:id='l2h-31'>PYTHONPATH</a> and the installation-dependent default. This allows
Python programs that know what they're doing to modify or replace the
module search path. Note that because the directory containing the
script being run is on the search path, it is important that the
script not have the same name as a standard module, or Python will
attempt to load the script as a module when that module is imported.
This will generally be an error. See section <A HREF="#standardModules"><tex2html_cross_ref_visible_mark></A>,
``Standard Modules,'' for more information.<P>实际上,解释器由 <code>sys.path</code> 变量指定的路径目录搜索模块,该变量初始化时默认包含了输入脚本(或者当前目录),<a class="envvar" id='l2h-32' xml:id='l2h-32'>PYTHONPATH</a> 和安装目录。这样就允许Python程序(原文如此,programs;我猜想应该是“programer”,程序员--译者)了解如何修改或替换模块搜索目录。需要注意的是由于这些目录中包含有搜索路径中运行的脚本,所以这些脚本不应该和标准模块重名,否则在导入模块时Python会尝试把这些脚本当作模块来加载。这通常会引发一个错误。请参见6.2节“标准模块( <A HREF="#standardModules"><tex2html_cross_ref_visible_mark></A>)”以了解更多的信息。<P><H2><A NAME="SECTION008120000000000000000">6.1.2 “编译”Python文件 ``Compiled'' Python files</A></H2><P>As an important speed-up of the start-up time for short programs that
use a lot of standard modules, if a file called <span class="file">spam.pyc</span> exists
in the directory where <span class="file">spam.py</span> is found, this is assumed to
contain an already-``byte-compiled'' version of the module <tt class="module">spam</tt>.
The modification time of the version of <span class="file">spam.py</span> used to create
<span class="file">spam.pyc</span> is recorded in <span class="file">spam.pyc</span>, and the
<span class="file">.pyc</span> file is ignored if these don't match.<P>对于引用了大量标准模块的短程序,有一个提高启动速度的重要方法,如果在 <span class="file">spam.py</span> 所在的目录下存在一个名为 <span class="file">spam.pyc</span> 的文件,它会被视为 <tt class="module">spam</tt> 模块的预“编译”(``byte-compiled'' ,二进制编译)版本。用于创建 <span class="file">spam.pyc</span> 的这一版 <span class="file">spam.py</span> 的修改时间记录在 <span class="file">spam.pyc</span> 文件中,如果两者不匹配,<span class="file">.pyc</span> 文件就被忽略。<P>Normally, you don't need to do anything to create the
<span class="file">spam.pyc</span> file. Whenever <span class="file">spam.py</span> is successfully
compiled, an attempt is made to write the compiled version to
<span class="file">spam.pyc</span>. It is not an error if this attempt fails; if for any
reason the file is not written completely, the resulting
<span class="file">spam.pyc</span> file will be recognized as invalid and thus ignored
later. The contents of the <span class="file">spam.pyc</span> file are platform
independent, so a Python module directory can be shared by machines of
different architectures.<P>通常你不需要为创建 <span class="file">spam.pyc</span> 文件做任何工作。一旦 <span class="file">spam.py</span> 成功编译,就会试图编译对应版本的 <span class="file">spam.pyc</span>。如果有任何原因导致写入不成功,返回的 <span class="file">spam.pyc</span> 文件就会视为无效,随后即被忽略。 <span class="file">spam.pyc</span> 文件的内容是平台独立的,所以Python模块目录可以在不同架构的机器之间共享。<P>Some tips for experts:<P>部分高级技巧:<P><UL><LI>When the Python interpreter is invoked with the <b class="programopt">-O</b> flag,
optimized code is generated and stored in <span class="file">.pyo</span> files. The
optimizer currently doesn't help much; it only removes
<tt class="keyword">assert</tt> statements. When <b class="programopt">-O</b> is used, <em>all</em>
bytecode is optimized; <code>.pyc</code> files are ignored and <code>.py</code>
files are compiled to optimized bytecode.<P>以 <b class="programopt">-O</b> 参数调用Python解释器时,会生成优化代码并保存在 <span class="file">.pyo</span> 文件中。现在的优化器没有太多帮助;它只是删除了断言(<tt class="keyword">assert</tt> )语句。使用 <b class="programopt">-O</b> 参参数,所有的代码都会被优化;<code>.pyc</code> 文件被忽略, <code>.py</code>文件被编译为优化代码。<P></LI><LI>Passing two <b class="programopt">-O</b> flags to the Python interpreter
(<b class="programopt">-OO</b>) will cause the bytecode compiler to perform
optimizations that could in some rare cases result in malfunctioning
programs. Currently only <code>__doc__</code> strings are removed from the
bytecode, resulting in more compact <span class="file">.pyo</span> files. Since some
programs may rely on having these available, you should only use this
option if you know what you're doing.<P>向Python解释器传递两个 <b class="programopt">-O</b> 参数(<b class="programopt">-OO</b>)会执行完全优化的二进制优化编译,这偶尔会生成错误的程序。现在的优化器,只是从二进制代码中删除了 <code>__doc__</code> 符串,生成更为紧凑的 <span class="file">.pyo</span> 文件。因为某些程序依赖于这些变量的可用性,你应该只在确定无误的场合使用这一选项。<P></LI><LI>A program doesn't run any faster when it is read from a <span class="file">.pyc</span> or
<span class="file">.pyo</span> file than when it is read from a <span class="file">.py</span> file; the only
thing that's faster about <span class="file">.pyc</span> or <span class="file">.pyo</span> files is the
speed with which they are loaded.<P>来自 <span class="file">.pyc</span> 文件或 <span class="file">.pyo</span> 文件中的程序不会比来自 <span class="file">.py</span> 文件的运行更快; <span class="file">.pyc</span> 或 <span class="file">.pyo</span> 文件只是在它们加载的时候更快一些。<P></LI><LI>When a script is run by giving its name on the command line, the
bytecode for the script is never written to a <span class="file">.pyc</span> or
<span class="file">.pyo</span> file. Thus, the startup time of a script may be reduced
by moving most of its code to a module and having a small bootstrap
script that imports that module. It is also possible to name a
<span class="file">.pyc</span> or <span class="file">.pyo</span> file directly on the command line.<P>通过脚本名在命令行运行脚本时,不会将为该脚本创建的二进制代码写入 <span class="file">.pyc</span> 或<span class="file">.pyo</span> 文件。当然,把脚本的主要代码移进一个模块里,然后用一个小的解构脚本导入这个模块,就可以提高脚本的启动速度。也可以直接在命令行中指定一个 <span class="file">.pyc</span> 或 <span class="file">.pyo</span> 文件。<P></LI><LI>It is possible to have a file called <span class="file">spam.pyc</span> (or
<span class="file">spam.pyo</span> when <b class="programopt">-O</b> is used) without a file
<span class="file">spam.py</span> for the same module. This can be used to distribute a
library of Python code in a form that is moderately hard to reverse
engineer.<P>对于同一个模块(这里指例程 <span class="file">spam.py</span> --译者),可以只有 <span class="file">spam.pyc</span> 文件(或者 <span class="file">spam.pyc</span> ,在使用 <b class="programopt">-O</b> 参数时)而没有 <span class="file">spam.py</span> 文件。这样可以打包发布比较难于逆向工程的Python代码库。<P></LI><LI>The module <a class="ulink" href="../lib/module-compileall.html" ><tt class="module">compileall</tt></a> <a id='l2h-33' xml:id='l2h-33'></a> can create <span class="file">.pyc</span> files (or
<span class="file">.pyo</span> files when <b class="programopt">-O</b> is used) for all modules in a
directory.<P><a class="ulink" href="../lib/module-compileall.html" ><tt class="module">compileall</tt></a> <a id='l2h-34' xml:id='l2h-34'></a> 模块 可以为指定目录中的所有模块创建 <span class="file">.pyc</span> 文件(或者使用 <span class="file">.pyo</span> 参数创建.pyo文件)。<P></LI></UL><P><H1><A NAME="SECTION008200000000000000000"></A><A NAME="standardModules"></A><BR>6.2 标准模块 Standard Modules </H1><P>Python comes with a library of standard modules, described in a separate
document, the <em class="citetitle"><a href="../lib/lib.html" title="Python Library Reference" >Python Library Reference</a></em>
(``Library Reference'' hereafter). Some modules are built into the
interpreter; these provide access to operations that are not part of
the core of the language but are nevertheless built in, either for
efficiency or to provide access to operating system primitives such as
system calls. The set of such modules is a configuration option which
also depends on the underlying platform For example,
the <tt class="module">amoeba</tt> module is only provided on systems that somehow
support Amoeba primitives. One particular module deserves some
attention: <a class="ulink" href="../lib/module-sys.html" ><tt class="module">sys</tt></a><a id='l2h-35' xml:id='l2h-35'></a>, which is built into every
Python interpreter. The variables <code>sys.ps1</code> and
<code>sys.ps2</code> define the strings used as primary and secondary
prompts:<P>Python带有一个标准模块库,并发布有独立的文档,名为 <em class="citetitle"><a href="../lib/lib.html" title="Python 库参考手册" >Python 库参考手册</a></em> (此后称其为“库参考手册”)。有一些模块内置于解释器之中,这些操作的访问接口不是语言内核的一部分,但是已经内置于解释器了。这既是为了提高效率,也是为了给系统调用等操作系统原生访问提供接口。这类模块集合是一个依赖于底层平台的配置选项。例如,<tt class="module">amoeba</tt> 模块只提供对 Amoeba
原生系统的支持。有一个具体的模块值得注意:<a class="ulink" href="../lib/module-sys.html" ><tt class="module">sys</tt></a><a id='l2h-36' xml:id='l2h-36'></a> ,这个模块内置于所有的Python解释器。变量 <code>sys.ps1</code> 和 <code>sys.ps2</code>定义了主提示符和副助提示符字符串:<P><div class="verbatim"><pre>
>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> sys.ps1 = 'C> '
C> print 'Yuck!'
Yuck!
C></pre></div><P>These two variables are only defined if the interpreter is in
interactive mode.<P>这两个变量只在解释器的交互模式下有意义。<P>The variable <code>sys.path</code> is a list of strings that determine the
interpreter's search path for modules. It is initialized to a default
path taken from the environment variable <a class="envvar" id='l2h-37' xml:id='l2h-37'>PYTHONPATH</a>, or from
a built-in default if <a class="envvar" id='l2h-38' xml:id='l2h-38'>PYTHONPATH</a> is not set. You can modify
it using standard list operations:<P>变量 <code>sys.path</code> 是解释器模块搜索路径的字符串列表。它由环境变量 <a class="envvar" id='l2h-39' xml:id='l2h-39'>PYTHONPATH</a> 初始化,如果没有设定 <a class="envvar" id='l2h-40' xml:id='l2h-40'>PYTHONPATH</a> ,就由内置的默认值初始化。你可以用标准的字符串操作修改它:<P><div class="verbatim"><pre>
>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')</pre></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -