~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/library/imp.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>36.2. imp — Access the import internals &mdash; Python 3.5.1 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.1',
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.1 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.1 documentation" href="../contents.html" />
33
 
    <link rel="up" title="36. Superseded Modules" href="superseded.html" />
34
 
    <link rel="next" title="37. Undocumented Modules" href="undoc.html" />
35
 
    <link rel="prev" title="36.1. optparse — Parser for command line options" href="optparse.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
    
40
 
 
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="undoc.html" title="37. Undocumented Modules"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="optparse.html" title="36.1. optparse — Parser for command line options"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">3.5.1</span>
64
 
          <a href="../index.html">Documentation </a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="superseded.html" accesskey="U">36. Superseded Modules</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
77
 
  <div class="section" id="module-imp">
78
 
<span id="imp-access-the-import-internals"></span><h1>36.2. <a class="reference internal" href="#module-imp" title="imp: Access the implementation of the import statement. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">imp</span></code></a> &#8212; Access the <a class="reference internal" href="../reference/import.html#importsystem"><span>import</span></a> internals<a class="headerlink" href="#module-imp" title="Permalink to this headline">¶</a></h1>
79
 
<div class="deprecated">
80
 
<p><span class="versionmodified">Deprecated since version 3.4: </span>The <a class="reference internal" href="#module-imp" title="imp: Access the implementation of the import statement. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">imp</span></code></a> package is pending deprecation in favor of <a class="reference internal" href="importlib.html#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>.</p>
81
 
</div>
82
 
<p id="index-0">This module provides an interface to the mechanisms used to implement the
83
 
<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.  It defines the following constants and functions:</p>
84
 
<dl class="function">
85
 
<dt id="imp.get_magic">
86
 
<code class="descclassname">imp.</code><code class="descname">get_magic</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imp.get_magic" title="Permalink to this definition">¶</a></dt>
87
 
<dd><p id="index-1">Return the magic string value used to recognize byte-compiled code files
88
 
(<code class="file docutils literal"><span class="pre">.pyc</span></code> files).  (This value may be different for each Python version.)</p>
89
 
<div class="deprecated">
90
 
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="importlib.html#importlib.util.MAGIC_NUMBER" title="importlib.util.MAGIC_NUMBER"><code class="xref py py-attr docutils literal"><span class="pre">importlib.util.MAGIC_NUMBER</span></code></a> instead.</p>
91
 
</div>
92
 
</dd></dl>
93
 
 
94
 
<dl class="function">
95
 
<dt id="imp.get_suffixes">
96
 
<code class="descclassname">imp.</code><code class="descname">get_suffixes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imp.get_suffixes" title="Permalink to this definition">¶</a></dt>
97
 
<dd><p>Return a list of 3-element tuples, each describing a particular type of
98
 
module. Each triple has the form <code class="docutils literal"><span class="pre">(suffix,</span> <span class="pre">mode,</span> <span class="pre">type)</span></code>, where <em>suffix</em> is
99
 
a string to be appended to the module name to form the filename to search
100
 
for, <em>mode</em> is the mode string to pass to the built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> function
101
 
to open the file (this can be <code class="docutils literal"><span class="pre">'r'</span></code> for text files or <code class="docutils literal"><span class="pre">'rb'</span></code> for binary
102
 
files), and <em>type</em> is the file type, which has one of the values
103
 
<a class="reference internal" href="#imp.PY_SOURCE" title="imp.PY_SOURCE"><code class="xref py py-const docutils literal"><span class="pre">PY_SOURCE</span></code></a>, <a class="reference internal" href="#imp.PY_COMPILED" title="imp.PY_COMPILED"><code class="xref py py-const docutils literal"><span class="pre">PY_COMPILED</span></code></a>, or <a class="reference internal" href="#imp.C_EXTENSION" title="imp.C_EXTENSION"><code class="xref py py-const docutils literal"><span class="pre">C_EXTENSION</span></code></a>, described
104
 
below.</p>
105
 
<div class="deprecated">
106
 
<p><span class="versionmodified">Deprecated since version 3.3: </span>Use the constants defined on <a class="reference internal" href="importlib.html#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> instead.</p>
107
 
</div>
108
 
</dd></dl>
109
 
 
110
 
<dl class="function">
111
 
<dt id="imp.find_module">
112
 
