~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.2-docs-html/library/importlib.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>31.5. importlib – The implementation of import &mdash; Python 3.5.2 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '3.5.2',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
23
    <script type="text/javascript" src="../_static/jquery.js"></script>
 
24
    <script type="text/javascript" src="../_static/underscore.js"></script>
 
25
    <script type="text/javascript" src="../_static/doctools.js"></script>
 
26
    <script type="text/javascript" src="../_static/sidebar.js"></script>
 
27
    <link rel="search" type="application/opensearchdescription+xml"
 
28
          title="Search within Python 3.5.2 documentation"
 
29
          href="../_static/opensearch.xml"/>
 
30
    <link rel="author" title="About these documents" href="../about.html" />
 
31
    <link rel="copyright" title="Copyright" href="../copyright.html" />
 
32
    <link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
 
33
    <link rel="up" title="31. Importing Modules" href="modules.html" />
 
34
    <link rel="next" title="32. Python Language Services" href="language.html" />
 
35
    <link rel="prev" title="31.4. runpy — Locating and executing Python modules" href="runpy.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    
 
38
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
39
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
40
    
 
41
    
 
42
 
 
43
 
 
44
  </head>
 
45
  <body role="document">  
 
46
    <div class="related" role="navigation" aria-label="related navigation">
 
47
      <h3>Navigation</h3>
 
48
      <ul>
 
49
        <li class="right" style="margin-right: 10px">
 
50
          <a href="../genindex.html" title="General Index"
 
51
             accesskey="I">index</a></li>
 
52
        <li class="right" >
 
53
          <a href="../py-modindex.html" title="Python Module Index"
 
54
             >modules</a> |</li>
 
55
        <li class="right" >
 
56
          <a href="language.html" title="32. Python Language Services"
 
57
             accesskey="N">next</a> |</li>
 
58
        <li class="right" >
 
59
          <a href="runpy.html" title="31.4. runpy — Locating and executing Python modules"
 
60
             accesskey="P">previous</a> |</li>
 
61
        <li><img src="../_static/py.png" alt=""
 
62
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
63
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
64
        <li>
 
65
          <span class="version_switcher_placeholder">3.5.2</span>
 
66
          <a href="../index.html">Documentation </a> &raquo;
 
67
        </li>
 
68
 
 
69
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
70
          <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">31. Importing Modules</a> &raquo;</li>
 
71
    <li class="right">
 
72
        
 
73
 
 
74
    <div class="inline-search" style="display: none" role="search">
 
75
        <form class="inline-search" action="../search.html" method="get">
 
76
          <input placeholder="Quick search" type="text" name="q" />
 
77
          <input type="submit" value="Go" />
 
78
          <input type="hidden" name="check_keywords" value="yes" />
 
79
          <input type="hidden" name="area" value="default" />
 
80
        </form>
 
81
    </div>
 
82
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
83
         |
 
84
    </li>
 
85
 
 
86
      </ul>
 
87
    </div>    
 
88
 
 
89
    <div class="document">
 
90
      <div class="documentwrapper">
 
91
        <div class="bodywrapper">
 
92
          <div class="body" role="main">
 
93
            
 
94
  <div class="section" id="module-importlib">
 
95
<span id="importlib-the-implementation-of-import"></span><h1>31.5. <a class="reference internal" href="#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal"><span class="pre">importlib</span></code></a> &#8211; The implementation of <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a><a class="headerlink" href="#module-importlib" title="Permalink to this headline">¶</a></h1>
 
96
<div class="versionadded">
 
97
<p><span class="versionmodified">New in version 3.1.</span></p>
 
98
</div>
 
99
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/importlib/__init__.py">Lib/importlib/__init__.py</a></p>
 
100
<hr class="docutils" />
 
101
<div class="section" id="introduction">
 
102
<h2>31.5.1. Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
 
103
<p>The purpose of the <a class="reference internal" href="#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal"><span class="pre">importlib</span></code></a> package is two-fold. One is to provide the
 
104
implementation of the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a> statement (and thus, by extension, the
 
105
<a class="reference internal" href="functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal"><span class="pre">__import__()</span></code></a> function) in Python source code. This provides an
 
106
implementation of <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a> which is portable to any Python
 
107
interpreter. This also provides an implementation which is easier to
 
108
comprehend than one implemented in a programming language other than Python.</p>
 
109
<p>Two, the components to implement <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a> are exposed in this
 
110
package, making it easier for users to create their own custom objects (known
 
111
generically as an <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a>) to participate in the import process.</p>
 
112
<div class="admonition seealso">
 
113
<p class="first admonition-title">See also</p>
 
114
<dl class="last docutils">
 
115
<dt><a class="reference internal" href="../reference/simple_stmts.html#import"><span>The import statement</span></a></dt>
 
116
<dd>The language reference for the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a> statement.</dd>
 
117
<dt><a class="reference external" href="http://legacy.python.org/doc/essays/packages.html">Packages specification</a></dt>
 
118
<dd>Original specification of packages. Some semantics have changed since
 
119
the writing of this document (e.g. redirecting based on <code class="docutils literal"><span class="pre">None</span></code>
 
120
in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a>).</dd>
 
121
<dt>The <a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal"><span class="pre">__import__()</span></code></a> function</dt>
 
122
<dd>The <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a> statement is syntactic sugar for this function.</dd>
 
123
<dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0235"><strong>PEP 235</strong></a></dt>
 
124
<dd>Import on Case-Insensitive Platforms</dd>
 
125
<dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0263"><strong>PEP 263</strong></a></dt>
 
126
<dd>Defining Python Source Code Encodings</dd>
 
127
<dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a></dt>
 
128
<dd>New Import Hooks</dd>
 
129
<dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a></dt>
 
130
<dd>Imports: Multi-Line and Absolute/Relative</dd>
 
131
<dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0366"><strong>PEP 366</strong></a></dt>
 
132
<dd>Main module explicit relative imports</dd>
 
133
<dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0420"><strong>PEP 420</strong></a></dt>
 
134
<dd>Implicit namespace packages</dd>
 
135
<dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0451"><strong>PEP 451</strong></a></dt>
 
136
<dd>A ModuleSpec Type for the Import System</dd>
 
137
<dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a></dt>
 
138
<dd>Elimination of PYO files</dd>
 
139
<dt><span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a></dt>
 
140
<dd>Multi-phase extension module initialization</dd>
 
141
<dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3120"><strong>PEP 3120</strong></a></dt>
 
142
<dd>Using UTF-8 as the Default Source Encoding</dd>
 
143
<dt><span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a></dt>
 
144
<dd>PYC Repository Directories</dd>
 
145
</dl>
 
146
</div>
 
147
</div>
 
148
<div class="section" id="functions">
 
149
<h2>31.5.2. Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h2>
 
150
<dl class="function">
 
151
<dt id="importlib.__import__">
 
