-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.html
More file actions
504 lines (456 loc) · 42.5 KB
/
import.html
File metadata and controls
504 lines (456 loc) · 42.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>匯入模組 — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
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="Copyright" href="../copyright.html" />
<link rel="next" title="Data marshalling support" href="marshal.html" />
<link rel="prev" title="作業系統工具" href="sys.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/c-api/import.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="marshal.html" title="Data marshalling support"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="sys.html" title="作業系統工具"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python / C API 參考手冊</a> »</li>
<li class="nav-item nav-item-2"><a href="utilities.html" accesskey="U">工具</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="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="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">This is a simplified interface to <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> below,
leaving the <em>globals</em> and <em>locals</em> arguments set to <em>NULL</em> and <em>level</em> set
to 0. When the <em>name</em>
argument contains a dot (when it specifies a submodule of a package), the
<em>fromlist</em> argument is set to the list <code class="docutils literal notranslate"><span class="pre">['*']</span></code> so that the return value is the
named module rather than the top-level package containing it as would otherwise
be the case. (Unfortunately, this has an additional side effect when <em>name</em> in
fact specifies a subpackage instead of a submodule: the submodules specified in
the package’s <code class="docutils literal notranslate"><span class="pre">__all__</span></code> variable are loaded.) Return a new reference to the
imported module, or <em>NULL</em> with an exception set on failure. A failing
import of a module doesn’t leave the module in <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>This function always uses absolute imports.</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="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><p>This function is a deprecated alias of <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">3.3 版更變: </span>This function used to fail immediately when the import lock was held
by another thread. In Python 3.3 though, the locking scheme switched
to per-module locks for most purposes, so this function’s special
behaviour isn’t needed anymore.</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="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">Import a module. This is best described by referring to the built-in Python
function <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>The return value is a new reference to the imported module or top-level
package, or <em>NULL</em> with an exception set on failure. Like for
<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>, the return value when a submodule of a package was
requested is normally the top-level package, unless a non-empty <em>fromlist</em>
was given.</p>
<p>Failing imports remove incomplete module objects, like with
<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="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><p>Import a module. This is best described by referring to the built-in Python
function <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>, as the standard <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> function calls
this function directly.</p>
<p>The return value is a new reference to the imported module or top-level package,
or <em>NULL</em> with an exception set on failure. Like for <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>,
the return value when a submodule of a package was requested is normally the
top-level package, unless a non-empty <em>fromlist</em> was given.</p>
<div class="versionadded">
<p><span class="versionmodified">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="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>Similar to <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>, but the name is a
UTF-8 encoded string instead of a Unicode object.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.3 版更變: </span>Negative values for <em>level</em> are no longer accepted.</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="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>This is a higher-level interface that calls the current 「import hook
function」 (with an explicit <em>level</em> of 0, meaning absolute import). It
invokes the <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> function from the <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> of the
current globals. This means that the import is done using whatever import
hooks are installed in the current environment.</p>
<p>This function always uses absolute imports.</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="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>Reload a module. Return a new reference to the reloaded module, or <em>NULL</em> with
an exception set on failure (the module still exists in this case).</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="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><p>Return the module object corresponding to a module name. The <em>name</em> argument
may be of the form <code class="docutils literal notranslate"><span class="pre">package.module</span></code>. First check the modules dictionary if
there’s one there, and if not, create a new one and insert it in the modules
dictionary. Return <em>NULL</em> with an exception set on failure.</p>
<div class="admonition note">
<p class="first admonition-title">備註</p>
<p class="last">This function does not load or import the module; if the module wasn’t already
loaded, you will get an empty module object. Use <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>
or one of its variants to import a module. Package structures implied by a
dotted name for <em>name</em> are not created if not already present.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">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="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>Similar to <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>, but the name is a UTF-8
encoded string instead of a 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="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">Given a module name (possibly of the form <code class="docutils literal notranslate"><span class="pre">package.module</span></code>) and a code object
read from a Python bytecode file or obtained from the built-in function
<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>, load the module. Return a new reference to the module object,
or <em>NULL</em> with an exception set if an error occurred. <em>name</em>
is removed from <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> in error cases, even if <em>name</em> was already
in <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> on entry to <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>. Leaving
incompletely initialized modules in <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> is dangerous, as imports of
such modules have no way to know that the module object is an unknown (and
probably damaged with respect to the module author’s intents) state.</p>
<p>The module’s <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> and <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> will be set, if
not set already, with the appropriate values. The spec’s loader will
be set to the module’s <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> (if set) and to an instance of
<code class="xref py py-class docutils literal notranslate"><span class="pre">SourceFileLoader</span></code> otherwise.</p>
<p>The module’s <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> attribute will be set to the code object’s
<code class="xref c c-member docutils literal notranslate"><span class="pre">co_filename</span></code>. If applicable, <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> will also
be set.</p>
<p>This function will reload the module if it was already imported. See
<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> for the intended way to reload a module.</p>
<p>If <em>name</em> points to a dotted name of the form <code class="docutils literal notranslate"><span class="pre">package.module</span></code>, any package
structures not already created will still not be created.</p>
<p>See also <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> and
<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="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>Like <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>, but the <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> attribute of
the module object is set to <em>pathname</em> if it is non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
<p>See also <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="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><p>Like <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>, but the <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>
attribute of the module object is set to <em>cpathname</em> if it is
non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>. Of the three functions, this is the preferred one to use.</p>
<div class="versionadded">
<p><span class="versionmodified">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="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><p>Like <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>, but <em>name</em>, <em>pathname</em> and
<em>cpathname</em> are UTF-8 encoded strings. Attempts are also made to figure out
what the value for <em>pathname</em> should be from <em>cpathname</em> if the former is
set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified">3.2 版新加入.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.3 版更變: </span>Uses <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> in calculating the source path if
only the bytecode path is provided.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_GetMagicNumber">
long <code class="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>Return the magic number for Python bytecode files (a.k.a. <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> file).
The magic number should be present in the first four bytes of the bytecode
file, in little-endian byte order. Returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.3 版更變: </span>Return value of <code class="docutils literal notranslate"><span class="pre">-1</span></code> upon failure.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_GetMagicTag">
const char * <code class="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>Return the magic tag string for <span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> format Python bytecode file
names. Keep in mind that the value at <code class="docutils literal notranslate"><span class="pre">sys.implementation.cache_tag</span></code> is
authoritative and should be used instead of this function.</p>
<div class="versionadded">
<p><span class="versionmodified">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="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>Return the dictionary used for the module administration (a.k.a.
<code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>). Note that this is a per-interpreter variable.</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="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>Return the already imported module with the given name. If the
module has not been imported yet then returns NULL but does not set
an error. Returns NULL and sets an error if the lookup failed.</p>
<div class="versionadded">
<p><span class="versionmodified">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="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><p>Return a finder object for 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>/<code class="xref py py-attr docutils literal notranslate"><span class="pre">pkg.__path__</span></code> item
<em>path</em>, possibly by fetching it from the <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>
dict. If it wasn’t yet cached, traverse <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> until a hook
is found that can handle the path item. Return <code class="docutils literal notranslate"><span class="pre">None</span></code> if no hook could;
this tells our caller that the <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a> could not find a
finder for this path item. Cache the result in <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>.
Return a new reference to the finder object.</p>
</dd></dl>
<dl class="function">
<dt id="c._PyImport_Init">
void <code class="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="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="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="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><p>Load a frozen module named <em>name</em>. Return <code class="docutils literal notranslate"><span class="pre">1</span></code> for success, <code class="docutils literal notranslate"><span class="pre">0</span></code> if the
module is not found, and <code class="docutils literal notranslate"><span class="pre">-1</span></code> with an exception set if the initialization
failed. To access the imported module on a successful load, use
<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>. (Note the misnomer — this function would
reload the module if it was already imported.)</p>
<div class="versionadded">
<p><span class="versionmodified">3.3 版新加入.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.4 版更變: </span>The <code class="docutils literal notranslate"><span class="pre">__file__</span></code> attribute is no longer set on the module.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_ImportFrozenModule">
int <code class="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>Similar to <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>, but the name is a
UTF-8 encoded string instead of a Unicode object.</p>
</dd></dl>
<dl class="type">
<dt id="c._frozen">
struct <code class="descname">_frozen</code><a class="headerlink" href="#c._frozen" title="本定義的永久連結">¶</a></dt>
<dd><p id="index-4">This is the structure type definition for frozen module descriptors, as
generated by the <strong class="program">freeze</strong> utility (see <code class="file docutils literal notranslate"><span class="pre">Tools/freeze/</span></code> in the
Python source distribution). Its definition, found in <code class="file docutils literal notranslate"><span class="pre">Include/import.h</span></code>,
is:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">_frozen</span> <span class="p">{</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">code</span><span class="p">;</span>
<span class="kt">int</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="descname">PyImport_FrozenModules</code><a class="headerlink" href="#c.PyImport_FrozenModules" title="本定義的永久連結">¶</a></dt>
<dd><p>This pointer is initialized to point to an array of <code class="xref c c-type docutils literal notranslate"><span class="pre">struct</span> <span class="pre">_frozen</span></code>
records, terminated by one whose members are all <em>NULL</em> or zero. When a frozen
module is imported, it is searched in this table. Third-party code could play
tricks with this to provide a dynamically created collection of frozen modules.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyImport_AppendInittab">
int <code class="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>Add a single module to the existing table of built-in modules. This is a
convenience wrapper around <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>, returning <code class="docutils literal notranslate"><span class="pre">-1</span></code> if
the table could not be extended. The new module can be imported by the name
<em>name</em>, and uses the function <em>initfunc</em> as the initialization function called
on the first attempted import. 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>
<dl class="type">
<dt id="c._inittab">
struct <code class="descname">_inittab</code><a class="headerlink" href="#c._inittab" title="本定義的永久連結">¶</a></dt>
<dd><p>Structure describing a single entry in the list of built-in modules. Each of
these structures gives the name and initialization function for a module built
into the interpreter. The name is an ASCII encoded string. Programs which
embed Python may use an array of these structures in conjunction with
<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> to provide additional built-in modules.
The structure is defined in <code class="file docutils literal notranslate"><span class="pre">Include/import.h</span></code> as:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">_inittab</span> <span class="p">{</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">;</span> <span class="cm">/* ASCII encoded string */</span>
<span class="n">PyObject</span><span class="o">*</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="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 <em>NULL</em> 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>
</div>
</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="下一章">Data marshalling support</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.7/Doc/c-api/import.rst"
rel="nofollow">Show Source
</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="General Index"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="marshal.html" title="Data marshalling support"
>下一頁</a> |</li>
<li class="right" >
<a href="sys.html" title="作業系統工具"
>上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python / C API 參考手冊</a> »</li>
<li class="nav-item nav-item-2"><a href="utilities.html" >工具</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on 8月 22, 2018.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.7.
</div>
</body>
</html>