<code class="descclassname">imp.</code><code class="descname">find_module</code><span class="sig-paren">(</span><em>name</em><span class="optional">[</span>, <em>path</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imp.find_module" title="Permalink to this definition">¶</a></dt>
113
 
<dd><p>Try to find the module <em>name</em>.  If <em>path</em> is omitted or <code class="docutils literal"><span class="pre">None</span></code>, the list of
114
 
directory names given by <code class="docutils literal"><span class="pre">sys.path</span></code> is searched, but first a few special
115
 
places are searched: the function tries to find a built-in module with the
116
 
given name (<a class="reference internal" href="#imp.C_BUILTIN" title="imp.C_BUILTIN"><code class="xref py py-const docutils literal"><span class="pre">C_BUILTIN</span></code></a>), then a frozen module (<a class="reference internal" href="#imp.PY_FROZEN" title="imp.PY_FROZEN"><code class="xref py py-const docutils literal"><span class="pre">PY_FROZEN</span></code></a>),
117
 
and on some systems some other places are looked in as well (on Windows, it
118
 
looks in the registry which may point to a specific file).</p>
119
 
<p>Otherwise, <em>path</em> must be a list of directory names; each directory is
120
 
searched for files with any of the suffixes returned by <a class="reference internal" href="#imp.get_suffixes" title="imp.get_suffixes"><code class="xref py py-func docutils literal"><span class="pre">get_suffixes()</span></code></a>
121
 
above.  Invalid names in the list are silently ignored (but all list items
122
 
must be strings).</p>
123
 
<p>If search is successful, the return value is a 3-element tuple <code class="docutils literal"><span class="pre">(file,</span>
124
 
<span class="pre">pathname,</span> <span class="pre">description)</span></code>:</p>
125
 
<p><em>file</em> is an open <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> positioned at the beginning, <em>pathname</em>
126
 
is the pathname of the file found, and <em>description</em> is a 3-element tuple as
127
 
contained in the list returned by <a class="reference internal" href="#imp.get_suffixes" title="imp.get_suffixes"><code class="xref py py-func docutils literal"><span class="pre">get_suffixes()</span></code></a> describing the kind of
128
 
module found.</p>
129
 
<p>If the module does not live in a file, the returned <em>file</em> is <code class="docutils literal"><span class="pre">None</span></code>,
130
 
<em>pathname</em> is the empty string, and the <em>description</em> tuple contains empty
131
 
strings for its suffix and mode; the module type is indicated as given in
132
 
parentheses above.  If the search is unsuccessful, <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
133
 
raised.  Other exceptions indicate problems with the arguments or
134
 
environment.</p>
135
 
<p>If the module is a package, <em>file</em> is <code class="docutils literal"><span class="pre">None</span></code>, <em>pathname</em> is the package
136
 
path and the last item in the <em>description</em> tuple is <a class="reference internal" href="#imp.PKG_DIRECTORY" title="imp.PKG_DIRECTORY"><code class="xref py py-const docutils literal"><span class="pre">PKG_DIRECTORY</span></code></a>.</p>
137
 
<p>This function does not handle hierarchical module names (names containing
138
 
dots).  In order to find <em>P.M</em>, that is, submodule <em>M</em> of package <em>P</em>, use
139
 
<a class="reference internal" href="#imp.find_module" title="imp.find_module"><code class="xref py py-func docutils literal"><span class="pre">find_module()</span></code></a> and <a class="reference internal" href="#imp.load_module" title="imp.load_module"><code class="xref py py-func docutils literal"><span class="pre">load_module()</span></code></a> to find and load package <em>P</em>, and
140
 
then use <a class="reference internal" href="#imp.find_module" title="imp.find_module"><code class="xref py py-func docutils literal"><span class="pre">find_module()</span></code></a> with the <em>path</em> argument set to <code class="docutils literal"><span class="pre">P.__path__</span></code>.
141
 
When <em>P</em> itself has a dotted name, apply this recipe recursively.</p>
142
 
<div class="deprecated">
143
 
<p><span class="versionmodified">Deprecated since version 3.3: </span>Use <a class="reference internal" href="importlib.html#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 unless Python 3.3
144
 
compatibility is required, in which case use
145
 
<a class="reference internal" href="importlib.html#importlib.find_loader" title="importlib.find_loader"><code class="xref py py-func docutils literal"><span class="pre">importlib.find_loader()</span></code></a>.</p>
146
 
</div>
147
 
</dd></dl>
148
 
 
149
 
<dl class="function">
150
 
<dt id="imp.load_module">
151
 
<code class="descclassname">imp.</code><code class="descname">load_module</code><span class="sig-paren">(</span><em>name</em>, <em>file</em>, <em>pathname</em>, <em>description</em><span class="sig-paren">)</span><a class="headerlink" href="#imp.load_module" title="Permalink to this definition">¶</a></dt>
152
 
<dd><p>Load a module that was previously found by <a class="reference internal" href="#imp.find_module" title="imp.find_module"><code class="xref py py-func docutils literal"><span class="pre">find_module()</span></code></a> (or by an
153
 
otherwise conducted search yielding compatible results).  This function does
154
 
more than importing the module: if the module was already imported, it will
155
 
reload the module!  The <em>name</em> argument indicates the full
156
 
module name (including the package name, if this is a submodule of a
157
 
package).  The <em>file</em> argument is an open file, and <em>pathname</em> is the
158
 
corresponding file name; these can be <code class="docutils literal"><span class="pre">None</span></code> and <code class="docutils literal"><span class="pre">''</span></code>, respectively, when
159
 
the module is a package or not being loaded from a file.  The <em>description</em>
160
 
argument is a tuple, as would be returned by <a class="reference internal" href="#imp.get_suffixes" title="imp.get_suffixes"><code class="xref py py-func docutils literal"><span class="pre">get_suffixes()</span></code></a>, describing
161
 
what kind of module must be loaded.</p>
162
 
<p>If the load is successful, the return value is the module object; otherwise,
163
 
an exception (usually <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>
164
 
<p><strong>Important:</strong> the caller is responsible for closing the <em>file</em> argument, if
165
 
it was not <code class="docutils literal"><span class="pre">None</span></code>, even when an exception is raised.  This is best done
166
 
using 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> ... <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal"><span class="pre">finally</span></code></a> statement.</p>
167
 
<div class="deprecated">
168
 
<p><span class="versionmodified">Deprecated since version 3.3: </span>If previously used in conjunction with <a class="reference internal" href="#imp.find_module" title="imp.find_module"><code class="xref py py-func docutils literal"><span class="pre">imp.find_module()</span></code></a> then
169
 
consider using <a class="reference internal" href="importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal"><span class="pre">importlib.import_module()</span></code></a>, otherwise use the loader
170
 
returned by the replacement you chose for <a class="reference internal" href="#imp.find_module" title="imp.find_module"><code class="xref py py-func docutils literal"><span class="pre">imp.find_module()</span></code></a>. If you
171
 
called <a class="reference internal" href="#imp.load_module" title="imp.load_module"><code class="xref py py-func docutils literal"><span class="pre">imp.load_module()</span></code></a> and related functions directly then use the
172
 
classes in <a class="reference internal" href="importlib.html#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>, e.g.
173
 
<code class="docutils literal"><span class="pre">importlib.machinery.SourceFileLoader(name,</span> <span class="pre">path).load_module()</span></code>.</p>
174
 
</div>
175
 
</dd></dl>
176
 
 
177
 
<dl class="function">
178
 
<dt id="imp.new_module">
179
 
<code class="descclassname">imp.</code><code class="descname">new_module</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#imp.new_module" title="Permalink to this definition">¶</a></dt>
180
 
<dd><p>Return a new empty module object called <em>name</em>.  This object is <em>not</em> inserted
181
 
in <code class="docutils literal"><span class="pre">sys.modules</span></code>.</p>
182
 
<div class="deprecated">
183
 
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <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> instead.</p>
184
 
</div>
185
 
</dd></dl>
186
 
 
187
 
<dl class="function">
188
 
<dt id="imp.reload">
189
 
<code class="descclassname">imp.</code><code class="descname">reload</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#imp.reload" title="Permalink to this definition">¶</a></dt>
190
 
<dd><p>Reload a previously imported <em>module</em>.  The argument must be a module object, so
191
 
it must have been successfully imported before.  This is useful if you have
192
 
edited the module source file using an external editor and want to try out the
193
 
new version without leaving the Python interpreter.  The return value is the
194
 
module object (the same as the <em>module</em> argument).</p>
195
 
<p>When <code class="docutils literal"><span class="pre">reload(module)</span></code> is executed:</p>
196
 
<ul class="simple">
197
 
<li>Python modules&#8217; code is recompiled and the module-level code reexecuted,
198
 
defining a new set of objects which are bound to names in the module&#8217;s
199
 
dictionary.  The <code class="docutils literal"><span class="pre">init</span></code> function of extension modules is not called a second
200
 
time.</li>
201
 
<li>As with all other objects in Python the old objects are only reclaimed after
202
 
their reference counts drop to zero.</li>
203
 
<li>The names in the module namespace are updated to point to any new or changed
204
 
objects.</li>
205
 
<li>Other references to the old objects (such as names external to the module) are
206
 
not rebound to refer to the new objects and must be updated in each namespace
207
 
where they occur if that is desired.</li>
208
 
</ul>
209
 
<p>There are a number of other caveats:</p>
210
 
<p>When a module is reloaded, its dictionary (containing the module&#8217;s global
211
 
variables) is retained.  Redefinitions of names will override the old
212
 
definitions, so this is generally not a problem.  If the new version of a module
213
 
does not define a name that was defined by the old version, the old definition
214
 
remains.  This feature can be used to the module&#8217;s advantage if it maintains a
215
 
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> statement it can test
216
 
for the table&#8217;s presence and skip its initialization if desired:</p>
217
 
<div class="highlight-python3"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
218
 
    <span class="n">cache</span>
219
 
<span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
220
 
    <span class="n">cache</span> <span class="o">=</span> <span class="p">{}</span>
221
 
</pre></div>
222
 
</div>
223
 
<p>It is legal though generally not very useful to reload built-in or dynamically
224
 
loaded modules, except for <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> and <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>.
225
 
In many cases, however, extension modules are not designed to be initialized
226
 
more than once, and may fail in arbitrary ways when reloaded.</p>
227
 
<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> ...
228
 
<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="#imp.reload" title="imp.reload"><code class="xref py py-func docutils literal"><span class="pre">reload()</span></code></a> for the other module does not
229
 
redefine the objects imported from it &#8212; one way around this is to re-execute
230
 
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> and qualified
231
 
names (<em>module</em>.*name*) instead.</p>
232
 
<p>If a module instantiates instances of a class, reloading the module that defines
233
 
the class does not affect the method definitions of the instances &#8212; they
234
 
continue to use the old class definition.  The same is true for derived classes.</p>
235
 
<div class="versionchanged">
236
 
<p><span class="versionmodified">Changed in version 3.3: </span>Relies on both <code class="docutils literal"><span class="pre">__name__</span></code> and <code class="docutils literal"><span class="pre">__loader__</span></code> being defined on the module
237
 
being reloaded instead of just <code class="docutils literal"><span class="pre">__name__</span></code>.</p>
238
 
</div>
239
 
<div class="deprecated">
240
 
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="importlib.html#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal"><span class="pre">importlib.reload()</span></code></a> instead.</p>
241
 
</div>
242
 
</dd></dl>
243
 
 
244
 
<p>The following functions are conveniences for handling <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> byte-compiled
245
 
file paths.</p>
246
 
<div class="versionadded">
247
 
<p><span class="versionmodified">New in version 3.2.</span></p>
248
 
</div>
249
 
<dl class="function">
250
 
<dt id="imp.cache_from_source">
251
 
<code class="descclassname">imp.</code><code class="descname">cache_from_source</code><span class="sig-paren">(</span><em>path</em>, <em>debug_override=None</em><span class="sig-paren">)</span><a class="headerlink" href="#imp.cache_from_source" title="Permalink to this definition">¶</a></dt>
252
 
<dd><p>Return the <span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> path to the byte-compiled file associated with the
253
 
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
254
 
value would be <code class="docutils literal"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code> for Python 3.2.
255
 
The <code class="docutils literal"><span class="pre">cpython-32</span></code> string comes from the current magic tag (see
256
 
<a class="reference internal" href="#imp.get_tag" title="imp.get_tag"><code class="xref py py-func docutils literal"><span class="pre">get_tag()</span></code></a>; if <code class="xref py py-attr docutils literal"><span class="pre">sys.implementation.cache_tag</span></code> is not defined then
257
 
<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). By passing in <code class="docutils literal"><span class="pre">True</span></code> or
258
 
<code class="docutils literal"><span class="pre">False</span></code> for <em>debug_override</em> you can override the system&#8217;s value for
259
 
<code class="docutils literal"><span class="pre">__debug__</span></code>, leading to optimized bytecode.</p>
260
 
<p><em>path</em> need not exist.</p>
261
 
<div class="versionchanged">
262
 
<p><span class="versionmodified">Changed in version 3.3: </span>If <code class="xref py py-attr docutils literal"><span class="pre">sys.implementation.cache_tag</span></code> is <code class="docutils literal"><span class="pre">None</span></code>, then
263
 
<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>
264
 
</div>
265
 
<div class="deprecated">
266
 
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="importlib.html#importlib.util.cache_from_source" title="importlib.util.cache_from_source"><code class="xref py py-func docutils literal"><span class="pre">importlib.util.cache_from_source()</span></code></a> instead.</p>
267
 
</div>
268
 
<div class="versionchanged">
269
 
<p><span class="versionmodified">Changed in version 3.5: </span>The <em>debug_override</em> parameter no longer creates a <code class="docutils literal"><span class="pre">.pyo</span></code> file.</p>
270
 
</div>
271
 
</dd></dl>
272
 
 
273
 
<dl class="function">
274
 
<dt id="imp.source_from_cache">
275
 
<code class="descclassname">imp.</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="#imp.source_from_cache" title="Permalink to this definition">¶</a></dt>
276
 
<dd><p>Given the <em>path</em> to a <span class="target" id="index-4"></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
277
 
file path.  For example, if <em>path</em> is
278
 
<code class="docutils literal"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code> the returned path would be
279
 
<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
280
 
to <span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> format, a <code class="docutils literal"><span class="pre">ValueError</span></code> is raised. If
281
 
<code class="xref py py-attr docutils literal"><span class="pre">sys.implementation.cache_tag</span></code> is not defined,
282
 
<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>
283
 
<div class="versionchanged">
284
 
<p><span class="versionmodified">Changed in version 3.3: </span>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> when
285
 
<code class="xref py py-attr docutils literal"><span class="pre">sys.implementation.cache_tag</span></code> is not defined.</p>
286
 
</div>
287
 
<div class="deprecated">
288
 
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="importlib.html#importlib.util.source_from_cache" title="importlib.util.source_from_cache"><code class="xref py py-func docutils literal"><span class="pre">importlib.util.source_from_cache()</span></code></a> instead.</p>
289
 
</div>
290
 
</dd></dl>
291
 
 
292
 
<dl class="function">
293
 
<dt id="imp.get_tag">
294
 
<code class="descclassname">imp.</code><code class="descname">get_tag</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imp.get_tag" title="Permalink to this definition">¶</a></dt>
295
 
<dd><p>Return the <span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> magic tag string matching this version of Python&#8217;s
296
 
magic number, as returned by <a class="reference internal" href="#imp.get_magic" title="imp.get_magic"><code class="xref py py-func docutils literal"><span class="pre">get_magic()</span></code></a>.</p>
297
 
<div class="deprecated">
298
 
<p><span class="versionmodified">Deprecated since version 3.4: </span>Use <code class="xref py py-attr docutils literal"><span class="pre">sys.implementation.cache_tag</span></code> directly starting
299
 
in Python 3.3.</p>
300
 
</div>
301
 
</dd></dl>
302
 
 
303
 
<p>The following functions help interact with the import system&#8217;s internal
304
 
locking mechanism.  Locking semantics of imports are an implementation
305
 
detail which may vary from release to release.  However, Python ensures
306
 
that circular imports work without any deadlocks.</p>
307
 
<dl class="function">
308
 
<dt id="imp.lock_held">
309
 
<code class="descclassname">imp.</code><code class="descname">lock_held</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imp.lock_held" title="Permalink to this definition">¶</a></dt>
310
 
<dd><p>Return <code class="docutils literal"><span class="pre">True</span></code> if the global import lock is currently held, else
311
 
<code class="docutils literal"><span class="pre">False</span></code>. On platforms without threads, always return <code class="docutils literal"><span class="pre">False</span></code>.</p>
312
 
<p>On platforms with threads, a thread executing an import first holds a
313
 
global import lock, then sets up a per-module lock for the rest of the
314
 
import.  This blocks other threads from importing the same module until
315
 
the original import completes, preventing other threads from seeing
316
 
incomplete module objects constructed by the original thread.  An
317
 
exception is made for circular imports, which by construction have to
318
 
expose an incomplete module object at some point.</p>
319
 
<div class="versionchanged">
320
 
<p><span class="versionmodified">Changed in version 3.3: </span>The locking scheme has changed to per-module locks for
321
 
the most part.  A global import lock is kept for some critical tasks,
322
 
such as initializing the per-module locks.</p>
323
 
</div>
324
 
<div class="deprecated">
325
 
<p><span class="versionmodified">Deprecated since version 3.4.</span></p>
326
 
</div>
327
 
</dd></dl>
328
 
 
329
 
<dl class="function">
330
 
<dt id="imp.acquire_lock">
331
 
<code class="descclassname">imp.</code><code class="descname">acquire_lock</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imp.acquire_lock" title="Permalink to this definition">¶</a></dt>
332
 
<dd><p>Acquire the interpreter&#8217;s global import lock for the current thread.
333
 
This lock should be used by import hooks to ensure thread-safety when
334
 
importing modules.</p>
335
 
<p>Once a thread has acquired the import lock, the same thread may acquire it
336
 
again without blocking; the thread must release it once for each time it has
337
 
acquired it.</p>
338
 
<p>On platforms without threads, this function does nothing.</p>
339
 
<div class="versionchanged">
340
 
<p><span class="versionmodified">Changed in version 3.3: </span>The locking scheme has changed to per-module locks for
341
 
the most part.  A global import lock is kept for some critical tasks,
342
 
such as initializing the per-module locks.</p>
343
 
</div>
344
 
<div class="deprecated">
345
 
<p><span class="versionmodified">Deprecated since version 3.4.</span></p>
346
 
</div>
347
 
</dd></dl>
348
 
 
349
 
<dl class="function">
350
 
<dt id="imp.release_lock">
351
 
<code class="descclassname">imp.</code><code class="descname">release_lock</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imp.release_lock" title="Permalink to this definition">¶</a></dt>
352
 
<dd><p>Release the interpreter&#8217;s global import lock. On platforms without
353
 
threads, this function does nothing.</p>
354
 
<div class="versionchanged">
355
 
<p><span class="versionmodified">Changed in version 3.3: </span>The locking scheme has changed to per-module locks for
356
 
the most part.  A global import lock is kept for some critical tasks,
357
 
such as initializing the per-module locks.</p>
358
 
</div>
359
 
<div class="deprecated">
360
 
<p><span class="versionmodified">Deprecated since version 3.4.</span></p>
361
 
</div>
362
 
</dd></dl>
363
 
 
364
 
<p>The following constants with integer values, defined in this module, are used
365
 
to indicate the search result of <a class="reference internal" href="#imp.find_module" title="imp.find_module"><code class="xref py py-func docutils literal"><span class="pre">find_module()</span></code></a>.</p>
366
 
<dl class="data">
367
 
<dt id="imp.PY_SOURCE">
368
 
<code class="descclassname">imp.</code><code class="descname">PY_SOURCE</code><a class="headerlink" href="#imp.PY_SOURCE" title="Permalink to this definition">¶</a></dt>
369
 
<dd><p>The module was found as a source file.</p>
370
 
<div class="deprecated">
371
 
<p><span class="versionmodified">Deprecated since version 3.3.</span></p>
372
 
</div>
373
 
</dd></dl>
374
 
 
375
 
<dl class="data">
376
 
<dt id="imp.PY_COMPILED">
377
 
<code class="descclassname">imp.</code><code class="descname">PY_COMPILED</code><a class="headerlink" href="#imp.PY_COMPILED" title="Permalink to this definition">¶</a></dt>
378
 
<dd><p>The module was found as a compiled code object file.</p>
379
 
<div class="deprecated">
380
 
<p><span class="versionmodified">Deprecated since version 3.3.</span></p>
381
 
</div>
382
 
</dd></dl>
383
 
 
384
 
<dl class="data">
385
 
<dt id="imp.C_EXTENSION">
386
 
<code class="descclassname">imp.</code><code class="descname">C_EXTENSION</code><a class="headerlink" href="#imp.C_EXTENSION" title="Permalink to this definition">¶</a></dt>
387
 
<dd><p>The module was found as dynamically loadable shared library.</p>
388
 
<div class="deprecated">
389
 
<p><span class="versionmodified">Deprecated since version 3.3.</span></p>
390
 
</div>
391
 
</dd></dl>
392
 
 
393
 
<dl class="data">
394
 
<dt id="imp.PKG_DIRECTORY">
395
 
<code class="descclassname">imp.</code><code class="descname">PKG_DIRECTORY</code><a class="headerlink" href="#imp.PKG_DIRECTORY" title="Permalink to this definition">¶</a></dt>
396
 
<dd><p>The module was found as a package directory.</p>
397
 
<div class="deprecated">
398
 
<p><span class="versionmodified">Deprecated since version 3.3.</span></p>
399
 
</div>
400
 
</dd></dl>
401
 
 
402
 
<dl class="data">
403
 
<dt id="imp.C_BUILTIN">
404
 
<code class="descclassname">imp.</code><code class="descname">C_BUILTIN</code><a class="headerlink" href="#imp.C_BUILTIN" title="Permalink to this definition">¶</a></dt>
405
 
<dd><p>The module was found as a built-in module.</p>
406
 
<div class="deprecated">
407
 
<p><span class="versionmodified">Deprecated since version 3.3.</span></p>
408
 
</div>
409
 
</dd></dl>
410
 
 
411
 
<dl class="data">
412
 
<dt id="imp.PY_FROZEN">
413
 
<code class="descclassname">imp.</code><code class="descname">PY_FROZEN</code><a class="headerlink" href="#imp.PY_FROZEN" title="Permalink to this definition">¶</a></dt>
414
 
<dd><p>The module was found as a frozen module.</p>
415
 
<div class="deprecated">
416
 
<p><span class="versionmodified">Deprecated since version 3.3.</span></p>
417
 
</div>
418
 
</dd></dl>
419
 
 
420
 
<dl class="class">
421
 
<dt id="imp.NullImporter">
422
 
<em class="property">class </em><code class="descclassname">imp.</code><code class="descname">NullImporter</code><span class="sig-paren">(</span><em>path_string</em><span class="sig-paren">)</span><a class="headerlink" href="#imp.NullImporter" title="Permalink to this definition">¶</a></dt>
423
 
<dd><p>The <a class="reference internal" href="#imp.NullImporter" title="imp.NullImporter"><code class="xref py py-class docutils literal"><span class="pre">NullImporter</span></code></a> type is a <span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> import hook that handles
424
 
non-directory path strings by failing to find any modules.  Calling this type
425
 
with an existing directory or empty string 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>.
426
 
Otherwise, a <a class="reference internal" href="#imp.NullImporter" title="imp.NullImporter"><code class="xref py py-class docutils literal"><span class="pre">NullImporter</span></code></a> instance is returned.</p>
427
 
<p>Instances have only one method:</p>
428
 
<dl class="method">
429
 
<dt id="imp.NullImporter.find_module">
430
 
<code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em><span class="optional">[</span>, <em>path</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imp.NullImporter.find_module" title="Permalink to this definition">¶</a></dt>
431
 
<dd><p>This method always returns <code class="docutils literal"><span class="pre">None</span></code>, indicating that the requested module could
432
 
not be found.</p>
433
 
</dd></dl>
434
 
 
435
 
<div class="versionchanged">
436
 
<p><span class="versionmodified">Changed in version 3.3: </span><code class="docutils literal"><span class="pre">None</span></code> is inserted into <code class="docutils literal"><span class="pre">sys.path_importer_cache</span></code> instead of an
437
 
instance of <a class="reference internal" href="#imp.NullImporter" title="imp.NullImporter"><code class="xref py py-class docutils literal"><span class="pre">NullImporter</span></code></a>.</p>
438
 
</div>
439
 
<div class="deprecated">
440
 
<p><span class="versionmodified">Deprecated since version 3.4: </span>Insert <code class="docutils literal"><span class="pre">None</span></code> into <code class="docutils literal"><span class="pre">sys.path_importer_cache</span></code> instead.</p>
441
 
</div>
442
 
</dd></dl>
443
 
 
444
 
<div class="section" id="examples">
445
 
<span id="examples-imp"></span><h2>36.2.1. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
446
 
<p>The following function emulates what was the standard import statement up to
447
 
Python 1.4 (no hierarchical module names).  (This <em>implementation</em> wouldn&#8217;t work
448
 
in that version, since <a class="reference internal" href="#imp.find_module" title="imp.find_module"><code class="xref py py-func docutils literal"><span class="pre">find_module()</span></code></a> has been extended and
449
 
<a class="reference internal" href="#imp.load_module" title="imp.load_module"><code class="xref py py-func docutils literal"><span class="pre">load_module()</span></code></a> has been added in 1.4.)</p>
450
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">imp</span>
451
 
<span class="kn">import</span> <span class="nn">sys</span>
452
 
 
453
 
<span class="k">def</span> <span class="nf">__import__</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="nb">globals</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="nb">locals</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">fromlist</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
454
 
    <span class="c1"># Fast path: see if the module has already been imported.</span>
455
 
    <span class="k">try</span><span class="p">:</span>
456
 
        <span class="k">return</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
457
 
    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
458
 
        <span class="k">pass</span>
459
 
 
460
 
    <span class="c1"># If any of the following calls raises an exception,</span>
461
 
    <span class="c1"># there&#39;s a problem we can&#39;t handle -- let the caller handle it.</span>
462
 
 
463
 
    <span class="n">fp</span><span class="p">,</span> <span class="n">pathname</span><span class="p">,</span> <span class="n">description</span> <span class="o">=</span> <span class="n">imp</span><span class="o">.</span><span class="n">find_module</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
464
 
 
465
 
    <span class="k">try</span><span class="p">:</span>
466
 
        <span class="k">return</span> <span class="n">imp</span><span class="o">.</span><span class="n">load_module</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">fp</span><span class="p">,</span> <span class="n">pathname</span><span class="p">,</span> <span class="n">description</span><span class="p">)</span>
467
 
    <span class="k">finally</span><span class="p">:</span>
468
 
        <span class="c1"># Since we may exit via an exception, close fp explicitly.</span>
469
 
        <span class="k">if</span> <span class="n">fp</span><span class="p">:</span>
470
 
            <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
471
 
</pre></div>
472
 
</div>
473
 
</div>
474
 
</div>
475
 
 
476
 
 
477
 
          </div>
478
 
        </div>
479
 
      </div>
480
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
481
 
        <div class="sphinxsidebarwrapper">
482
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
483
 
  <ul>
484
 
<li><a class="reference internal" href="#">36.2. <code class="docutils literal"><span class="pre">imp</span></code> &#8212; Access the <code class="docutils literal"><span class="pre">import</span></code> internals</a><ul>
485
 
<li><a class="reference internal" href="#examples">36.2.1. Examples</a></li>
486
 
</ul>
487
 
</li>
488
 
</ul>
489
 
 
490
 
  <h4>Previous topic</h4>
491
 
  <p class="topless"><a href="optparse.html"
492
 
                        title="previous chapter">36.1. <code class="docutils literal"><span class="pre">optparse</span></code> &#8212; Parser for command line options</a></p>
493
 
  <h4>Next topic</h4>
494
 
  <p class="topless"><a href="undoc.html"
495
 
                        title="next chapter">37. Undocumented Modules</a></p>
496
 
<h3>This Page</h3>
497
 
<ul class="this-page-menu">
498
 
  <li><a href="../bugs.html">Report a Bug</a></li>
499
 
  <li><a href="../_sources/library/imp.txt"
500
 
         rel="nofollow">Show Source</a></li>
501
 
</ul>
502
 
 
503
 
<div id="searchbox" style="display: none" role="search">
504
 
  <h3>Quick search</h3>
505
 
    <form class="search" action="../search.html" method="get">
506
 
      <input type="text" name="q" />
507
 
      <input type="submit" value="Go" />
508
 
      <input type="hidden" name="check_keywords" value="yes" />
509
 
      <input type="hidden" name="area" value="default" />
510
 
    </form>
511
 
    <p class="searchtip" style="font-size: 90%">
512
 
    Enter search terms or a module, class or function name.
513
 
    </p>
514
 
</div>
515
 
<script type="text/javascript">$('#searchbox').show(0);</script>
516
 
        </div>
517
 
      </div>
518
 
      <div class="clearer"></div>
519
 
    </div>  
520
 
    <div class="related" role="navigation" aria-label="related navigation">
521
 
      <h3>Navigation</h3>
522
 
      <ul>
523
 
        <li class="right" style="margin-right: 10px">
524
 
          <a href="../genindex.html" title="General Index"
525
 
             >index</a></li>
526
 
        <li class="right" >
527
 
          <a href="../py-modindex.html" title="Python Module Index"
528
 
             >modules</a> |</li>
529
 
        <li class="right" >
530
 
          <a href="undoc.html" title="37. Undocumented Modules"
531
 
             >next</a> |</li>
532
 
        <li class="right" >
533
 
          <a href="optparse.html" title="36.1. optparse — Parser for command line options"
534
 
             >previous</a> |</li>
535
 
        <li><img src="../_static/py.png" alt=""
536
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
537
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
538
 
        <li>
539
 
          <span class="version_switcher_placeholder">3.5.1</span>
540
 
          <a href="../index.html">Documentation </a> &raquo;
541
 
        </li>
542
 
 
543
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
544
 
          <li class="nav-item nav-item-2"><a href="superseded.html" >36. Superseded Modules</a> &raquo;</li> 
545
 
      </ul>
546
 
    </div>  
547
 
    <div class="footer">
548
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
549
 
    <br />
550
 
    The Python Software Foundation is a non-profit corporation.
551
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
552
 
    <br />
553
 
    Last updated on Jan 22, 2016.
554
 
    <a href="../bugs.html">Found a bug</a>?
555
 
    <br />
556
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
557
 
    </div>
558
 
 
559
 
  </body>
560
 
</html>
 
 
b'\\ No newline at end of file'