152
<code class="descclassname">importlib.</code><code class="descname">__import__</code><span class="sig-paren">(</span><em>name</em>, <em>globals=None</em>, <em>locals=None</em>, <em>fromlist=()</em>, <em>level=0</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.__import__" title="Permalink to this definition">¶</a></dt>
 
153
<dd><p>An implementation of the built-in <a class="reference internal" href="functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal"><span class="pre">__import__()</span></code></a> function.</p>
 
154
<div class="admonition note">
 
155
<p class="first admonition-title">Note</p>
 
156
<p class="last">Programmatic importing of modules should use <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal"><span class="pre">import_module()</span></code></a>
 
157
instead of this function.</p>
 
158
</div>
 
159
</dd></dl>
 
160
 
 
161
<dl class="function">
 
162
<dt id="importlib.import_module">
 
163
<code class="descclassname">importlib.</code><code class="descname">import_module</code><span class="sig-paren">(</span><em>name</em>, <em>package=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.import_module" title="Permalink to this definition">¶</a></dt>
 
164
<dd><p>Import a module. The <em>name</em> argument specifies what module to
 
165
import in absolute or relative terms
 
166
(e.g. either <code class="docutils literal"><span class="pre">pkg.mod</span></code> or <code class="docutils literal"><span class="pre">..mod</span></code>). If the name is
 
167
specified in relative terms, then the <em>package</em> argument must be set to
 
168
the name of the package which is to act as the anchor for resolving the
 
169
package name (e.g. <code class="docutils literal"><span class="pre">import_module('..mod',</span> <span class="pre">'pkg.subpkg')</span></code> will import
 
170
<code class="docutils literal"><span class="pre">pkg.mod</span></code>).</p>
 
171
<p>The <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal"><span class="pre">import_module()</span></code></a> function acts as a simplifying wrapper around
 
172
<a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal"><span class="pre">importlib.__import__()</span></code></a>. This means all semantics of the function are
 
173
derived from <a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal"><span class="pre">importlib.__import__()</span></code></a>. The most important difference
 
174
between these two functions is that <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal"><span class="pre">import_module()</span></code></a> returns the
 
175
specified package or module (e.g. <code class="docutils literal"><span class="pre">pkg.mod</span></code>), while <a class="reference internal" href="functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal"><span class="pre">__import__()</span></code></a>
 
176
returns the top-level package or module (e.g. <code class="docutils literal"><span class="pre">pkg</span></code>).</p>
 
177
<p>If you are dynamically importing a module that was created since the
 
178
interpreter began execution (e.g., created a Python source file), you may
 
179
need to call <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal"><span class="pre">invalidate_caches()</span></code></a> in order for the new module to be
 
180
noticed by the import system.</p>
 
181
<div class="versionchanged">
 
182
<p><span class="versionmodified">Changed in version 3.3: </span>Parent packages are automatically imported.</p>
 
183
</div>
 
184
</dd></dl>
 
185
 
 
186
<dl class="function">
 
187
<dt id="importlib.find_loader">
 
188
<code class="descclassname">importlib.</code><code class="descname">find_loader</code><span class="sig-paren">(</span><em>name</em>, <em>path=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.find_loader" title="Permalink to this definition">¶</a></dt>
 
189
<dd><p>Find the loader for a module, optionally within the specified <em>path</em>. If the
 
190
module is in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal"><span class="pre">sys.modules</span></code></a>, then <code class="docutils literal"><span class="pre">sys.modules[name].__loader__</span></code> is
 
191
returned (unless the loader would be <code class="docutils literal"><span class="pre">None</span></code> or is not set, in which case
 
192
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised). Otherwise a search using <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal"><span class="pre">sys.meta_path</span></code></a>
 
193
is done. <code class="docutils literal"><span class="pre">None</span></code> is returned if no loader is found.</p>
 
194
<p>A dotted name does not have its parents implicitly imported as that requires
 
195
loading them and that may not be desired. To properly import a submodule you
 
196
will need to import all parent packages of the submodule and use the correct
 
197
argument to <em>path</em>.</p>
 
198
<div class="versionadded">
 
199
<p><span class="versionmodified">New in version 3.3.</span></p>
 
200
</div>
 
201
<div class="versionchanged">
 
202
<p><span class="versionmodified">Changed in version 3.4: </span>If <code class="docutils literal"><span class="pre">__loader__</span></code> is not set, raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>, just like when the
 
203
attribute is set to <code class="docutils literal"><span class="pre">None</span></code>.</p>
 
204
</div>
 
205
<div class="deprecated">
 
206
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.util.find_spec" title="importlib.util.find_spec"><code class="xref py py-func docutils literal"><span class="pre">importlib.util.find_spec()</span></code></a> instead.</p>
 
207
</div>
 
208
</dd></dl>
 
209
 
 
210
<dl class="function">
 
211
<dt id="importlib.invalidate_caches">
 
212
<code class="descclassname">importlib.</code><code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.invalidate_caches" title="Permalink to this definition">¶</a></dt>
 
213
<dd><p>Invalidate the internal caches of finders stored at
 
214
<a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal"><span class="pre">sys.meta_path</span></code></a>. If a finder implements <code class="docutils literal"><span class="pre">invalidate_caches()</span></code> then it
 
215
will be called to perform the invalidation.  This function should be called
 
216
if any modules are created/installed while your program is running to
 
217
guarantee all finders will notice the new module&#8217;s existence.</p>
 
218
<div class="versionadded">
 
219
<p><span class="versionmodified">New in version 3.3.</span></p>
 
220
</div>
 
221
</dd></dl>
 
222
 
 
223
<dl class="function">
 
224
<dt id="importlib.reload">
 
225
<code class="descclassname">importlib.</code><code class="descname">reload</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.reload" title="Permalink to this definition">¶</a></dt>
 
226
<dd><p>Reload a previously imported <em>module</em>.  The argument must be a module object,
 
227
so it must have been successfully imported before.  This is useful if you
 
228
have edited the module source file using an external editor and want to try
 
229
out the new version without leaving the Python interpreter.  The return value
 
230
is the module object (which can be different if re-importing causes a
 
231
different object to be placed in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a>).</p>
 
232
<p>When <a class="reference internal" href="#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal"><span class="pre">reload()</span></code></a> is executed:</p>
 
233
<ul class="simple">
 
234
<li>Python module&#8217;s code is recompiled and the module-level code re-executed,
 
235
defining a new set of objects which are bound to names in the module&#8217;s
 
236
dictionary by reusing the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which originally loaded the
 
237
module.  The <code class="docutils literal"><span class="pre">init</span></code> function of extension modules is not called a second
 
238
time.</li>
 
239
<li>As with all other objects in Python the old objects are only reclaimed
 
240
after their reference counts drop to zero.</li>
 
241
<li>The names in the module namespace are updated to point to any new or
 
242
changed objects.</li>
 
243
<li>Other references to the old objects (such as names external to the module) are
 
244
not rebound to refer to the new objects and must be updated in each namespace
 
245
where they occur if that is desired.</li>
 
246
</ul>
 
247
<p>There are a number of other caveats:</p>
 
248
<p>When a module is reloaded, its dictionary (containing the module&#8217;s global
 
249
variables) is retained.  Redefinitions of names will override the old
 
250
definitions, so this is generally not a problem.  If the new version of a
 
251
module does not define a name that was defined by the old version, the old
 
252
definition remains.  This feature can be used to the module&#8217;s advantage if it
 
253
maintains a global table or cache of objects &#8212; with a <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal"><span class="pre">try</span></code></a>
 
254
statement it can test for the table&#8217;s presence and skip its initialization if
 
255
desired:</p>
 
256
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
 
257
    <span class="n">cache</span>
 
258
<span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
 
259
    <span class="n">cache</span> <span class="o">=</span> <span class="p">{}</span>
 
260
</pre></div>
 
261
</div>
 
262
<p>It is generally not very useful to reload built-in or dynamically loaded
 
263
modules.  Reloading <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal"><span class="pre">sys</span></code></a>, <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal"><span class="pre">__main__</span></code></a>, <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal"><span class="pre">builtins</span></code></a> and other
 
264
key modules is not recommended.  In many cases extension modules are not
 
265
designed to be initialized more than once, and may fail in arbitrary ways
 
266
when reloaded.</p>
 
267
<p>If a module imports objects from another module using <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal"><span class="pre">from</span></code></a> ...
 
268
<a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a> ..., calling <a class="reference internal" href="#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal"><span class="pre">reload()</span></code></a> for the other module does not
 
269
redefine the objects imported from it &#8212; one way around this is to
 
270
re-execute the <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal"><span class="pre">from</span></code></a> statement, another is to use <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a>
 
271
and qualified names (<em>module.name</em>) instead.</p>
 
272
<p>If a module instantiates instances of a class, reloading the module that
 
273
defines the class does not affect the method definitions of the instances &#8212;
 
274
they continue to use the old class definition.  The same is true for derived
 
275
classes.</p>
 
276
<div class="versionadded">
 
277
<p><span class="versionmodified">New in version 3.4.</span></p>
 
278
</div>
 
279
</dd></dl>
 
280
 
 
281
</div>
 
282
<div class="section" id="module-importlib.abc">
 
283
<span id="importlib-abc-abstract-base-classes-related-to-import"></span><h2>31.5.3. <a class="reference internal" href="#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal"><span class="pre">importlib.abc</span></code></a> &#8211; Abstract base classes related to import<a class="headerlink" href="#module-importlib.abc" title="Permalink to this headline">¶</a></h2>
 
284
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/importlib/abc.py">Lib/importlib/abc.py</a></p>
 
285
<hr class="docutils" />
 
286
<p>The <a class="reference internal" href="#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal"><span class="pre">importlib.abc</span></code></a> module contains all of the core abstract base classes
 
287
used by <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a>. Some subclasses of the core abstract base classes
 
288
are also provided to help in implementing the core ABCs.</p>
 
289
<p>ABC hierarchy:</p>
 
290
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="nb">object</span>
 
291
 <span class="o">+--</span> <span class="n">Finder</span> <span class="p">(</span><span class="n">deprecated</span><span class="p">)</span>
 
292
 <span class="o">|</span>    <span class="o">+--</span> <span class="n">MetaPathFinder</span>
 
293
 <span class="o">|</span>    <span class="o">+--</span> <span class="n">PathEntryFinder</span>
 
294
 <span class="o">+--</span> <span class="n">Loader</span>
 
295
      <span class="o">+--</span> <span class="n">ResourceLoader</span> <span class="o">--------+</span>
 
296
      <span class="o">+--</span> <span class="n">InspectLoader</span>          <span class="o">|</span>
 
297
           <span class="o">+--</span> <span class="n">ExecutionLoader</span> <span class="o">--+</span>
 
298
                                 <span class="o">+--</span> <span class="n">FileLoader</span>
 
299
                                 <span class="o">+--</span> <span class="n">SourceLoader</span>
 
300
</pre></div>
 
301
</div>
 
302
<dl class="class">
 
303
<dt id="importlib.abc.Finder">
 
304
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">Finder</code><a class="headerlink" href="#importlib.abc.Finder" title="Permalink to this definition">¶</a></dt>
 
305
<dd><p>An abstract base class representing a <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a>.</p>
 
306
<div class="deprecated">
 
307
<p><span class="versionmodified">Deprecated since version 3.3: </span>Use <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal"><span class="pre">MetaPathFinder</span></code></a> or <a class="reference internal" href="#importlib.abc.PathEntryFinder" title="importlib.abc.PathEntryFinder"><code class="xref py py-class docutils literal"><span class="pre">PathEntryFinder</span></code></a> instead.</p>
 
308
</div>
 
309
<dl class="method">
 
310
<dt id="importlib.abc.Finder.find_module">
 
311
<em class="property">abstractmethod </em><code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em>, <em>path=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Finder.find_module" title="Permalink to this definition">¶</a></dt>
 
312
<dd><p>An abstact method for finding a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> for the specified
 
313
module.  Originally specified in <span class="target" id="index-11"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>, this method was meant
 
314
for use in <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal"><span class="pre">sys.meta_path</span></code></a> and in the path-based import subsystem.</p>
 
315
<div class="versionchanged">
 
316
<p><span class="versionmodified">Changed in version 3.4: </span>Returns <code class="docutils literal"><span class="pre">None</span></code> when called instead of raising
 
317
<a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>.</p>
 
318
</div>
 
319
</dd></dl>
 
320
 
 
321
</dd></dl>
 
322
 
 
323
<dl class="class">
 
324
<dt id="importlib.abc.MetaPathFinder">
 
325
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">MetaPathFinder</code><a class="headerlink" href="#importlib.abc.MetaPathFinder" title="Permalink to this definition">¶</a></dt>
 
326
<dd><p>An abstract base class representing a <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finder</span></a>. For
 
327
compatibility, this is a subclass of <a class="reference internal" href="#importlib.abc.Finder" title="importlib.abc.Finder"><code class="xref py py-class docutils literal"><span class="pre">Finder</span></code></a>.</p>
 
328
<div class="versionadded">
 
329
<p><span class="versionmodified">New in version 3.3.</span></p>
 
330
</div>
 
331
<dl class="method">
 
332
<dt id="importlib.abc.MetaPathFinder.find_spec">
 
333
<code class="descname">find_spec</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em>, <em>target=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.MetaPathFinder.find_spec" title="Permalink to this definition">¶</a></dt>
 
334
<dd><p>An abstract method for finding a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for
 
335
the specified module.  If this is a top-level import, <em>path</em> will
 
336
be <code class="docutils literal"><span class="pre">None</span></code>.  Otherwise, this is a search for a subpackage or
 
337
module and <em>path</em> will be the value of <a class="reference internal" href="../reference/import.html#__path__" title="__path__"><code class="xref py py-attr docutils literal"><span class="pre">__path__</span></code></a> from the
 
338
parent package. If a spec cannot be found, <code class="docutils literal"><span class="pre">None</span></code> is returned.
 
339
When passed in, <code class="docutils literal"><span class="pre">target</span></code> is a module object that the finder may
 
340
use to make a more educated about what spec to return.</p>
 
341
<div class="versionadded">
 
342
<p><span class="versionmodified">New in version 3.4.</span></p>
 
343
</div>
 
344
</dd></dl>
 
345
 
 
346
<dl class="method">
 
347
<dt id="importlib.abc.MetaPathFinder.find_module">
 
348
<code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.MetaPathFinder.find_module" title="Permalink to this definition">¶</a></dt>
 
349
<dd><p>A legacy method for finding a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> for the specified
 
350
module.  If this is a top-level import, <em>path</em> will be <code class="docutils literal"><span class="pre">None</span></code>.
 
351
Otherwise, this is a search for a subpackage or module and <em>path</em>
 
352
will be the value of <a class="reference internal" href="../reference/import.html#__path__" title="__path__"><code class="xref py py-attr docutils literal"><span class="pre">__path__</span></code></a> from the parent
 
353
package. If a loader cannot be found, <code class="docutils literal"><span class="pre">None</span></code> is returned.</p>
 
354
<p>If <a class="reference internal" href="#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a> is defined, backwards-compatible functionality is
 
355
provided.</p>
 
356
<div class="versionchanged">
 
357
<p><span class="versionmodified">Changed in version 3.4: </span>Returns <code class="docutils literal"><span class="pre">None</span></code> when called instead of raising
 
358
<a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>. Can use <a class="reference internal" href="#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a> to provide
 
359
functionality.</p>
 
360
</div>
 
361
<div class="deprecated">
 
362
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a> instead.</p>
 
363
</div>
 
364
</dd></dl>
 
365
 
 
366
<dl class="method">
 
367
<dt id="importlib.abc.MetaPathFinder.invalidate_caches">
 
368
<code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.MetaPathFinder.invalidate_caches" title="Permalink to this definition">¶</a></dt>
 
369
<dd><p>An optional method which, when called, should invalidate any internal
 
370
cache used by the finder. Used by <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal"><span class="pre">importlib.invalidate_caches()</span></code></a>
 
371
when invalidating the caches of all finders on <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal"><span class="pre">sys.meta_path</span></code></a>.</p>
 
372
<div class="versionchanged">
 
373
<p><span class="versionmodified">Changed in version 3.4: </span>Returns <code class="docutils literal"><span class="pre">None</span></code> when called instead of <code class="docutils literal"><span class="pre">NotImplemented</span></code>.</p>
 
374
</div>
 
375
</dd></dl>
 
376
 
 
377
</dd></dl>
 
378
 
 
379
<dl class="class">
 
380
<dt id="importlib.abc.PathEntryFinder">
 
381
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">PathEntryFinder</code><a class="headerlink" href="#importlib.abc.PathEntryFinder" title="Permalink to this definition">¶</a></dt>
 
382
<dd><p>An abstract base class representing a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>.  Though
 
383
it bears some similarities to <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal"><span class="pre">MetaPathFinder</span></code></a>, <code class="docutils literal"><span class="pre">PathEntryFinder</span></code>
 
384
is meant for use only within the path-based import subsystem provided
 
385
by <code class="xref py py-class docutils literal"><span class="pre">PathFinder</span></code>. This ABC is a subclass of <a class="reference internal" href="#importlib.abc.Finder" title="importlib.abc.Finder"><code class="xref py py-class docutils literal"><span class="pre">Finder</span></code></a> for
 
386
compatibility reasons only.</p>
 
387
<div class="versionadded">
 
388
<p><span class="versionmodified">New in version 3.3.</span></p>
 
389
</div>
 
390
<dl class="method">
 
391
<dt id="importlib.abc.PathEntryFinder.find_spec">
 
392
<code class="descname">find_spec</code><span class="sig-paren">(</span><em>fullname</em>, <em>target=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.find_spec" title="Permalink to this definition">¶</a></dt>
 
393
<dd><p>An abstract method for finding a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for
 
394
the specified module.  The finder will search for the module only
 
395
within the <a class="reference internal" href="../glossary.html#term-path-entry"><span class="xref std std-term">path entry</span></a> to which it is assigned.  If a spec
 
396
cannot be found, <code class="docutils literal"><span class="pre">None</span></code> is returned.  When passed in, <code class="docutils literal"><span class="pre">target</span></code>
 
397
is a module object that the finder may use to make a more educated
 
398
about what spec to return.</p>
 
399
<div class="versionadded">
 
400
<p><span class="versionmodified">New in version 3.4.</span></p>
 
401
</div>
 
402
</dd></dl>
 
403
 
 
404
<dl class="method">
 
405
<dt id="importlib.abc.PathEntryFinder.find_loader">
 
406
<code class="descname">find_loader</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.find_loader" title="Permalink to this definition">¶</a></dt>
 
407
<dd><p>A legacy method for finding a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> for the specified
 
408
module.  Returns a 2-tuple of <code class="docutils literal"><span class="pre">(loader,</span> <span class="pre">portion)</span></code> where <code class="docutils literal"><span class="pre">portion</span></code>
 
409
is a sequence of file system locations contributing to part of a namespace
 
410
package. The loader may be <code class="docutils literal"><span class="pre">None</span></code> while specifying <code class="docutils literal"><span class="pre">portion</span></code> to
 
411
signify the contribution of the file system locations to a namespace
 
412
package. An empty list can be used for <code class="docutils literal"><span class="pre">portion</span></code> to signify the loader
 
413
is not part of a namespace package. If <code class="docutils literal"><span class="pre">loader</span></code> is <code class="docutils literal"><span class="pre">None</span></code> and
 
414
<code class="docutils literal"><span class="pre">portion</span></code> is the empty list then no loader or location for a namespace
 
415
package were found (i.e. failure to find anything for the module).</p>
 
416
<p>If <a class="reference internal" href="#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a> is defined then backwards-compatible functionality is
 
417
provided.</p>
 
418
<div class="versionchanged">
 
419
<p><span class="versionmodified">Changed in version 3.4: </span>Returns <code class="docutils literal"><span class="pre">(None,</span> <span class="pre">[])</span></code> instead of raising <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>.
 
420
Uses <a class="reference internal" href="#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a> when available to provide functionality.</p>
 
421
</div>
 
422
<div class="deprecated">
 
423
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a> instead.</p>
 
424
</div>
 
425
</dd></dl>
 
426
 
 
427
<dl class="method">
 
428
<dt id="importlib.abc.PathEntryFinder.find_module">
 
429
<code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.find_module" title="Permalink to this definition">¶</a></dt>
 
430
<dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.Finder.find_module" title="importlib.abc.Finder.find_module"><code class="xref py py-meth docutils literal"><span class="pre">Finder.find_module()</span></code></a> which is
 
431
equivalent to <code class="docutils literal"><span class="pre">self.find_loader(fullname)[0]</span></code>.</p>
 
432
<div class="deprecated">
 
433
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a> instead.</p>
 
434
</div>
 
435
</dd></dl>
 
436
 
 
437
<dl class="method">
 
438
<dt id="importlib.abc.PathEntryFinder.invalidate_caches">
 
439
<code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.invalidate_caches" title="Permalink to this definition">¶</a></dt>
 
440
<dd><p>An optional method which, when called, should invalidate any internal
 
441
cache used by the finder. Used by <code class="xref py py-meth docutils literal"><span class="pre">PathFinder.invalidate_caches()</span></code>
 
442
when invalidating the caches of all cached finders.</p>
 
443
</dd></dl>
 
444
 
 
445
</dd></dl>
 
446
 
 
447
<dl class="class">
 
448
<dt id="importlib.abc.Loader">
 
449
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">Loader</code><a class="headerlink" href="#importlib.abc.Loader" title="Permalink to this definition">¶</a></dt>
 
450
<dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>.
 
451
See <span class="target" id="index-12"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> for the exact definition for a loader.</p>
 
452
<dl class="method">
 
453
<dt id="importlib.abc.Loader.create_module">
 
454
<code class="descname">create_module</code><span class="sig-paren">(</span><em>spec</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.create_module" title="Permalink to this definition">¶</a></dt>
 
455
<dd><p>A method that returns the module object to use when
 
456
importing a module.  This method may return <code class="docutils literal"><span class="pre">None</span></code>,
 
457
indicating that default module creation semantics should take place.</p>
 
458
<div class="versionadded">
 
459
<p><span class="versionmodified">New in version 3.4.</span></p>
 
460
</div>
 
461
<div class="versionchanged">
 
462
<p><span class="versionmodified">Changed in version 3.5: </span>Starting in Python 3.6, this method will not be optional when
 
463
<a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">exec_module()</span></code></a> is defined.</p>
 
464
</div>
 
465
</dd></dl>
 
466
 
 
467
<dl class="method">
 
468
<dt id="importlib.abc.Loader.exec_module">
 
469
<code class="descname">exec_module</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.exec_module" title="Permalink to this definition">¶</a></dt>
 
470
<dd><p>An abstract method that executes the module in its own namespace
 
471
when a module is imported or reloaded.  The module should already
 
472
be initialized when exec_module() is called.</p>
 
473
<div class="versionadded">
 
474
<p><span class="versionmodified">New in version 3.4.</span></p>
 
475
</div>
 
476
</dd></dl>
 
477
 
 
478
<dl class="method">
 
479
<dt id="importlib.abc.Loader.load_module">
 
480
<code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.load_module" title="Permalink to this definition">¶</a></dt>
 
481
<dd><p>A legacy method for loading a module. If the module cannot be
 
482
loaded, <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> is raised, otherwise the loaded module is
 
483
returned.</p>
 
484
<p>If the requested module already exists in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a>, that
 
485
module should be used and reloaded.
 
486
Otherwise the loader should create a new module and insert it into
 
487
<a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a> before any loading begins, to prevent recursion
 
488
from the import. If the loader inserted a module and the load fails, it
 
489
must be removed by the loader from <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a>; modules already
 
490
in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a> before the loader began execution should be left
 
491
alone (see <a class="reference internal" href="#importlib.util.module_for_loader" title="importlib.util.module_for_loader"><code class="xref py py-func docutils literal"><span class="pre">importlib.util.module_for_loader()</span></code></a>).</p>
 
492
<p>The loader should set several attributes on the module.
 
493
(Note that some of these attributes can change when a module is
 
494
reloaded):</p>
 
495
<ul>
 
496
<li><dl class="first docutils">
 
497
<dt><a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code></a></dt>
 
498
<dd><p class="first last">The name of the module.</p>
 
499
</dd>
 
500
</dl>
 
501
</li>
 
502
<li><dl class="first docutils">
 
503
<dt><a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal"><span class="pre">__file__</span></code></a></dt>
 
504
<dd><p class="first last">The path to where the module data is stored (not set for built-in
 
505
modules).</p>
 
506
</dd>
 
507
</dl>
 
508
</li>
 
509
<li><dl class="first docutils">
 
510
<dt><a class="reference internal" href="../reference/import.html#__cached__" title="__cached__"><code class="xref py py-attr docutils literal"><span class="pre">__cached__</span></code></a></dt>
 
511
<dd><p class="first last">The path to where a compiled version of the module is/should be
 
512
stored (not set when the attribute would be inappropriate).</p>
 
513
</dd>
 
514
</dl>
 
515
</li>
 
516
<li><dl class="first docutils">
 
517
<dt><a class="reference internal" href="../reference/import.html#__path__" title="__path__"><code class="xref py py-attr docutils literal"><span class="pre">__path__</span></code></a></dt>
 
518
<dd><p class="first last">A list of strings specifying the search path within a
 
519
package. This attribute is not set on modules.</p>
 
520
</dd>
 
521
</dl>
 
522
</li>
 
523
<li><dl class="first docutils">
 
524
<dt><a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a></dt>
 
525
<dd><p class="first last">The parent package for the module/package. If the module is
 
526
top-level then it has a value of the empty string. The
 
527
<a class="reference internal" href="#importlib.util.module_for_loader" title="importlib.util.module_for_loader"><code class="xref py py-func docutils literal"><span class="pre">importlib.util.module_for_loader()</span></code></a> decorator can handle the
 
528
details for <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a>.</p>
 
529
</dd>
 
530
</dl>
 
531
</li>
 
532
<li><dl class="first docutils">
 
533
<dt><a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal"><span class="pre">__loader__</span></code></a></dt>
 
534
<dd><p class="first last">The loader used to load the module. The
 
535
<a class="reference internal" href="#importlib.util.module_for_loader" title="importlib.util.module_for_loader"><code class="xref py py-func docutils literal"><span class="pre">importlib.util.module_for_loader()</span></code></a> decorator can handle the
 
536
details for <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a>.</p>
 
537
</dd>
 
538
</dl>
 
539
</li>
 
540
</ul>
 
541
<p>When <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">exec_module()</span></code></a> is available then backwards-compatible
 
542
functionality is provided.</p>
 
543
<div class="versionchanged">
 
544
<p><span class="versionmodified">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> when called instead of
 
545
<a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>. Functionality provided when
 
546
<a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">exec_module()</span></code></a> is available.</p>
 
547
</div>
 
548
<div class="deprecated">
 
549
<p><span class="versionmodified">Deprecated since version 3.4: </span>The recommended API for loading a module is <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">exec_module()</span></code></a>
 
550
(and <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal"><span class="pre">create_module()</span></code></a>).  Loaders should implement
 
551
it instead of load_module().  The import machinery takes care of
 
552
all the other responsibilities of load_module() when exec_module()
 
553
is implemented.</p>
 
554
</div>
 
555
</dd></dl>
 
556
 
 
557
<dl class="method">
 
558
<dt id="importlib.abc.Loader.module_repr">
 
559
<code class="descname">module_repr</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.module_repr" title="Permalink to this definition">¶</a></dt>
 
560
<dd><p>A legacy method which when implemented calculates and returns the
 
561
given module&#8217;s repr, as a string. The module type&#8217;s default repr() will
 
562
use the result of this method as appropriate.</p>
 
563
<div class="versionadded">
 
564
<p><span class="versionmodified">New in version 3.3.</span></p>
 
565
</div>
 
566
<div class="versionchanged">
 
567
<p><span class="versionmodified">Changed in version 3.4: </span>Made optional instead of an abstractmethod.</p>
 
568
</div>
 
569
<div class="deprecated">
 
570
<p><span class="versionmodified">Deprecated since version 3.4: </span>The import machinery now takes care of this automatically.</p>
 
571
</div>
 
572
</dd></dl>
 
573
 
 
574
</dd></dl>
 
575
 
 
576
<dl class="class">
 
577
<dt id="importlib.abc.ResourceLoader">
 
578
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">ResourceLoader</code><a class="headerlink" href="#importlib.abc.ResourceLoader" title="Permalink to this definition">¶</a></dt>
 
579
<dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which implements the optional
 
580
<span class="target" id="index-13"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> protocol for loading arbitrary resources from the storage
 
581
back-end.</p>
 
582
<dl class="method">
 
583
<dt id="importlib.abc.ResourceLoader.get_data">
 
584
<em class="property">abstractmethod </em><code class="descname">get_data</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ResourceLoader.get_data" title="Permalink to this definition">¶</a></dt>
 
585
<dd><p>An abstract method to return the bytes for the data located at <em>path</em>.
 
586
Loaders that have a file-like storage back-end
 
587
that allows storing arbitrary data
 
588
can implement this abstract method to give direct access
 
589
to the data stored. <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> is to be raised if the <em>path</em> cannot
 
590
be found. The <em>path</em> is expected to be constructed using a module&#8217;s
 
591
<a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal"><span class="pre">__file__</span></code></a> attribute or an item from a package&#8217;s <a class="reference internal" href="../reference/import.html#__path__" title="__path__"><code class="xref py py-attr docutils literal"><span class="pre">__path__</span></code></a>.</p>
 
592
<div class="versionchanged">
 
593
<p><span class="versionmodified">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>.</p>
 
594
</div>
 
595
</dd></dl>
 
596
 
 
597
</dd></dl>
 
598
 
 
599
<dl class="class">
 
600
<dt id="importlib.abc.InspectLoader">
 
601
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">InspectLoader</code><a class="headerlink" href="#importlib.abc.InspectLoader" title="Permalink to this definition">¶</a></dt>
 
602
<dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which implements the optional
 
603
<span class="target" id="index-14"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> protocol for loaders that inspect modules.</p>
 
604
<dl class="method">
 
605
<dt id="importlib.abc.InspectLoader.get_code">
 
606
<code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.get_code" title="Permalink to this definition">¶</a></dt>
 
607
<dd><p>Return the code object for a module, or <code class="docutils literal"><span class="pre">None</span></code> if the module does not
 
608
have a code object (as would be the case, for example, for a built-in
 
609
module).  Raise an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> if loader cannot find the
 
610
requested module.</p>
 
611
<div class="admonition note">
 
612
<p class="first admonition-title">Note</p>
 
613
<p class="last">While the method has a default implementation, it is suggested that
 
614
it be overridden if possible for performance.</p>
 
615
</div>
 
616
<div class="versionchanged" id="index-15">
 
617
<p><span class="versionmodified">Changed in version 3.4: </span>No longer abstract and a concrete implementation is provided.</p>
 
618
</div>
 
619
</dd></dl>
 
620
 
 
621
<dl class="method">
 
622
<dt id="importlib.abc.InspectLoader.get_source">
 
623
<em class="property">abstractmethod </em><code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.get_source" title="Permalink to this definition">¶</a></dt>
 
624
<dd><p>An abstract method to return the source of a module. It is returned as
 
625
a text string using <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a>, translating all
 
626
recognized line separators into <code class="docutils literal"><span class="pre">'\n'</span></code> characters.  Returns <code class="docutils literal"><span class="pre">None</span></code>
 
627
if no source is available (e.g. a built-in module). Raises
 
628
<a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> if the loader cannot find the module specified.</p>
 
629
<div class="versionchanged">
 
630
<p><span class="versionmodified">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>.</p>
 
631
</div>
 
632
</dd></dl>
 
633
 
 
634
<dl class="method">
 
635
<dt id="importlib.abc.InspectLoader.is_package">
 
636
<code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.is_package" title="Permalink to this definition">¶</a></dt>
 
637
<dd><p>An abstract method to return a true value if the module is a package, a
 
638
false value otherwise. <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> is raised if the
 
639
<a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> cannot find the module.</p>
 
640
<div class="versionchanged">
 
641
<p><span class="versionmodified">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>.</p>
 
642
</div>
 
643
</dd></dl>
 
644
 
 
645
<dl class="staticmethod">
 
646
<dt id="importlib.abc.InspectLoader.source_to_code">
 
647
<em class="property">static </em><code class="descname">source_to_code</code><span class="sig-paren">(</span><em>data</em>, <em>path='&lt;string&gt;'</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.source_to_code" title="Permalink to this definition">¶</a></dt>
 
648
<dd><p>Create a code object from Python source.</p>
 
649
<p>The <em>data</em> argument can be whatever the <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal"><span class="pre">compile()</span></code></a> function
 
650
supports (i.e. string or bytes). The <em>path</em> argument should be
 
651
the &#8220;path&#8221; to where the source code originated from, which can be an
 
652
abstract concept (e.g. location in a zip file).</p>
 
653
<p>With the subsequent code object one can execute it in a module by
 
654
running <code class="docutils literal"><span class="pre">exec(code,</span> <span class="pre">module.__dict__)</span></code>.</p>
 
655
<div class="versionadded">
 
656
<p><span class="versionmodified">New in version 3.4.</span></p>
 
657
</div>
 
658
<div class="versionchanged">
 
659
<p><span class="versionmodified">Changed in version 3.5: </span>Made the method static.</p>
 
660
</div>
 
661
</dd></dl>
 
662
 
 
663
<dl class="method">
 
664
<dt id="importlib.abc.InspectLoader.exec_module">
 
665
<code class="descname">exec_module</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.exec_module" title="Permalink to this definition">¶</a></dt>
 
666
<dd><p>Implementation of <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">Loader.exec_module()</span></code></a>.</p>
 
667
<div class="versionadded">
 
668
<p><span class="versionmodified">New in version 3.4.</span></p>
 
669
</div>
 
670
</dd></dl>
 
671
 
 
672
<dl class="method">
 
673
<dt id="importlib.abc.InspectLoader.load_module">
 
674
<code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.load_module" title="Permalink to this definition">¶</a></dt>
 
675
<dd><p>Implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal"><span class="pre">Loader.load_module()</span></code></a>.</p>
 
676
<div class="deprecated">
 
677
<p><span class="versionmodified">Deprecated since version 3.4: </span>use <a class="reference internal" href="#importlib.abc.InspectLoader.exec_module" title="importlib.abc.InspectLoader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">exec_module()</span></code></a> instead.</p>
 
678
</div>
 
679
</dd></dl>
 
680
 
 
681
</dd></dl>
 
682
 
 
683
<dl class="class">
 
684
<dt id="importlib.abc.ExecutionLoader">
 
685
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">ExecutionLoader</code><a class="headerlink" href="#importlib.abc.ExecutionLoader" title="Permalink to this definition">¶</a></dt>
 
686
<dd><p>An abstract base class which inherits from <a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal"><span class="pre">InspectLoader</span></code></a> that,
 
687
when implemented, helps a module to be executed as a script. The ABC
 
688
represents an optional <span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> protocol.</p>
 
689
<dl class="method">
 
690
<dt id="importlib.abc.ExecutionLoader.get_filename">
 
691
<em class="property">abstractmethod </em><code class="descname">get_filename</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ExecutionLoader.get_filename" title="Permalink to this definition">¶</a></dt>
 
692
<dd><p>An abstract method that is to return the value of <a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal"><span class="pre">__file__</span></code></a> for
 
693
the specified module. If no path is available, <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> is
 
694
raised.</p>
 
695
<p>If source code is available, then the method should return the path to
 
696
the source file, regardless of whether a bytecode was used to load the
 
697
module.</p>
 
698
<div class="versionchanged">
 
699
<p><span class="versionmodified">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>.</p>
 
700
</div>
 
701
</dd></dl>
 
702
 
 
703
</dd></dl>
 
704
 
 
705
<dl class="class">
 
706
<dt id="importlib.abc.FileLoader">
 
707
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">FileLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader" title="Permalink to this definition">¶</a></dt>
 
708
<dd><p>An abstract base class which inherits from <a class="reference internal" href="#importlib.abc.ResourceLoader" title="importlib.abc.ResourceLoader"><code class="xref py py-class docutils literal"><span class="pre">ResourceLoader</span></code></a> and
 
709
<a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal"><span class="pre">ExecutionLoader</span></code></a>, providing concrete implementations of
 
710
<a class="reference internal" href="#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal"><span class="pre">ResourceLoader.get_data()</span></code></a> and <a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal"><span class="pre">ExecutionLoader.get_filename()</span></code></a>.</p>
 
711
<p>The <em>fullname</em> argument is a fully resolved name of the module the loader is
 
712
to handle. The <em>path</em> argument is the path to the file for the module.</p>
 
713
<div class="versionadded">
 
714
<p><span class="versionmodified">New in version 3.3.</span></p>
 
715
</div>
 
716
<dl class="attribute">
 
717
<dt id="importlib.abc.FileLoader.name">
 
718
<code class="descname">name</code><a class="headerlink" href="#importlib.abc.FileLoader.name" title="Permalink to this definition">¶</a></dt>
 
719
<dd><p>The name of the module the loader can handle.</p>
 
720
</dd></dl>
 
721
 
 
722
<dl class="attribute">
 
723
<dt id="importlib.abc.FileLoader.path">
 
724
<code class="descname">path</code><a class="headerlink" href="#importlib.abc.FileLoader.path" title="Permalink to this definition">¶</a></dt>
 
725
<dd><p>Path to the file of the module.</p>
 
726
</dd></dl>
 
727
 
 
728
<dl class="method">
 
729
<dt id="importlib.abc.FileLoader.load_module">
 
730
<code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.load_module" title="Permalink to this definition">¶</a></dt>
 
731
<dd><p>Calls super&#8217;s <code class="docutils literal"><span class="pre">load_module()</span></code>.</p>
 
732
<div class="deprecated">
 
733
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">Loader.exec_module()</span></code></a> instead.</p>
 
734
</div>
 
735
</dd></dl>
 
736
 
 
737
<dl class="method">
 
738
<dt id="importlib.abc.FileLoader.get_filename">
 
739
<em class="property">abstractmethod </em><code class="descname">get_filename</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.get_filename" title="Permalink to this definition">¶</a></dt>
 
740
<dd><p>Returns <a class="reference internal" href="#importlib.abc.FileLoader.path" title="importlib.abc.FileLoader.path"><code class="xref py py-attr docutils literal"><span class="pre">path</span></code></a>.</p>
 
741
</dd></dl>
 
742
 
 
743
<dl class="method">
 
744
<dt id="importlib.abc.FileLoader.get_data">
 
745
<em class="property">abstractmethod </em><code class="descname">get_data</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.get_data" title="Permalink to this definition">¶</a></dt>
 
746
<dd><p>Reads <em>path</em> as a binary file and returns the bytes from it.</p>
 
747
</dd></dl>
 
748
 
 
749
</dd></dl>
 
750
 
 
751
<dl class="class">
 
752
<dt id="importlib.abc.SourceLoader">
 
753
<em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">SourceLoader</code><a class="headerlink" href="#importlib.abc.SourceLoader" title="Permalink to this definition">¶</a></dt>
 
754
<dd><p>An abstract base class for implementing source (and optionally bytecode)
 
755
file loading. The class inherits from both <a class="reference internal" href="#importlib.abc.ResourceLoader" title="importlib.abc.ResourceLoader"><code class="xref py py-class docutils literal"><span class="pre">ResourceLoader</span></code></a> and
 
756
<a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal"><span class="pre">ExecutionLoader</span></code></a>, requiring the implementation of:</p>
 
757
<ul>
 
758
<li><p class="first"><a class="reference internal" href="#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal"><span class="pre">ResourceLoader.get_data()</span></code></a></p>
 
759
</li>
 
760
<li><dl class="first docutils">
 
761
<dt><a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal"><span class="pre">ExecutionLoader.get_filename()</span></code></a></dt>
 
762
<dd><p class="first last">Should only return the path to the source file; sourceless
 
763
loading is not supported.</p>
 
764
</dd>
 
765
</dl>
 
766
</li>
 
767
</ul>
 
768
<p>The abstract methods defined by this class are to add optional bytecode
 
769
file support. Not implementing these optional methods (or causing them to
 
770
raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>) causes the loader to
 
771
only work with source code. Implementing the methods allows the loader to
 
772
work with source <em>and</em> bytecode files; it does not allow for <em>sourceless</em>
 
773
loading where only bytecode is provided.  Bytecode files are an
 
774
optimization to speed up loading by removing the parsing step of Python&#8217;s
 
775
compiler, and so no bytecode-specific API is exposed.</p>
 
776
<dl class="method">
 
777
<dt id="importlib.abc.SourceLoader.path_stats">
 
778
<code class="descname">path_stats</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.path_stats" title="Permalink to this definition">¶</a></dt>
 
779
<dd><p>Optional abstract method which returns a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal"><span class="pre">dict</span></code></a> containing
 
780
metadata about the specified path.  Supported dictionary keys are:</p>
 
781
<ul class="simple">
 
782
<li><code class="docutils literal"><span class="pre">'mtime'</span></code> (mandatory): an integer or floating-point number
 
783
representing the modification time of the source code;</li>
 
784
<li><code class="docutils literal"><span class="pre">'size'</span></code> (optional): the size in bytes of the source code.</li>
 
785
</ul>
 
786
<p>Any other keys in the dictionary are ignored, to allow for future
 
787
extensions. If the path cannot be handled, <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> is raised.</p>
 
788
<div class="versionadded">
 
789
<p><span class="versionmodified">New in version 3.3.</span></p>
 
790
</div>
 
791
<div class="versionchanged">
 
792
<p><span class="versionmodified">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>.</p>
 
793
</div>
 
794
</dd></dl>
 
795
 
 
796
<dl class="method">
 
797
<dt id="importlib.abc.SourceLoader.path_mtime">
 
798
<code class="descname">path_mtime</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.path_mtime" title="Permalink to this definition">¶</a></dt>
 
799
<dd><p>Optional abstract method which returns the modification time for the
 
800
specified path.</p>
 
801
<div class="deprecated">
 
802
<p><span class="versionmodified">Deprecated since version 3.3: </span>This method is deprecated in favour of <a class="reference internal" href="#importlib.abc.SourceLoader.path_stats" title="importlib.abc.SourceLoader.path_stats"><code class="xref py py-meth docutils literal"><span class="pre">path_stats()</span></code></a>.  You don&#8217;t
 
803
have to implement it, but it is still available for compatibility
 
804
purposes. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> if the path cannot be handled.</p>
 
805
</div>
 
806
<div class="versionchanged">
 
807
<p><span class="versionmodified">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a>.</p>
 
808
</div>
 
809
</dd></dl>
 
810
 
 
811
<dl class="method">
 
812
<dt id="importlib.abc.SourceLoader.set_data">
 
813
<code class="descname">set_data</code><span class="sig-paren">(</span><em>path</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.set_data" title="Permalink to this definition">¶</a></dt>
 
814
<dd><p>Optional abstract method which writes the specified bytes to a file
 
815
path. Any intermediate directories which do not exist are to be created
 
816
automatically.</p>
 
817
<p>When writing to the path fails because the path is read-only
 
818
(<a class="reference internal" href="errno.html#errno.EACCES" title="errno.EACCES"><code class="xref py py-attr docutils literal"><span class="pre">errno.EACCES</span></code></a>/<a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal"><span class="pre">PermissionError</span></code></a>), do not propagate the
 
819
exception.</p>
 
820
<div class="versionchanged">
 
821
<p><span class="versionmodified">Changed in version 3.4: </span>No longer raises <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a> when called.</p>
 
822
</div>
 
823
</dd></dl>
 
824
 
 
825
<dl class="method">
 
826
<dt id="importlib.abc.SourceLoader.get_code">
 
827
<code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.get_code" title="Permalink to this definition">¶</a></dt>
 
828
<dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.get_code" title="importlib.abc.InspectLoader.get_code"><code class="xref py py-meth docutils literal"><span class="pre">InspectLoader.get_code()</span></code></a>.</p>
 
829
</dd></dl>
 
830
 
 
831
<dl class="method">
 
832
<dt id="importlib.abc.SourceLoader.exec_module">
 
833
<code class="descname">exec_module</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.exec_module" title="Permalink to this definition">¶</a></dt>
 
834
<dd><blockquote>
 
835
<div>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">Loader.exec_module()</span></code></a>.</div></blockquote>
 
836
<div class="versionadded">
 
837
<p><span class="versionmodified">New in version 3.4.</span></p>
 
838
</div>
 
839
</dd></dl>
 
840
 
 
841
<dl class="method">
 
842
<dt id="importlib.abc.SourceLoader.load_module">
 
843
<code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.load_module" title="Permalink to this definition">¶</a></dt>
 
844
<dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal"><span class="pre">Loader.load_module()</span></code></a>.</p>
 
845
<div class="deprecated">
 
846
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.SourceLoader.exec_module" title="importlib.abc.SourceLoader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">exec_module()</span></code></a> instead.</p>
 
847
</div>
 
848
</dd></dl>
 
849
 
 
850
<dl class="method">
 
851
<dt id="importlib.abc.SourceLoader.get_source">
 
852
<code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.get_source" title="Permalink to this definition">¶</a></dt>
 
853
<dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.get_source" title="importlib.abc.InspectLoader.get_source"><code class="xref py py-meth docutils literal"><span class="pre">InspectLoader.get_source()</span></code></a>.</p>
 
854
</dd></dl>
 
855
 
 
856
<dl class="method">
 
857
<dt id="importlib.abc.SourceLoader.is_package">
 
858
<code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.is_package" title="Permalink to this definition">¶</a></dt>
 
859
<dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.is_package" title="importlib.abc.InspectLoader.is_package"><code class="xref py py-meth docutils literal"><span class="pre">InspectLoader.is_package()</span></code></a>. A module
 
860
is determined to be a package if its file path (as provided by
 
861
<a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal"><span class="pre">ExecutionLoader.get_filename()</span></code></a>) is a file named
 
862
<code class="docutils literal"><span class="pre">__init__</span></code> when the file extension is removed <strong>and</strong> the module name
 
863
itself does not end in <code class="docutils literal"><span class="pre">__init__</span></code>.</p>
 
864
</dd></dl>
 
865
 
 
866
</dd></dl>
 
867
 
 
868
</div>
 
869
<div class="section" id="module-importlib.machinery">
 
870
<span id="importlib-machinery-importers-and-path-hooks"></span><h2>31.5.4. <a class="reference internal" href="#module-importlib.machinery" title="importlib.machinery: Importers and path hooks"><code class="xref py py-mod docutils literal"><span class="pre">importlib.machinery</span></code></a> &#8211; Importers and path hooks<a class="headerlink" href="#module-importlib.machinery" title="Permalink to this headline">¶</a></h2>
 
871
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/importlib/machinery.py">Lib/importlib/machinery.py</a></p>
 
872
<hr class="docutils" />
 
873
<p>This module contains the various objects that help <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a>
 
874
find and load modules.</p>
 
875
<dl class="attribute">
 
876
<dt id="importlib.machinery.SOURCE_SUFFIXES">
 
877
<code class="descclassname">importlib.machinery.</code><code class="descname">SOURCE_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.SOURCE_SUFFIXES" title="Permalink to this definition">¶</a></dt>
 
878
<dd><p>A list of strings representing the recognized file suffixes for source
 
879
modules.</p>
 
880
<div class="versionadded">
 
881
<p><span class="versionmodified">New in version 3.3.</span></p>
 
882
</div>
 
883
</dd></dl>
 
884
 
 
885
<dl class="attribute">
 
886
<dt id="importlib.machinery.DEBUG_BYTECODE_SUFFIXES">
 
887
<code class="descclassname">importlib.machinery.</code><code class="descname">DEBUG_BYTECODE_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.DEBUG_BYTECODE_SUFFIXES" title="Permalink to this definition">¶</a></dt>
 
888
<dd><p>A list of strings representing the file suffixes for non-optimized bytecode
 
889
modules.</p>
 
890
<div class="versionadded">
 
891
<p><span class="versionmodified">New in version 3.3.</span></p>
 
892
</div>
 
893
<div class="deprecated">
 
894
<p><span class="versionmodified">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#importlib.machinery.BYTECODE_SUFFIXES" title="importlib.machinery.BYTECODE_SUFFIXES"><code class="xref py py-attr docutils literal"><span class="pre">BYTECODE_SUFFIXES</span></code></a> instead.</p>
 
895
</div>
 
896
</dd></dl>
 
897
 
 
898
<dl class="attribute">
 
899
<dt id="importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES">
 
900
<code class="descclassname">importlib.machinery.</code><code class="descname">OPTIMIZED_BYTECODE_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES" title="Permalink to this definition">¶</a></dt>
 
901
<dd><p>A list of strings representing the file suffixes for optimized bytecode
 
902
modules.</p>
 
903
<div class="versionadded">
 
904
<p><span class="versionmodified">New in version 3.3.</span></p>
 
905
</div>
 
906
<div class="deprecated">
 
907
<p><span class="versionmodified">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#importlib.machinery.BYTECODE_SUFFIXES" title="importlib.machinery.BYTECODE_SUFFIXES"><code class="xref py py-attr docutils literal"><span class="pre">BYTECODE_SUFFIXES</span></code></a> instead.</p>
 
908
</div>
 
909
</dd></dl>
 
910
 
 
911
<dl class="attribute">
 
912
<dt id="importlib.machinery.BYTECODE_SUFFIXES">
 
913
<code class="descclassname">importlib.machinery.</code><code class="descname">BYTECODE_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.BYTECODE_SUFFIXES" title="Permalink to this definition">¶</a></dt>
 
914
<dd><p>A list of strings representing the recognized file suffixes for bytecode
 
915
modules (including the leading dot).</p>
 
916
<div class="versionadded">
 
917
<p><span class="versionmodified">New in version 3.3.</span></p>
 
918
</div>
 
919
<div class="versionchanged">
 
920
<p><span class="versionmodified">Changed in version 3.5: </span>The value is no longer dependent on <code class="docutils literal"><span class="pre">__debug__</span></code>.</p>
 
921
</div>
 
922
</dd></dl>
 
923
 
 
924
<dl class="attribute">
 
925
<dt id="importlib.machinery.EXTENSION_SUFFIXES">
 
926
<code class="descclassname">importlib.machinery.</code><code class="descname">EXTENSION_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.EXTENSION_SUFFIXES" title="Permalink to this definition">¶</a></dt>
 
927
<dd><p>A list of strings representing the recognized file suffixes for
 
928
extension modules.</p>
 
929
<div class="versionadded">
 
930
<p><span class="versionmodified">New in version 3.3.</span></p>
 
931
</div>
 
932
</dd></dl>
 
933
 
 
934
<dl class="function">
 
935
<dt id="importlib.machinery.all_suffixes">
 
936
<code class="descclassname">importlib.machinery.</code><code class="descname">all_suffixes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.all_suffixes" title="Permalink to this definition">¶</a></dt>
 
937
<dd><p>Returns a combined list of strings representing all file suffixes for
 
938
modules recognized by the standard import machinery. This is a
 
939
helper for code which simply needs to know if a filesystem path
 
940
potentially refers to a module without needing any details on the kind
 
941
of module (for example, <a class="reference internal" href="inspect.html#inspect.getmodulename" title="inspect.getmodulename"><code class="xref py py-func docutils literal"><span class="pre">inspect.getmodulename()</span></code></a>).</p>
 
942
<div class="versionadded">
 
943
<p><span class="versionmodified">New in version 3.3.</span></p>
 
944
</div>
 
945
</dd></dl>
 
946
 
 
947
<dl class="class">
 
948
<dt id="importlib.machinery.BuiltinImporter">
 
949
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">BuiltinImporter</code><a class="headerlink" href="#importlib.machinery.BuiltinImporter" title="Permalink to this definition">¶</a></dt>
 
950
<dd><p>An <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a> for built-in modules. All known built-in modules are
 
951
listed in <a class="reference internal" href="sys.html#sys.builtin_module_names" title="sys.builtin_module_names"><code class="xref py py-data docutils literal"><span class="pre">sys.builtin_module_names</span></code></a>. This class implements the
 
952
<a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> and
 
953
<a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.InspectLoader</span></code></a> ABCs.</p>
 
954
<p>Only class methods are defined by this class to alleviate the need for
 
955
instantiation.</p>
 
956
<div class="versionchanged">
 
957
<p><span class="versionmodified">Changed in version 3.5: </span>As part of <span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a>, the builtin importer now implements
 
958
<code class="xref py py-meth docutils literal"><span class="pre">Loader.create_module()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">Loader.exec_module()</span></code></p>
 
959
</div>
 
960
</dd></dl>
 
961
 
 
962
<dl class="class">
 
963
<dt id="importlib.machinery.FrozenImporter">
 
964
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">FrozenImporter</code><a class="headerlink" href="#importlib.machinery.FrozenImporter" title="Permalink to this definition">¶</a></dt>
 
965
<dd><p>An <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a> for frozen modules. This class implements the
 
966
<a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> and
 
967
<a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.InspectLoader</span></code></a> ABCs.</p>
 
968
<p>Only class methods are defined by this class to alleviate the need for
 
969
instantiation.</p>
 
970
</dd></dl>
 
971
 
 
972
<dl class="class">
 
973
<dt id="importlib.machinery.WindowsRegistryFinder">
 
974
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">WindowsRegistryFinder</code><a class="headerlink" href="#importlib.machinery.WindowsRegistryFinder" title="Permalink to this definition">¶</a></dt>
 
975
<dd><p><a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">Finder</span></a> for modules declared in the Windows registry.  This class
 
976
implements the <a class="reference internal" href="#importlib.abc.Finder" title="importlib.abc.Finder"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.Finder</span></code></a> ABC.</p>
 
977
<p>Only class methods are defined by this class to alleviate the need for
 
978
instantiation.</p>
 
979
<div class="versionadded">
 
980
<p><span class="versionmodified">New in version 3.3.</span></p>
 
981
</div>
 
982
</dd></dl>
 
983
 
 
984
<dl class="class">
 
985
<dt id="importlib.machinery.PathFinder">
 
986
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">PathFinder</code><a class="headerlink" href="#importlib.machinery.PathFinder" title="Permalink to this definition">¶</a></dt>
 
987
<dd><p>A <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">Finder</span></a> for <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal"><span class="pre">sys.path</span></code></a> and package <code class="docutils literal"><span class="pre">__path__</span></code> attributes.
 
988
This class implements the <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> ABC.</p>
 
989
<p>Only class methods are defined by this class to alleviate the need for
 
990
instantiation.</p>
 
991
<dl class="classmethod">
 
992
<dt id="importlib.machinery.PathFinder.find_spec">
 
993
<em class="property">classmethod </em><code class="descname">find_spec</code><span class="sig-paren">(</span><em>fullname</em>, <em>path=None</em>, <em>target=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.PathFinder.find_spec" title="Permalink to this definition">¶</a></dt>
 
994
<dd><p>Class method that attempts to find a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a>
 
995
for the module specified by <em>fullname</em> on <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal"><span class="pre">sys.path</span></code></a> or, if
 
996
defined, on <em>path</em>. For each path entry that is searched,
 
997
<a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal"><span class="pre">sys.path_importer_cache</span></code></a> is checked. If a non-false object
 
998
is found then it is used as the <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a> to look
 
999
for the module being searched for. If no entry is found in
 
1000
<a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal"><span class="pre">sys.path_importer_cache</span></code></a>, then <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal"><span class="pre">sys.path_hooks</span></code></a> is
 
1001
searched for a finder for the path entry and, if found, is stored
 
1002
in <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal"><span class="pre">sys.path_importer_cache</span></code></a> along with being queried about
 
1003
the module. If no finder is ever found then <code class="docutils literal"><span class="pre">None</span></code> is both
 
1004
stored in the cache and returned.</p>
 
1005
<div class="versionadded">
 
1006
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1007
</div>
 
1008
<div class="versionchanged">
 
1009
<p><span class="versionmodified">Changed in version 3.5: </span>If the current working directory &#8211; represented by an empty string &#8211;
 
1010
is no longer valid then <code class="docutils literal"><span class="pre">None</span></code> is returned but no value is cached
 
1011
in <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal"><span class="pre">sys.path_importer_cache</span></code></a>.</p>
 
1012
</div>
 
1013
</dd></dl>
 
1014
 
 
1015
<dl class="classmethod">
 
1016
<dt id="importlib.machinery.PathFinder.find_module">
 
1017
<em class="property">classmethod </em><code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em>, <em>path=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.PathFinder.find_module" title="Permalink to this definition">¶</a></dt>
 
1018
<dd><p>A legacy wrapper around <a class="reference internal" href="#importlib.machinery.PathFinder.find_spec" title="importlib.machinery.PathFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a>.</p>
 
1019
<div class="deprecated">
 
1020
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.machinery.PathFinder.find_spec" title="importlib.machinery.PathFinder.find_spec"><code class="xref py py-meth docutils literal"><span class="pre">find_spec()</span></code></a> instead.</p>
 
1021
</div>
 
1022
</dd></dl>
 
1023
 
 
1024
<dl class="classmethod">
 
1025
<dt id="importlib.machinery.PathFinder.invalidate_caches">
 
1026
<em class="property">classmethod </em><code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.PathFinder.invalidate_caches" title="Permalink to this definition">¶</a></dt>
 
1027
<dd><p>Calls <a class="reference internal" href="#importlib.abc.PathEntryFinder.invalidate_caches" title="importlib.abc.PathEntryFinder.invalidate_caches"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.PathEntryFinder.invalidate_caches()</span></code></a> on all
 
1028
finders stored in <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-attr docutils literal"><span class="pre">sys.path_importer_cache</span></code></a>.</p>
 
1029
</dd></dl>
 
1030
 
 
1031
<div class="versionchanged">
 
1032
<p><span class="versionmodified">Changed in version 3.4: </span>Calls objects in <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal"><span class="pre">sys.path_hooks</span></code></a> with the current working
 
1033
directory for <code class="docutils literal"><span class="pre">''</span></code> (i.e. the empty string).</p>
 
1034
</div>
 
1035
</dd></dl>
 
1036
 
 
1037
<dl class="class">
 
1038
<dt id="importlib.machinery.FileFinder">
 
1039
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">FileFinder</code><span class="sig-paren">(</span><em>path</em>, <em>*loader_details</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder" title="Permalink to this definition">¶</a></dt>
 
1040
<dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.PathEntryFinder" title="importlib.abc.PathEntryFinder"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.PathEntryFinder</span></code></a> which
 
1041
caches results from the file system.</p>
 
1042
<p>The <em>path</em> argument is the directory for which the finder is in charge of
 
1043
searching.</p>
 
1044
<p>The <em>loader_details</em> argument is a variable number of 2-item tuples each
 
1045
containing a loader and a sequence of file suffixes the loader recognizes.
 
1046
The loaders are expected to be callables which accept two arguments of
 
1047
the module&#8217;s name and the path to the file found.</p>
 
1048
<p>The finder will cache the directory contents as necessary, making stat calls
 
1049
for each module search to verify the cache is not outdated. Because cache
 
1050
staleness relies upon the granularity of the operating system&#8217;s state
 
1051
information of the file system, there is a potential race condition of
 
1052
searching for a module, creating a new file, and then searching for the
 
1053
module the new file represents. If the operations happen fast enough to fit
 
1054
within the granularity of stat calls, then the module search will fail. To
 
1055
prevent this from happening, when you create a module dynamically, make sure
 
1056
to call <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal"><span class="pre">importlib.invalidate_caches()</span></code></a>.</p>
 
1057
<div class="versionadded">
 
1058
<p><span class="versionmodified">New in version 3.3.</span></p>
 
1059
</div>
 
1060
<dl class="attribute">
 
1061
<dt id="importlib.machinery.FileFinder.path">
 
1062
<code class="descname">path</code><a class="headerlink" href="#importlib.machinery.FileFinder.path" title="Permalink to this definition">¶</a></dt>
 
1063
<dd><p>The path the finder will search in.</p>
 
1064
</dd></dl>
 
1065
 
 
1066
<dl class="method">
 
1067
<dt id="importlib.machinery.FileFinder.find_spec">
 
1068
<code class="descname">find_spec</code><span class="sig-paren">(</span><em>fullname</em>, <em>target=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.find_spec" title="Permalink to this definition">¶</a></dt>
 
1069
<dd><p>Attempt to find the spec to handle <em>fullname</em> within <a class="reference internal" href="#importlib.machinery.FileFinder.path" title="importlib.machinery.FileFinder.path"><code class="xref py py-attr docutils literal"><span class="pre">path</span></code></a>.</p>
 
1070
<div class="versionadded">
 
1071
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1072
</div>
 
1073
</dd></dl>
 
1074
 
 
1075
<dl class="method">
 
1076
<dt id="importlib.machinery.FileFinder.find_loader">
 
1077
<code class="descname">find_loader</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.find_loader" title="Permalink to this definition">¶</a></dt>
 
1078
<dd><p>Attempt to find the loader to handle <em>fullname</em> within <a class="reference internal" href="#importlib.machinery.FileFinder.path" title="importlib.machinery.FileFinder.path"><code class="xref py py-attr docutils literal"><span class="pre">path</span></code></a>.</p>
 
1079
</dd></dl>
 
1080
 
 
1081
<dl class="method">
 
1082
<dt id="importlib.machinery.FileFinder.invalidate_caches">
 
1083
<code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.invalidate_caches" title="Permalink to this definition">¶</a></dt>
 
1084
<dd><p>Clear out the internal cache.</p>
 
1085
</dd></dl>
 
1086
 
 
1087
<dl class="classmethod">
 
1088
<dt id="importlib.machinery.FileFinder.path_hook">
 
1089
<em class="property">classmethod </em><code class="descname">path_hook</code><span class="sig-paren">(</span><em>*loader_details</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.path_hook" title="Permalink to this definition">¶</a></dt>
 
1090
<dd><p>A class method which returns a closure for use on <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-attr docutils literal"><span class="pre">sys.path_hooks</span></code></a>.
 
1091
An instance of <a class="reference internal" href="#importlib.machinery.FileFinder" title="importlib.machinery.FileFinder"><code class="xref py py-class docutils literal"><span class="pre">FileFinder</span></code></a> is returned by the closure using the
 
1092
path argument given to the closure directly and <em>loader_details</em>
 
1093
indirectly.</p>
 
1094
<p>If the argument to the closure is not an existing directory,
 
1095
<a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> is raised.</p>
 
1096
</dd></dl>
 
1097
 
 
1098
</dd></dl>
 
1099
 
 
1100
<dl class="class">
 
1101
<dt id="importlib.machinery.SourceFileLoader">
 
1102
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">SourceFileLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader" title="Permalink to this definition">¶</a></dt>
 
1103
<dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader" title="importlib.abc.SourceLoader"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.SourceLoader</span></code></a> by
 
1104
subclassing <a class="reference internal" href="#importlib.abc.FileLoader" title="importlib.abc.FileLoader"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.FileLoader</span></code></a> and providing some concrete
 
1105
implementations of other methods.</p>
 
1106
<div class="versionadded">
 
1107
<p><span class="versionmodified">New in version 3.3.</span></p>
 
1108
</div>
 
1109
<dl class="attribute">
 
1110
<dt id="importlib.machinery.SourceFileLoader.name">
 
1111
<code class="descname">name</code><a class="headerlink" href="#importlib.machinery.SourceFileLoader.name" title="Permalink to this definition">¶</a></dt>
 
1112
<dd><p>The name of the module that this loader will handle.</p>
 
1113
</dd></dl>
 
1114
 
 
1115
<dl class="attribute">
 
1116
<dt id="importlib.machinery.SourceFileLoader.path">
 
1117
<code class="descname">path</code><a class="headerlink" href="#importlib.machinery.SourceFileLoader.path" title="Permalink to this definition">¶</a></dt>
 
1118
<dd><p>The path to the source file.</p>
 
1119
</dd></dl>
 
1120
 
 
1121
<dl class="method">
 
1122
<dt id="importlib.machinery.SourceFileLoader.is_package">
 
1123
<code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.is_package" title="Permalink to this definition">¶</a></dt>
 
1124
<dd><p>Return true if <a class="reference internal" href="#importlib.machinery.SourceFileLoader.path" title="importlib.machinery.SourceFileLoader.path"><code class="xref py py-attr docutils literal"><span class="pre">path</span></code></a> appears to be for a package.</p>
 
1125
</dd></dl>
 
1126
 
 
1127
<dl class="method">
 
1128
<dt id="importlib.machinery.SourceFileLoader.path_stats">
 
1129
<code class="descname">path_stats</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.path_stats" title="Permalink to this definition">¶</a></dt>
 
1130
<dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader.path_stats" title="importlib.abc.SourceLoader.path_stats"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.SourceLoader.path_stats()</span></code></a>.</p>
 
1131
</dd></dl>
 
1132
 
 
1133
<dl class="method">
 
1134
<dt id="importlib.machinery.SourceFileLoader.set_data">
 
1135
<code class="descname">set_data</code><span class="sig-paren">(</span><em>path</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.set_data" title="Permalink to this definition">¶</a></dt>
 
1136
<dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader.set_data" title="importlib.abc.SourceLoader.set_data"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.SourceLoader.set_data()</span></code></a>.</p>
 
1137
</dd></dl>
 
1138
 
 
1139
<dl class="method">
 
1140
<dt id="importlib.machinery.SourceFileLoader.load_module">
 
1141
<code class="descname">load_module</code><span class="sig-paren">(</span><em>name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.load_module" title="Permalink to this definition">¶</a></dt>
 
1142
<dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> where
 
1143
specifying the name of the module to load is optional.</p>
 
1144
</dd></dl>
 
1145
 
 
1146
</dd></dl>
 
1147
 
 
1148
<dl class="class">
 
1149
<dt id="importlib.machinery.SourcelessFileLoader">
 
1150
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">SourcelessFileLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader" title="Permalink to this definition">¶</a></dt>
 
1151
<dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.FileLoader" title="importlib.abc.FileLoader"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.FileLoader</span></code></a> which can
 
1152
import bytecode files (i.e. no source code files exist).</p>
 
1153
<p>Please note that direct use of bytecode files (and thus not source code
 
1154
files) inhibits your modules from being usable by all Python
 
1155
implementations or new versions of Python which change the bytecode
 
1156
format.</p>
 
1157
<div class="versionadded">
 
1158
<p><span class="versionmodified">New in version 3.3.</span></p>
 
1159
</div>
 
1160
<dl class="attribute">
 
1161
<dt id="importlib.machinery.SourcelessFileLoader.name">
 
1162
<code class="descname">name</code><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.name" title="Permalink to this definition">¶</a></dt>
 
1163
<dd><p>The name of the module the loader will handle.</p>
 
1164
</dd></dl>
 
1165
 
 
1166
<dl class="attribute">
 
1167
<dt id="importlib.machinery.SourcelessFileLoader.path">
 
1168
<code class="descname">path</code><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.path" title="Permalink to this definition">¶</a></dt>
 
1169
<dd><p>The path to the bytecode file.</p>
 
1170
</dd></dl>
 
1171
 
 
1172
<dl class="method">
 
1173
<dt id="importlib.machinery.SourcelessFileLoader.is_package">
 
1174
<code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.is_package" title="Permalink to this definition">¶</a></dt>
 
1175
<dd><p>Determines if the module is a package based on <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.path" title="importlib.machinery.SourcelessFileLoader.path"><code class="xref py py-attr docutils literal"><span class="pre">path</span></code></a>.</p>
 
1176
</dd></dl>
 
1177
 
 
1178
<dl class="method">
 
1179
<dt id="importlib.machinery.SourcelessFileLoader.get_code">
 
1180
<code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.get_code" title="Permalink to this definition">¶</a></dt>
 
1181
<dd><p>Returns the code object for <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.name" title="importlib.machinery.SourcelessFileLoader.name"><code class="xref py py-attr docutils literal"><span class="pre">name</span></code></a> created from <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.path" title="importlib.machinery.SourcelessFileLoader.path"><code class="xref py py-attr docutils literal"><span class="pre">path</span></code></a>.</p>
 
1182
</dd></dl>
 
1183
 
 
1184
<dl class="method">
 
1185
<dt id="importlib.machinery.SourcelessFileLoader.get_source">
 
1186
<code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.get_source" title="Permalink to this definition">¶</a></dt>
 
1187
<dd><p>Returns <code class="docutils literal"><span class="pre">None</span></code> as bytecode files have no source when this loader is
 
1188
used.</p>
 
1189
</dd></dl>
 
1190
 
 
1191
<dl class="method">
 
1192
<dt id="importlib.machinery.SourcelessFileLoader.load_module">
 
1193
<code class="descname">load_module</code><span class="sig-paren">(</span><em>name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.load_module" title="Permalink to this definition">¶</a></dt>
 
1194
<dd></dd></dl>
 
1195
 
 
1196
<p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> where
 
1197
specifying the name of the module to load is optional.</p>
 
1198
</dd></dl>
 
1199
 
 
1200
<dl class="class">
 
1201
<dt id="importlib.machinery.ExtensionFileLoader">
 
1202
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">ExtensionFileLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader" title="Permalink to this definition">¶</a></dt>
 
1203
<dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.ExecutionLoader</span></code></a> for
 
1204
extension modules.</p>
 
1205
<p>The <em>fullname</em> argument specifies the name of the module the loader is to
 
1206
support. The <em>path</em> argument is the path to the extension module&#8217;s file.</p>
 
1207
<div class="versionadded">
 
1208
<p><span class="versionmodified">New in version 3.3.</span></p>
 
1209
</div>
 
1210
<dl class="attribute">
 
1211
<dt id="importlib.machinery.ExtensionFileLoader.name">
 
1212
<code class="descname">name</code><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.name" title="Permalink to this definition">¶</a></dt>
 
1213
<dd><p>Name of the module the loader supports.</p>
 
1214
</dd></dl>
 
1215
 
 
1216
<dl class="attribute">
 
1217
<dt id="importlib.machinery.ExtensionFileLoader.path">
 
1218
<code class="descname">path</code><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.path" title="Permalink to this definition">¶</a></dt>
 
1219
<dd><p>Path to the extension module.</p>
 
1220
</dd></dl>
 
1221
 
 
1222
<dl class="method">
 
1223
<dt id="importlib.machinery.ExtensionFileLoader.create_module">
 
1224
<code class="descname">create_module</code><span class="sig-paren">(</span><em>spec</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.create_module" title="Permalink to this definition">¶</a></dt>
 
1225
<dd><p>Creates the module object from the given specification in accordance
 
1226
with <span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a>.</p>
 
1227
<div class="versionadded">
 
1228
<p><span class="versionmodified">New in version 3.5.</span></p>
 
1229
</div>
 
1230
</dd></dl>
 
1231
 
 
1232
<dl class="method">
 
1233
<dt id="importlib.machinery.ExtensionFileLoader.exec_module">
 
1234
<code class="descname">exec_module</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.exec_module" title="Permalink to this definition">¶</a></dt>
 
1235
<dd><p>Initializes the given module object in accordance with <span class="target" id="index-19"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a>.</p>
 
1236
<div class="versionadded">
 
1237
<p><span class="versionmodified">New in version 3.5.</span></p>
 
1238
</div>
 
1239
</dd></dl>
 
1240
 
 
1241
<dl class="method">
 
1242
<dt id="importlib.machinery.ExtensionFileLoader.is_package">
 
1243
<code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.is_package" title="Permalink to this definition">¶</a></dt>
 
1244
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if the file path points to a package&#8217;s <code class="docutils literal"><span class="pre">__init__</span></code>
 
1245
module based on <a class="reference internal" href="#importlib.machinery.EXTENSION_SUFFIXES" title="importlib.machinery.EXTENSION_SUFFIXES"><code class="xref py py-attr docutils literal"><span class="pre">EXTENSION_SUFFIXES</span></code></a>.</p>
 
1246
</dd></dl>
 
1247
 
 
1248
<dl class="method">
 
1249
<dt id="importlib.machinery.ExtensionFileLoader.get_code">
 
1250
<code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_code" title="Permalink to this definition">¶</a></dt>
 
1251
<dd><p>Returns <code class="docutils literal"><span class="pre">None</span></code> as extension modules lack a code object.</p>
 
1252
</dd></dl>
 
1253
 
 
1254
<dl class="method">
 
1255
<dt id="importlib.machinery.ExtensionFileLoader.get_source">
 
1256
<code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_source" title="Permalink to this definition">¶</a></dt>
 
1257
<dd><p>Returns <code class="docutils literal"><span class="pre">None</span></code> as extension modules do not have source code.</p>
 
1258
</dd></dl>
 
1259
 
 
1260
<dl class="method">
 
1261
<dt id="importlib.machinery.ExtensionFileLoader.get_filename">
 
1262
<code class="descname">get_filename</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_filename" title="Permalink to this definition">¶</a></dt>
 
1263
<dd><p>Returns <a class="reference internal" href="#importlib.machinery.ExtensionFileLoader.path" title="importlib.machinery.ExtensionFileLoader.path"><code class="xref py py-attr docutils literal"><span class="pre">path</span></code></a>.</p>
 
1264
<div class="versionadded">
 
1265
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1266
</div>
 
1267
</dd></dl>
 
1268
 
 
1269
</dd></dl>
 
1270
 
 
1271
<dl class="class">
 
1272
<dt id="importlib.machinery.ModuleSpec">
 
1273
<em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">ModuleSpec</code><span class="sig-paren">(</span><em>name</em>, <em>loader</em>, <em>*</em>, <em>origin=None</em>, <em>loader_state=None</em>, <em>is_package=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ModuleSpec" title="Permalink to this definition">¶</a></dt>
 
1274
<dd><p>A specification for a module&#8217;s import-system-related state.</p>
 
1275
<div class="versionadded">
 
1276
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1277
</div>
 
1278
<dl class="attribute">
 
1279
<dt id="importlib.machinery.ModuleSpec.name">
 
1280
<code class="descname">name</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.name" title="Permalink to this definition">¶</a></dt>
 
1281
<dd></dd></dl>
 
1282
 
 
1283
<p>(<code class="docutils literal"><span class="pre">__name__</span></code>)</p>
 
1284
<p>A string for the fully-qualified name of the module.</p>
 
1285
<dl class="attribute">
 
1286
<dt id="importlib.machinery.ModuleSpec.loader">
 
1287
<code class="descname">loader</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.loader" title="Permalink to this definition">¶</a></dt>
 
1288
<dd></dd></dl>
 
1289
 
 
1290
<p>(<code class="docutils literal"><span class="pre">__loader__</span></code>)</p>
 
1291
<p>The loader to use for loading.  For namespace packages this should be
 
1292
set to <code class="docutils literal"><span class="pre">None</span></code>.</p>
 
1293
<dl class="attribute">
 
1294
<dt id="importlib.machinery.ModuleSpec.origin">
 
1295
<code class="descname">origin</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.origin" title="Permalink to this definition">¶</a></dt>
 
1296
<dd></dd></dl>
 
1297
 
 
1298
<p>(<code class="docutils literal"><span class="pre">__file__</span></code>)</p>
 
1299
<p>Name of the place from which the module is loaded, e.g. &#8220;builtin&#8221; for
 
1300
built-in modules and the filename for modules loaded from source.
 
1301
Normally &#8220;origin&#8221; should be set, but it may be <code class="docutils literal"><span class="pre">None</span></code> (the default)
 
1302
which indicates it is unspecified.</p>
 
1303
<dl class="attribute">
 
1304
<dt id="importlib.machinery.ModuleSpec.submodule_search_locations">
 
1305
<code class="descname">submodule_search_locations</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.submodule_search_locations" title="Permalink to this definition">¶</a></dt>
 
1306
<dd></dd></dl>
 
1307
 
 
1308
<p>(<code class="docutils literal"><span class="pre">__path__</span></code>)</p>
 
1309
<p>List of strings for where to find submodules, if a package (<code class="docutils literal"><span class="pre">None</span></code>
 
1310
otherwise).</p>
 
1311
<dl class="attribute">
 
1312
<dt id="importlib.machinery.ModuleSpec.loader_state">
 
1313
<code class="descname">loader_state</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.loader_state" title="Permalink to this definition">¶</a></dt>
 
1314
<dd></dd></dl>
 
1315
 
 
1316
<p>Container of extra module-specific data for use during loading (or
 
1317
<code class="docutils literal"><span class="pre">None</span></code>).</p>
 
1318
<dl class="attribute">
 
1319
<dt id="importlib.machinery.ModuleSpec.cached">
 
1320
<code class="descname">cached</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.cached" title="Permalink to this definition">¶</a></dt>
 
1321
<dd></dd></dl>
 
1322
 
 
1323
<p>(<code class="docutils literal"><span class="pre">__cached__</span></code>)</p>
 
1324
<p>String for where the compiled module should be stored (or <code class="docutils literal"><span class="pre">None</span></code>).</p>
 
1325
<dl class="attribute">
 
1326
<dt id="importlib.machinery.ModuleSpec.parent">
 
1327
<code class="descname">parent</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.parent" title="Permalink to this definition">¶</a></dt>
 
1328
<dd></dd></dl>
 
1329
 
 
1330
<p>(<code class="docutils literal"><span class="pre">__package__</span></code>)</p>
 
1331
<p>(Read-only) Fully-qualified name of the package to which the module
 
1332
belongs as a submodule (or <code class="docutils literal"><span class="pre">None</span></code>).</p>
 
1333
<dl class="attribute">
 
1334
<dt id="importlib.machinery.ModuleSpec.has_location">
 
1335
<code class="descname">has_location</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.has_location" title="Permalink to this definition">¶</a></dt>
 
1336
<dd></dd></dl>
 
1337
 
 
1338
<p>Boolean indicating whether or not the module&#8217;s &#8220;origin&#8221;
 
1339
attribute refers to a loadable location.</p>
 
1340
</dd></dl>
 
1341
 
 
1342
</div>
 
1343
<div class="section" id="module-importlib.util">
 
1344
<span id="importlib-util-utility-code-for-importers"></span><h2>31.5.5. <a class="reference internal" href="#module-importlib.util" title="importlib.util: Utility code for importers"><code class="xref py py-mod docutils literal"><span class="pre">importlib.util</span></code></a> &#8211; Utility code for importers<a class="headerlink" href="#module-importlib.util" title="Permalink to this headline">¶</a></h2>
 
1345
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/importlib/util.py">Lib/importlib/util.py</a></p>
 
1346
<hr class="docutils" />
 
1347
<p>This module contains the various objects that help in the construction of
 
1348
an <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a>.</p>
 
1349
<dl class="attribute">
 
1350
<dt id="importlib.util.MAGIC_NUMBER">
 
1351
<code class="descclassname">importlib.util.</code><code class="descname">MAGIC_NUMBER</code><a class="headerlink" href="#importlib.util.MAGIC_NUMBER" title="Permalink to this definition">¶</a></dt>
 
1352
<dd><p>The bytes which represent the bytecode version number. If you need help with
 
1353
loading/writing bytecode then consider <a class="reference internal" href="#importlib.abc.SourceLoader" title="importlib.abc.SourceLoader"><code class="xref py py-class docutils literal"><span class="pre">importlib.abc.SourceLoader</span></code></a>.</p>
 
1354
<div class="versionadded">
 
1355
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1356
</div>
 
1357
</dd></dl>
 
1358
 
 
1359
<dl class="function">
 
1360
<dt id="importlib.util.cache_from_source">
 
1361
<code class="descclassname">importlib.util.</code><code class="descname">cache_from_source</code><span class="sig-paren">(</span><em>path</em>, <em>debug_override=None</em>, <em>*</em>, <em>optimization=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.cache_from_source" title="Permalink to this definition">¶</a></dt>
 
1362
<dd><p>Return the <span class="target" id="index-20"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a>/<span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> path to the byte-compiled file associated
 
1363
with the source <em>path</em>.  For example, if <em>path</em> is <code class="docutils literal"><span class="pre">/foo/bar/baz.py</span></code> the return
 
1364
value would be <code class="docutils literal"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code> for Python 3.2.
 
1365
The <code class="docutils literal"><span class="pre">cpython-32</span></code> string comes from the current magic tag (see
 
1366
<code class="xref py py-func docutils literal"><span class="pre">get_tag()</span></code>; if <code class="xref py py-attr docutils literal"><span class="pre">sys.implementation.cache_tag</span></code> is not defined then
 
1367
<a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a> will be raised).</p>
 
1368
<p>The <em>optimization</em> parameter is used to specify the optimization level of the
 
1369
bytecode file. An empty string represents no optimization, so
 
1370
<code class="docutils literal"><span class="pre">/foo/bar/baz.py</span></code> with an <em>optimization</em> of <code class="docutils literal"><span class="pre">''</span></code> will result in a
 
1371
bytecode path of <code class="docutils literal"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code>. <code class="docutils literal"><span class="pre">None</span></code> causes
 
1372
the interpter&#8217;s optimization level to be used. Any other value&#8217;s string
 
1373
representation being used, so <code class="docutils literal"><span class="pre">/foo/bar/baz.py</span></code> with an <em>optimization</em> of
 
1374
<code class="docutils literal"><span class="pre">2</span></code> will lead to the bytecode path of
 
1375
<code class="docutils literal"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.opt-2.pyc</span></code>. The string representation
 
1376
of <em>optimization</em> can only be alphanumeric, else <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised.</p>
 
1377
<p>The <em>debug_override</em> parameter is deprecated and can be used to override
 
1378
the system&#8217;s value for <code class="docutils literal"><span class="pre">__debug__</span></code>. A <code class="docutils literal"><span class="pre">True</span></code> value is the equivalent of
 
1379
setting <em>optimization</em> to the empty string. A <code class="docutils literal"><span class="pre">False</span></code> value is the same as
 
1380
setting <em>optimization</em> to <code class="docutils literal"><span class="pre">1</span></code>. If both <em>debug_override</em> an <em>optimization</em>
 
1381
are not <code class="docutils literal"><span class="pre">None</span></code> then <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> is raised.</p>
 
1382
<div class="versionadded">
 
1383
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1384
</div>
 
1385
<div class="versionchanged">
 
1386
<p><span class="versionmodified">Changed in version 3.5: </span>The <em>optimization</em> parameter was added and the <em>debug_override</em> parameter
 
1387
was deprecated.</p>
 
1388
</div>
 
1389
</dd></dl>
 
1390
 
 
1391
<dl class="function">
 
1392
<dt id="importlib.util.source_from_cache">
 
1393
<code class="descclassname">importlib.util.</code><code class="descname">source_from_cache</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.source_from_cache" title="Permalink to this definition">¶</a></dt>
 
1394
<dd><p>Given the <em>path</em> to a <span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> file name, return the associated source code
 
1395
file path.  For example, if <em>path</em> is
 
1396
<code class="docutils literal"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code> the returned path would be
 
1397
<code class="docutils literal"><span class="pre">/foo/bar/baz.py</span></code>.  <em>path</em> need not exist, however if it does not conform
 
1398
to <span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> or <span class="target" id="index-24"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> format, a <code class="docutils literal"><span class="pre">ValueError</span></code> is raised. If
 
1399
<code class="xref py py-attr docutils literal"><span class="pre">sys.implementation.cache_tag</span></code> is not defined,
 
1400
<a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a> is raised.</p>
 
1401
<div class="versionadded">
 
1402
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1403
</div>
 
1404
</dd></dl>
 
1405
 
 
1406
<dl class="function">
 
1407
<dt id="importlib.util.decode_source">
 
1408
<code class="descclassname">importlib.util.</code><code class="descname">decode_source</code><span class="sig-paren">(</span><em>source_bytes</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.decode_source" title="Permalink to this definition">¶</a></dt>
 
1409
<dd><p>Decode the given bytes representing source code and return it as a string
 
1410
with universal newlines (as required by
 
1411
<a class="reference internal" href="#importlib.abc.InspectLoader.get_source" title="importlib.abc.InspectLoader.get_source"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.InspectLoader.get_source()</span></code></a>).</p>
 
1412
<div class="versionadded">
 
1413
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1414
</div>
 
1415
</dd></dl>
 
1416
 
 
1417
<dl class="function">
 
1418
<dt id="importlib.util.resolve_name">
 
1419
<code class="descclassname">importlib.util.</code><code class="descname">resolve_name</code><span class="sig-paren">(</span><em>name</em>, <em>package</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.resolve_name" title="Permalink to this definition">¶</a></dt>
 
1420
<dd><p>Resolve a relative module name to an absolute one.</p>
 
1421
<p>If  <strong>name</strong> has no leading dots, then <strong>name</strong> is simply returned. This
 
1422
allows for usage such as
 
1423
<code class="docutils literal"><span class="pre">importlib.util.resolve_name('sys',</span> <span class="pre">__package__)</span></code> without doing a
 
1424
check to see if the <strong>package</strong> argument is needed.</p>
 
1425
<p><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised if <strong>name</strong> is a relative module name but
 
1426
package is a false value (e.g. <code class="docutils literal"><span class="pre">None</span></code> or the empty string).
 
1427
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is also raised a relative name would escape its containing
 
1428
package (e.g. requesting <code class="docutils literal"><span class="pre">..bacon</span></code> from within the <code class="docutils literal"><span class="pre">spam</span></code> package).</p>
 
1429
<div class="versionadded">
 
1430
<p><span class="versionmodified">New in version 3.3.</span></p>
 
1431
</div>
 
1432
</dd></dl>
 
1433
 
 
1434
<dl class="function">
 
1435
<dt id="importlib.util.find_spec">
 
1436
<code class="descclassname">importlib.util.</code><code class="descname">find_spec</code><span class="sig-paren">(</span><em>name</em>, <em>package=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.find_spec" title="Permalink to this definition">¶</a></dt>
 
1437
<dd><p>Find the <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for a module, optionally relative to
 
1438
the specified <strong>package</strong> name. If the module is in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal"><span class="pre">sys.modules</span></code></a>,
 
1439
then <code class="docutils literal"><span class="pre">sys.modules[name].__spec__</span></code> is returned (unless the spec would be
 
1440
<code class="docutils literal"><span class="pre">None</span></code> or is not set, in which case <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised).
 
1441
Otherwise a search using <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal"><span class="pre">sys.meta_path</span></code></a> is done. <code class="docutils literal"><span class="pre">None</span></code> is
 
1442
returned if no spec is found.</p>
 
1443
<p>If <strong>name</strong> is for a submodule (contains a dot), the parent module is
 
1444
automatically imported.</p>
 
1445
<p><strong>name</strong> and <strong>package</strong> work the same as for <code class="xref py py-func docutils literal"><span class="pre">import_module()</span></code>.</p>
 
1446
<div class="versionadded">
 
1447
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1448
</div>
 
1449
</dd></dl>
 
1450
 
 
1451
<dl class="function">
 
1452
<dt id="importlib.util.module_from_spec">
 
1453
<code class="descclassname">importlib.util.</code><code class="descname">module_from_spec</code><span class="sig-paren">(</span><em>spec</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.module_from_spec" title="Permalink to this definition">¶</a></dt>
 
1454
<dd><p>Create a new module based on <strong>spec</strong> and <code class="docutils literal"><span class="pre">spec.loader.create_module()</span></code>.</p>
 
1455
<p>If <code class="docutils literal"><span class="pre">spec.loader.create_module()</span></code> does not return <code class="docutils literal"><span class="pre">None</span></code>, then any
 
1456
pre-existing attributes will not be reset. Also, no <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></code></a>
 
1457
will be raised if triggered while accessing <strong>spec</strong> or setting an attribute
 
1458
on the module.</p>
 
1459
<p>This function is preferred over using <a class="reference internal" href="types.html#types.ModuleType" title="types.ModuleType"><code class="xref py py-class docutils literal"><span class="pre">types.ModuleType</span></code></a> to create a
 
1460
new module as <strong>spec</strong> is used to set as many import-controlled attributes on
 
1461
the module as possible.</p>
 
1462
<div class="versionadded">
 
1463
<p><span class="versionmodified">New in version 3.5.</span></p>
 
1464
</div>
 
1465
</dd></dl>
 
1466
 
 
1467
<dl class="function">
 
1468
<dt id="importlib.util.module_for_loader">
 
1469
<code class="descclassname">&#64;</code><code class="descclassname">importlib.util.</code><code class="descname">module_for_loader</code><a class="headerlink" href="#importlib.util.module_for_loader" title="Permalink to this definition">¶</a></dt>
 
1470
<dd><p>A <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> for <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.Loader.load_module()</span></code></a>
 
1471
to handle selecting the proper
 
1472
module object to load with. The decorated method is expected to have a call
 
1473
signature taking two positional arguments
 
1474
(e.g. <code class="docutils literal"><span class="pre">load_module(self,</span> <span class="pre">module)</span></code>) for which the second argument
 
1475
will be the module <strong>object</strong> to be used by the loader.
 
1476
Note that the decorator will not work on static methods because of the
 
1477
assumption of two arguments.</p>
 
1478
<p>The decorated method will take in the <strong>name</strong> of the module to be loaded
 
1479
as expected for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>. If the module is not found in
 
1480
<a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a> then a new one is constructed. Regardless of where the
 
1481
module came from, <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal"><span class="pre">__loader__</span></code></a> set to <strong>self</strong> and <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a>
 
1482
is set based on what <a class="reference internal" href="#importlib.abc.InspectLoader.is_package" title="importlib.abc.InspectLoader.is_package"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.InspectLoader.is_package()</span></code></a> returns
 
1483
(if available). These attributes are set unconditionally to support
 
1484
reloading.</p>
 
1485
<p>If an exception is raised by the decorated method and a module was added to
 
1486
<a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a>, then the module will be removed to prevent a partially
 
1487
initialized module from being in left in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a>. If the module
 
1488
was already in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal"><span class="pre">sys.modules</span></code></a> then it is left alone.</p>
 
1489
<div class="versionchanged">
 
1490
<p><span class="versionmodified">Changed in version 3.3: </span><a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal"><span class="pre">__loader__</span></code></a> and <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a> are automatically set
 
1491
(when possible).</p>
 
1492
</div>
 
1493
<div class="versionchanged">
 
1494
<p><span class="versionmodified">Changed in version 3.4: </span>Set <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code></a>, <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal"><span class="pre">__loader__</span></code></a> <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a>
 
1495
unconditionally to support reloading.</p>
 
1496
</div>
 
1497
<div class="deprecated">
 
1498
<p><span class="versionmodified">Deprecated since version 3.4: </span>The import machinery now directly performs all the functionality
 
1499
provided by this function.</p>
 
1500
</div>
 
1501
</dd></dl>
 
1502
 
 
1503
<dl class="function">
 
1504
<dt id="importlib.util.set_loader">
 
1505
<code class="descclassname">&#64;</code><code class="descclassname">importlib.util.</code><code class="descname">set_loader</code><a class="headerlink" href="#importlib.util.set_loader" title="Permalink to this definition">¶</a></dt>
 
1506
<dd><p>A <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> for <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.Loader.load_module()</span></code></a>
 
1507
to set the <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal"><span class="pre">__loader__</span></code></a>
 
1508
attribute on the returned module. If the attribute is already set the
 
1509
decorator does nothing. It is assumed that the first positional argument to
 
1510
the wrapped method (i.e. <code class="docutils literal"><span class="pre">self</span></code>) is what <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal"><span class="pre">__loader__</span></code></a> should be set
 
1511
to.</p>
 
1512
<div class="versionchanged">
 
1513
<p><span class="versionmodified">Changed in version 3.4: </span>Set <code class="docutils literal"><span class="pre">__loader__</span></code> if set to <code class="docutils literal"><span class="pre">None</span></code>, as if the attribute does not
 
1514
exist.</p>
 
1515
</div>
 
1516
<div class="deprecated">
 
1517
<p><span class="versionmodified">Deprecated since version 3.4: </span>The import machinery takes care of this automatically.</p>
 
1518
</div>
 
1519
</dd></dl>
 
1520
 
 
1521
<dl class="function">
 
1522
<dt id="importlib.util.set_package">
 
1523
<code class="descclassname">&#64;</code><code class="descclassname">importlib.util.</code><code class="descname">set_package</code><a class="headerlink" href="#importlib.util.set_package" title="Permalink to this definition">¶</a></dt>
 
1524
<dd><p>A <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> for <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> to set the <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a> attribute on the returned module. If <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a>
 
1525
is set and has a value other than <code class="docutils literal"><span class="pre">None</span></code> it will not be changed.</p>
 
1526
<div class="deprecated">
 
1527
<p><span class="versionmodified">Deprecated since version 3.4: </span>The import machinery takes care of this automatically.</p>
 
1528
</div>
 
1529
</dd></dl>
 
1530
 
 
1531
<dl class="function">
 
1532
<dt id="importlib.util.spec_from_loader">
 
1533
<code class="descclassname">importlib.util.</code><code class="descname">spec_from_loader</code><span class="sig-paren">(</span><em>name</em>, <em>loader</em>, <em>*</em>, <em>origin=None</em>, <em>is_package=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.spec_from_loader" title="Permalink to this definition">¶</a></dt>
 
1534
<dd><p>A factory function for creating a <code class="xref py py-class docutils literal"><span class="pre">ModuleSpec</span></code> instance based
 
1535
on a loader.  The parameters have the same meaning as they do for
 
1536
ModuleSpec.  The function uses available <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> APIs, such as
 
1537
<code class="xref py py-meth docutils literal"><span class="pre">InspectLoader.is_package()</span></code>, to fill in any missing
 
1538
information on the spec.</p>
 
1539
<div class="versionadded">
 
1540
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1541
</div>
 
1542
</dd></dl>
 
1543
 
 
1544
<dl class="function">
 
1545
<dt id="importlib.util.spec_from_file_location">
 
1546
<code class="descclassname">importlib.util.</code><code class="descname">spec_from_file_location</code><span class="sig-paren">(</span><em>name</em>, <em>location</em>, <em>*</em>, <em>loader=None</em>, <em>submodule_search_locations=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.spec_from_file_location" title="Permalink to this definition">¶</a></dt>
 
1547
<dd><p>A factory function for creating a <code class="xref py py-class docutils literal"><span class="pre">ModuleSpec</span></code> instance based
 
1548
on the path to a file.  Missing information will be filled in on the
 
1549
spec by making use of loader APIs and by the implication that the
 
1550
module will be file-based.</p>
 
1551
<div class="versionadded">
 
1552
<p><span class="versionmodified">New in version 3.4.</span></p>
 
1553
</div>
 
1554
</dd></dl>
 
1555
 
 
1556
<dl class="class">
 
1557
<dt id="importlib.util.LazyLoader">
 
1558
<em class="property">class </em><code class="descclassname">importlib.util.</code><code class="descname">LazyLoader</code><span class="sig-paren">(</span><em>loader</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.LazyLoader" title="Permalink to this definition">¶</a></dt>
 
1559
<dd><p>A class which postpones the execution of the loader of a module until the
 
1560
module has an attribute accessed.</p>
 
1561
<p>This class <strong>only</strong> works with loaders that define
 
1562
<a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal"><span class="pre">exec_module()</span></code></a> as control over what module type
 
1563
is used for the module is required. For those same reasons, the loader&#8217;s
 
1564
<a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal"><span class="pre">create_module()</span></code></a> method will be ignored (i.e., the
 
1565
loader&#8217;s method should only return <code class="docutils literal"><span class="pre">None</span></code>; this excludes
 
1566
<code class="xref py py-class docutils literal"><span class="pre">BuiltinImporter</span></code> and <code class="xref py py-class docutils literal"><span class="pre">ExtensionFileLoader</span></code>). Finally,
 
1567
modules which substitute the object placed into <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal"><span class="pre">sys.modules</span></code></a> will
 
1568
not work as there is no way to properly replace the module references
 
1569
throughout the interpreter safely; <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised if such a
 
1570
substitution is detected.</p>
 
1571
<div class="admonition note">
 
1572
<p class="first admonition-title">Note</p>
 
1573
<p class="last">For projects where startup time is critical, this class allows for
 
1574
potentially minimizing the cost of loading a module if it is never used.
 
1575
For projects where startup time is not essential then use of this class is
 
1576
<strong>heavily</strong> discouraged due to error messages created during loading being
 
1577
postponed and thus occurring out of context.</p>
 
1578
</div>
 
1579
<div class="versionadded">
 
1580
<p><span class="versionmodified">New in version 3.5.</span></p>
 
1581
</div>
 
1582
<dl class="classmethod">
 
1583
<dt id="importlib.util.LazyLoader.factory">
 
1584
<em class="property">classmethod </em><code class="descname">factory</code><span class="sig-paren">(</span><em>loader</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.LazyLoader.factory" title="Permalink to this definition">¶</a></dt>
 
1585
<dd><p>A static method which returns a callable that creates a lazy loader. This
 
1586
is meant to be used in situations where the loader is passed by class
 
1587
instead of by instance.</p>
 
1588
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">suffixes</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SOURCE_SUFFIXES</span>
 
1589
<span class="n">loader</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SourceFileLoader</span>
 
1590
<span class="n">lazy_loader</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">LazyLoader</span><span class="o">.</span><span class="n">factory</span><span class="p">(</span><span class="n">loader</span><span class="p">)</span>
 
1591
<span class="n">finder</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">FileFinder</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="p">(</span><span class="n">lazy_loader</span><span class="p">,</span> <span class="n">suffixes</span><span class="p">))</span>
 
1592
</pre></div>
 
1593
</div>
 
1594
</dd></dl>
 
1595
 
 
1596
</dd></dl>
 
1597
 
 
1598
</div>
 
1599
</div>
 
1600
 
 
1601
 
 
1602
          </div>
 
1603
        </div>
 
1604
      </div>
 
1605
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
1606
        <div class="sphinxsidebarwrapper">
 
1607
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
1608
  <ul>
 
1609
<li><a class="reference internal" href="#">31.5. <code class="docutils literal"><span class="pre">importlib</span></code> &#8211; The implementation of <code class="docutils literal"><span class="pre">import</span></code></a><ul>
 
1610
<li><a class="reference internal" href="#introduction">31.5.1. Introduction</a></li>
 
1611
<li><a class="reference internal" href="#functions">31.5.2. Functions</a></li>
 
1612
<li><a class="reference internal" href="#module-importlib.abc">31.5.3. <code class="docutils literal"><span class="pre">importlib.abc</span></code> &#8211; Abstract base classes related to import</a></li>
 
1613
<li><a class="reference internal" href="#module-importlib.machinery">31.5.4. <code class="docutils literal"><span class="pre">importlib.machinery</span></code> &#8211; Importers and path hooks</a></li>
 
1614
<li><a class="reference internal" href="#module-importlib.util">31.5.5. <code class="docutils literal"><span class="pre">importlib.util</span></code> &#8211; Utility code for importers</a></li>
 
1615
</ul>
 
1616
</li>
 
1617
</ul>
 
1618
 
 
1619
  <h4>Previous topic</h4>
 
1620
  <p class="topless"><a href="runpy.html"
 
1621
                        title="previous chapter">31.4. <code class="docutils literal"><span class="pre">runpy</span></code> &#8212; Locating and executing Python modules</a></p>
 
1622
  <h4>Next topic</h4>
 
1623
  <p class="topless"><a href="language.html"
 
1624
                        title="next chapter">32. Python Language Services</a></p>
 
1625
  <div role="note" aria-label="source link">
 
1626
    <h3>This Page</h3>
 
1627
    <ul class="this-page-menu">
 
1628
      <li><a href="../bugs.html">Report a Bug</a></li>
 
1629
      <li><a href="../_sources/library/importlib.txt"
 
1630
            rel="nofollow">Show Source</a></li>
 
1631
    </ul>
 
1632
  </div>
 
1633
        </div>
 
1634
      </div>
 
1635
      <div class="clearer"></div>
 
1636
    </div>  
 
1637
    <div class="related" role="navigation" aria-label="related navigation">
 
1638
      <h3>Navigation</h3>
 
1639
      <ul>
 
1640
        <li class="right" style="margin-right: 10px">
 
1641
          <a href="../genindex.html" title="General Index"
 
1642
             >index</a></li>
 
1643
        <li class="right" >
 
1644
          <a href="../py-modindex.html" title="Python Module Index"
 
1645
             >modules</a> |</li>
 
1646
        <li class="right" >
 
1647
          <a href="language.html" title="32. Python Language Services"
 
1648
             >next</a> |</li>
 
1649
        <li class="right" >
 
1650
          <a href="runpy.html" title="31.4. runpy — Locating and executing Python modules"
 
1651
             >previous</a> |</li>
 
1652
        <li><img src="../_static/py.png" alt=""
 
1653
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
1654
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
1655
        <li>
 
1656
          <span class="version_switcher_placeholder">3.5.2</span>
 
1657
          <a href="../index.html">Documentation </a> &raquo;
 
1658
        </li>
 
1659
 
 
1660
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
1661
          <li class="nav-item nav-item-2"><a href="modules.html" >31. Importing Modules</a> &raquo;</li>
 
1662
    <li class="right">
 
1663
        
 
1664
 
 
1665
    <div class="inline-search" style="display: none" role="search">
 
1666
        <form class="inline-search" action="../search.html" method="get">
 
1667
          <input placeholder="Quick search" type="text" name="q" />
 
1668
          <input type="submit" value="Go" />
 
1669
          <input type="hidden" name="check_keywords" value="yes" />
 
1670
          <input type="hidden" name="area" value="default" />
 
1671
        </form>
 
1672
    </div>
 
1673
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
1674
         |
 
1675
    </li>
 
1676
 
 
1677
      </ul>
 
1678
    </div>  
 
1679
    <div class="footer">
 
1680
    &copy; <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
 
1681
    <br />
 
1682
    The Python Software Foundation is a non-profit corporation.
 
1683
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
1684
    <br />
 
1685
    Last updated on Oct 19, 2016.
 
1686
    <a href="../bugs.html">Found a bug</a>?
 
1687
    <br />
 
1688
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
1689
    </div>
 
1690
 
 
1691
  </body>
 
1692
</html>
 
 
b'\\ No newline at end of file'