Files
hustoj/web/python-docs/c-api/import.html
2025-03-09 17:10:56 +08:00

491 lines
47 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>导入模块 &#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="数据 marshal 操作支持" href="marshal.html" />
<link rel="prev" title="操作系统实用工具" href="sys.html" />
<link rel="canonical" href="https://docs.python.org/3/c-api/import.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="sys.html"
title="上一章">操作系统实用工具</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="marshal.html"
title="下一章">数据 marshal 操作支持</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/c-api/import.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/c-api/import.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="marshal.html" title="数据 marshal 操作支持"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="sys.html" title="操作系统实用工具"
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" >Python/C API 参考手册</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="utilities.html" accesskey="U">工具</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="importing-modules">
<span id="importing"></span><h1>导入模块<a class="headerlink" href="#importing-modules" title="永久链接至标题"></a></h1>
<dl class="function">
<dt id="c.PyImport_ImportModule">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ImportModule</code><span class="sig-paren">(</span>const char<em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModule" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p id="index-0">这是下面 <a class="reference internal" href="#c.PyImport_ImportModuleEx" title="PyImport_ImportModuleEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleEx()</span></code></a> 的简化版接口,将 <em>globals</em><em>locals</em> 参数设为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并将 <em>level</em> 设为 0。 当 <em>name</em> 参数包含一个点号(即指定了一个包的子模块)时,<em>fromlist</em> 参数会被设为列表 <code class="docutils literal notranslate"><span class="pre">['*']</span></code> 这样返回值将为所指定的模块而不像在其他情况下那样为包含模块的最高层级包。 (不幸的是,这在 <em>name</em> 实际上是指定一个子包而非子模块时将有一个额外的副作用:在包的 <code class="docutils literal notranslate"><span class="pre">__all__</span></code> 变量中指定的子模块会被加载。) 返回一个对所导入模块的新引用,或是在导入失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并设置一个异常。 模块导入失败同模块不会留 在 <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> 中。</p>
<p>该函数总是使用绝对路径导入。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ImportModuleNoBlock">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ImportModuleNoBlock</code><span class="sig-paren">(</span>const char<em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModuleNoBlock" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>该函数是 <a class="reference internal" href="#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a> 的一个被遗弃的别名。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>在导入锁被另一线程掌控时此函数会立即失败。 但是从 Python 3.3 起,锁方案在大多数情况下都已切换为针对每个模块加锁,所以此函数的特殊行为已无必要。</p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ImportModuleEx">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ImportModuleEx</code><span class="sig-paren">(</span>const char<em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *globals</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *locals</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *fromlist</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModuleEx" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p id="index-1">导入一个模块。 请参阅内置 Python 函数 <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> 获取完善的相关描述。</p>
<p>返回值是一个对所导入模块或最高层级包的新引用,或是在导入失败时则为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并设置一个异常。 与 <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> 类似,当请求一个包的子模块时返回值通常为该最高层级包,除非给出了一个非空的 <em>fromlist</em></p>
<p>导入失败将移动不完整的模块对象,就像 <a class="reference internal" href="#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a> 那样。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ImportModuleLevelObject">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ImportModuleLevelObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *globals</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *locals</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *fromlist</em>, int<em> level</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModuleLevelObject" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>导入一个模块。 关于此函数的最佳说明请参考内置 Python 函数 <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a>,因为标准 <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> 函数会直接调用此函数。</p>
<p>返回值是一个对所导入模块或最高层级包的新引用,或是在导入失败时则为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并设置一个异常。 与 <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> 类似,当请求一个包的子模块时返回值通常为该最高层级包,除非给出了一个非空的 <em>fromlist</em></p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ImportModuleLevel">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ImportModuleLevel</code><span class="sig-paren">(</span>const char<em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *globals</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *locals</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *fromlist</em>, int<em> level</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModuleLevel" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>类似于 <a class="reference internal" href="#c.PyImport_ImportModuleLevelObject" title="PyImport_ImportModuleLevelObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleLevelObject()</span></code></a>,但其名称为 UTF-8 编码的字符串而不是 Unicode 对象。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>不再接受 <em>level</em> 为负数值。</p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_Import">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_Import</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_Import" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>这是一个调用了当前“导入钩子函数”的更高层级接口(显式指定 <em>level</em> 为 0表示绝对导入。 它将发起调用当前全局作用域下 <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> 中的 <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> 函数。 这意味着将使用当前环境下安装的任何导入钩子来完成导入。</p>
<p>该函数总是使用绝对路径导入。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ReloadModule">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ReloadModule</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *m</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ReloadModule" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>重载一个模块。 返回一个指向被重载模块的新引用,或者在失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并设置一个异常(在此情况下模块仍然会存在)。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_AddModuleObject">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_AddModuleObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_AddModuleObject" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>返回对应于某个模块名称的模块对象。 <em>name</em> 参数的形式可以为 <code class="docutils literal notranslate"><span class="pre">package.module</span></code>。 如果存在 modules 字典则首先检查该字典,如果找不到,则创建一个新模块并将其插入到 modules 字典。 在失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并设置一个异常。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>此函数不会加载或导入指定模块;如果模块还未被加载,你将得到一个空的模块对象。 请使用 <a class="reference internal" href="#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a> 或它的某个变体形式来导入模块。 <em>name</em> 使用带点号名称的包结构如果尚不存在则不会被创建。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_AddModule">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_AddModule</code><span class="sig-paren">(</span>const char<em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_AddModule" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>类似于 <a class="reference internal" href="#c.PyImport_AddModuleObject" title="PyImport_AddModuleObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_AddModuleObject()</span></code></a>,但其名称为 UTF-8 编码的字符串而不是 Unicode 对象。object.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ExecCodeModule">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ExecCodeModule</code><span class="sig-paren">(</span>const char<em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *co</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExecCodeModule" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p id="index-2">给定一个模块名称(可能为 <code class="docutils literal notranslate"><span class="pre">package.module</span></code> 形式)和一个从 Pyhon 字节码文件读取或从内置函数 <a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 获取的代码对象,加载该模块。 返回对该模块对象的新引用,或者如果发生错误则返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并设置一个异常。 在发生错误的情况下 <em>name</em> 会从 <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.modules</span></code></a> 中被移除,即使 <em>name</em> 在进入 <a class="reference internal" href="#c.PyImport_ExecCodeModule" title="PyImport_ExecCodeModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModule()</span></code></a> 时已存在于 <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.modules</span></code></a> 中。 在 <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.modules</span></code></a> 中保留未完全初始化的模块是危险的,因为导入这样的模块没有办法知道模块对象是否处于一种未知的(对于模块作业的意图来说可能是已损坏的)状态。</p>
<p>模块的 <a class="reference internal" href="../reference/import.html#__spec__" title="__spec__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__spec__</span></code></a><a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> 如果尚未设置的话,将被设置为适当的值。 相应 spec 的加载器(如果已设置)将被设为模块的 <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> 而在其他情况下设为 <code class="xref py py-class docutils literal notranslate"><span class="pre">SourceFileLoader</span></code> 的实例。</p>
<p>模块的 <a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> 属性将被设为代码对象的 <code class="xref c c-member docutils literal notranslate"><span class="pre">co_filename</span></code>。 如果适用,<a class="reference internal" href="../reference/import.html#__cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a> 也将被设置。</p>
<p>如果模块已被导入则此函数将重载它。 请参阅 <a class="reference internal" href="#c.PyImport_ReloadModule" title="PyImport_ReloadModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ReloadModule()</span></code></a> 了解重载模块的预定方式。</p>
<p>如果 <em>name</em> 指向一个形式为 <code class="docutils literal notranslate"><span class="pre">package.module</span></code> 的带点号的名称,则任何尚未创建的包结构仍然不会被创建。</p>
<p>另请参阅 <a class="reference internal" href="#c.PyImport_ExecCodeModuleEx" title="PyImport_ExecCodeModuleEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleEx()</span></code></a><a class="reference internal" href="#c.PyImport_ExecCodeModuleWithPathnames" title="PyImport_ExecCodeModuleWithPathnames"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleWithPathnames()</span></code></a></p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ExecCodeModuleEx">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ExecCodeModuleEx</code><span class="sig-paren">(</span>const char<em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *co</em>, const char<em> *pathname</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExecCodeModuleEx" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>类似于 <a class="reference internal" href="#c.PyImport_ExecCodeModule" title="PyImport_ExecCodeModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModule()</span></code></a>,但如果 <em>pathname</em> 不为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 则会被设为模块对象的 <a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> 属性的值。</p>
<p>参见 <a class="reference internal" href="#c.PyImport_ExecCodeModuleWithPathnames" title="PyImport_ExecCodeModuleWithPathnames"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleWithPathnames()</span></code></a></p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ExecCodeModuleObject">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ExecCodeModuleObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *co</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *pathname</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *cpathname</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExecCodeModuleObject" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>类似于 <a class="reference internal" href="#c.PyImport_ExecCodeModuleEx" title="PyImport_ExecCodeModuleEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleEx()</span></code></a>,但如果 <em>cpathname</em> 不为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 则会被设为模块对象的 <a class="reference internal" href="../reference/import.html#__cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a> 值。 在三个函数中,这是推荐使用的一个。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ExecCodeModuleWithPathnames">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_ExecCodeModuleWithPathnames</code><span class="sig-paren">(</span>const char<em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *co</em>, const char<em> *pathname</em>, const char<em> *cpathname</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExecCodeModuleWithPathnames" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>类似于 <a class="reference internal" href="#c.PyImport_ExecCodeModuleObject" title="PyImport_ExecCodeModuleObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleObject()</span></code></a>,但 <em>name</em>, <em>pathname</em><em>cpathname</em> 为 UTF-8 编码的字符串。如果 <em>pathname</em> 也被设为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 则还会尝试根据 <em>cpathname</em> 推断出前者的值。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>如果只提供了字节码路径则会使用 <a class="reference internal" href="../library/imp.html#imp.source_from_cache" title="imp.source_from_cache"><code class="xref py py-func docutils literal notranslate"><span class="pre">imp.source_from_cache()</span></code></a> 来计算源路径。</p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_GetMagicNumber">
long <code class="sig-name descname">PyImport_GetMagicNumber</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetMagicNumber" title="永久链接至目标"></a></dt>
<dd><p>返回 Python 字节码文件(即 <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> 文件)的魔数。 此魔数应当存在于字节码文件的开头四个字节中,按照小端字节序。 出错时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code></p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>失败时返回值 <code class="docutils literal notranslate"><span class="pre">-1</span></code></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_GetMagicTag">
const char * <code class="sig-name descname">PyImport_GetMagicTag</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetMagicTag" title="永久链接至目标"></a></dt>
<dd><p>针对 <span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> 格式的 Python 字节码文件名返回魔术标签字符串。 请记住在 <code class="docutils literal notranslate"><span class="pre">sys.implementation.cache_tag</span></code> 上的值是应当被用来代替此函数的更权威的值。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_GetModuleDict">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_GetModuleDict</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetModuleDict" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>返回用于模块管理的字典 (即 <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>)。 请注意这是针对每个解释器的变量。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_GetModule">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_GetModule</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetModule" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回给定名称的已导入模块。 如果模块尚未导入则返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 但不会设置错误。 如果查找失败则返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并设置错误。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_GetImporter">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyImport_GetImporter</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *path</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetImporter" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><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>/<code class="xref py py-attr docutils literal notranslate"><span class="pre">pkg.__path__</span></code> 中条目 <em>path</em> 的查找器对象,可能会通过 <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> 字典来获取。 如果它尚未被缓存,则会遍历 <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> 直至找到一个能处理该 path 条目的钩子。 如果没有可用的钩子则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>;这将告知调用方 <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a> 无法为该 path 条目找到查找器。 结果将缓存到 <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>。 返回一个指向查找器对象的新引用。</p>
</dd></dl>
<dl class="function">
<dt id="c._PyImport_Init">
void <code class="sig-name descname">_PyImport_Init</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c._PyImport_Init" title="永久链接至目标"></a></dt>
<dd><p>Initialize the import mechanism. For internal use only.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_Cleanup">
void <code class="sig-name descname">PyImport_Cleanup</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_Cleanup" title="永久链接至目标"></a></dt>
<dd><p>Empty the module table. For internal use only.</p>
</dd></dl>
<dl class="function">
<dt id="c._PyImport_Fini">
void <code class="sig-name descname">_PyImport_Fini</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c._PyImport_Fini" title="永久链接至目标"></a></dt>
<dd><p>Finalize the import mechanism. For internal use only.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ImportFrozenModuleObject">
int <code class="sig-name descname">PyImport_ImportFrozenModuleObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportFrozenModuleObject" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>加载名称为 <em>name</em> 的已冻结模块。 成功时返回 <code class="docutils literal notranslate"><span class="pre">1</span></code>,如果未找到模块则返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>,如果初始化失败则返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 并设置一个异常。 要在加载成功后访问被导入的模块,请使用 <a class="reference internal" href="#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a>。 (请注意此名称有误导性 --- 如果模块已被导入此函数将重载它。)</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span><code class="docutils literal notranslate"><span class="pre">__file__</span></code> 属性将不再在模块上设置。</p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ImportFrozenModule">
int <code class="sig-name descname">PyImport_ImportFrozenModule</code><span class="sig-paren">(</span>const char<em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportFrozenModule" title="永久链接至目标"></a></dt>
<dd><p>类似于 <a class="reference internal" href="#c.PyImport_ImportFrozenModuleObject" title="PyImport_ImportFrozenModuleObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportFrozenModuleObject()</span></code></a>,但其名称为 UTF-8 编码的字符串而不是 Unicode 对象。</p>
</dd></dl>
<dl class="type">
<dt id="c._frozen">
struct <code class="sig-name descname">_frozen</code><a class="headerlink" href="#c._frozen" title="永久链接至目标"></a></dt>
<dd><p id="index-4">这是针对已冻结模块描述器的结构类型定义,与由 <strong class="program">freeze</strong> 工具所生成的一致 (请参看 Python 源代码发行版中的 <code class="file docutils literal notranslate"><span class="pre">Tools/freeze/</span></code>)。 其定义可在 <code class="file docutils literal notranslate"><span class="pre">Include/import.h</span></code> 中找到:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">_frozen</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">name</span><span class="p">;</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">code</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
</dd></dl>
<dl class="var">
<dt id="c.PyImport_FrozenModules">
const struct <a class="reference internal" href="#c._frozen" title="_frozen">_frozen</a>* <code class="sig-name descname">PyImport_FrozenModules</code><a class="headerlink" href="#c.PyImport_FrozenModules" title="永久链接至目标"></a></dt>
<dd><p>该指针被初始化为指向 <code class="xref c c-type docutils literal notranslate"><span class="pre">struct</span> <span class="pre">_frozen</span></code> 数组,以 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 或者 0 作为结束标记。当一个冻结模块被导入,首先要在这个表中搜索。第三方库可以以此来提供动态创建的冻结模块集合。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_AppendInittab">
int <code class="sig-name descname">PyImport_AppendInittab</code><span class="sig-paren">(</span>const char<em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* (<em>*initfunc</em>)(void)<span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_AppendInittab" title="永久链接至目标"></a></dt>
<dd><p>向现有的内置模块表添加一个模块。 这是对 <a class="reference internal" href="#c.PyImport_ExtendInittab" title="PyImport_ExtendInittab"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code></a> 的便捷包装,如果无法扩展表则返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。 新的模块可使用名称 <em>name</em> 来导入,并使用函数 <em>initfunc</em> 作为在第一次尝试导入时调用的初始化函数。 此函数应当在 <a class="reference internal" href="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> 之前调用。</p>
</dd></dl>
<dl class="type">
<dt id="c._inittab">
struct <code class="sig-name descname">_inittab</code><a class="headerlink" href="#c._inittab" title="永久链接至目标"></a></dt>
<dd><p>描述内置模块列表中的一个条目的结构体。 每个结构体都给出了内置在解释器中的某个模块的名称和初始化函数。 名称是一个 ASCII 编码的字符串。 嵌入了 Python 的程序可以使用该结构体的数组来与 <a class="reference internal" href="#c.PyImport_ExtendInittab" title="PyImport_ExtendInittab"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code></a> 相结合以提供额外的内置模块。 该结构体在 <code class="file docutils literal notranslate"><span class="pre">Include/import.h</span></code> 中被定义为:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">_inittab</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">name</span><span class="p">;</span><span class="w"> </span><span class="cm">/* ASCII encoded string */</span>
<span class="w"> </span><span class="n">PyObject</span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">initfunc</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span>
<span class="p">};</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ExtendInittab">
int <code class="sig-name descname">PyImport_ExtendInittab</code><span class="sig-paren">(</span>struct <a class="reference internal" href="#c._inittab" title="_inittab">_inittab</a><em> *newtab</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExtendInittab" title="永久链接至目标"></a></dt>
<dd><p>Add a collection of modules to the table of built-in modules. The <em>newtab</em>
array must end with a sentinel entry which contains <code class="docutils literal notranslate"><span class="pre">NULL</span></code> for the <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code>
field; failure to provide the sentinel value can result in a memory fault.
Returns <code class="docutils literal notranslate"><span class="pre">0</span></code> on success or <code class="docutils literal notranslate"><span class="pre">-1</span></code> if insufficient memory could be allocated to
extend the internal table. In the event of failure, no modules are added to the
internal table. This should be called before <a class="reference internal" href="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>.</p>
</dd></dl>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>上一个主题</h4>
<p class="topless"><a href="sys.html"
title="上一章">操作系统实用工具</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="marshal.html"
title="下一章">数据 marshal 操作支持</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/c-api/import.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="marshal.html" title="数据 marshal 操作支持"
>下一页</a> |</li>
<li class="right" >
<a href="sys.html" title="操作系统实用工具"
>上一页</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/C API 参考手册</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="utilities.html" >工具</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>