1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>31.5. pkgutil — Package extension utility — Python 2.7.12 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
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 2.7.12 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 2.7.12 documentation" href="../contents.html" />
33
<link rel="up" title="31. Importing Modules" href="modules.html" />
34
<link rel="next" title="31.6. modulefinder — Find modules used by a script" href="modulefinder.html" />
35
<link rel="prev" title="31.4. zipimport — Import modules from Zip archives" href="zipimport.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>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="modulefinder.html" title="31.6. modulefinder — Find modules used by a script"
55
accesskey="N">next</a> |</li>
57
<a href="zipimport.html" title="31.4. zipimport — Import modules from Zip archives"
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> »</li>
63
<span class="version_switcher_placeholder">2.7.12</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">31. Importing Modules</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-pkgutil">
78
<span id="pkgutil-package-extension-utility"></span><h1>31.5. <a class="reference internal" href="#module-pkgutil" title="pkgutil: Utilities for the import system."><code class="xref py py-mod docutils literal"><span class="pre">pkgutil</span></code></a> — Package extension utility<a class="headerlink" href="#module-pkgutil" title="Permalink to this headline">¶</a></h1>
79
<div class="versionadded">
80
<p><span class="versionmodified">New in version 2.3.</span></p>
82
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/pkgutil.py">Lib/pkgutil.py</a></p>
83
<hr class="docutils" />
84
<p>This module provides utilities for the import system, in particular package
87
<dt id="pkgutil.extend_path">
88
<code class="descclassname">pkgutil.</code><code class="descname">extend_path</code><span class="sig-paren">(</span><em>path</em>, <em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.extend_path" title="Permalink to this definition">¶</a></dt>
89
<dd><p>Extend the search path for the modules which comprise a package. Intended
90
use is to place the following code in a package’s <code class="file docutils literal"><span class="pre">__init__.py</span></code>:</p>
91
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pkgutil</span> <span class="kn">import</span> <span class="n">extend_path</span>
92
<span class="n">__path__</span> <span class="o">=</span> <span class="n">extend_path</span><span class="p">(</span><span class="n">__path__</span><span class="p">,</span> <span class="n">__name__</span><span class="p">)</span>
95
<p>This will add to the package’s <code class="docutils literal"><span class="pre">__path__</span></code> all subdirectories of directories
96
on <code class="docutils literal"><span class="pre">sys.path</span></code> named after the package. This is useful if one wants to
97
distribute different parts of a single logical package as multiple
99
<p>It also looks for <code class="file docutils literal"><span class="pre">*.pkg</span></code> files beginning where <code class="docutils literal"><span class="pre">*</span></code> matches the
100
<em>name</em> argument. This feature is similar to <code class="file docutils literal"><span class="pre">*.pth</span></code> files (see the
101
<a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal"><span class="pre">site</span></code></a> module for more information), except that it doesn’t special-case
102
lines starting with <code class="docutils literal"><span class="pre">import</span></code>. A <code class="file docutils literal"><span class="pre">*.pkg</span></code> file is trusted at face
103
value: apart from checking for duplicates, all entries found in a
104
<code class="file docutils literal"><span class="pre">*.pkg</span></code> file are added to the path, regardless of whether they exist
105
on the filesystem. (This is a feature.)</p>
106
<p>If the input path is not a list (as is the case for frozen packages) it is
107
returned unchanged. The input path is not modified; an extended copy is
108
returned. Items are only appended to the copy at the end.</p>
109
<p>It is assumed that <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> is a sequence. Items of <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>
110
that are not (Unicode or 8-bit) strings referring to existing directories are
111
ignored. Unicode items 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> that cause errors when used as
112
filenames may cause this function to raise an exception (in line with
113
<a class="reference internal" href="os.path.html#os.path.isdir" title="os.path.isdir"><code class="xref py py-func docutils literal"><span class="pre">os.path.isdir()</span></code></a> behavior).</p>
117
<dt id="pkgutil.ImpImporter">
118
<em class="property">class </em><code class="descclassname">pkgutil.</code><code class="descname">ImpImporter</code><span class="sig-paren">(</span><em>dirname=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.ImpImporter" title="Permalink to this definition">¶</a></dt>
119
<dd><p><span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> Importer that wraps Python’s “classic” import algorithm.</p>
120
<p>If <em>dirname</em> is a string, a <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> importer is created that searches that
121
directory. If <em>dirname</em> is <code class="docutils literal"><span class="pre">None</span></code>, a <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> importer is created that
122
searches the current <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>, plus any modules that are frozen or
124
<p>Note that <a class="reference internal" href="#pkgutil.ImpImporter" title="pkgutil.ImpImporter"><code class="xref py py-class docutils literal"><span class="pre">ImpImporter</span></code></a> does not currently support being used by
125
placement 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>
129
<dt id="pkgutil.ImpLoader">
130
<em class="property">class </em><code class="descclassname">pkgutil.</code><code class="descname">ImpLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>file</em>, <em>filename</em>, <em>etc</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.ImpLoader" title="Permalink to this definition">¶</a></dt>
131
<dd><p><span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> Loader that wraps Python’s “classic” import algorithm.</p>
134
<dl class="function">
135
<dt id="pkgutil.find_loader">
136
<code class="descclassname">pkgutil.</code><code class="descname">find_loader</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.find_loader" title="Permalink to this definition">¶</a></dt>
137
<dd><p>Find a <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> “loader” object for <em>fullname</em>.</p>
138
<p>If <em>fullname</em> contains dots, path must be the containing package’s
139
<code class="docutils literal"><span class="pre">__path__</span></code>. Returns <code class="docutils literal"><span class="pre">None</span></code> if the module cannot be found or imported.
140
This function uses <a class="reference internal" href="#pkgutil.iter_importers" title="pkgutil.iter_importers"><code class="xref py py-func docutils literal"><span class="pre">iter_importers()</span></code></a>, and is thus subject to the same
141
limitations regarding platform-specific special import locations such as the
142
Windows registry.</p>
145
<dl class="function">
146
<dt id="pkgutil.get_importer">
147
<code class="descclassname">pkgutil.</code><code class="descname">get_importer</code><span class="sig-paren">(</span><em>path_item</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.get_importer" title="Permalink to this definition">¶</a></dt>
148
<dd><p>Retrieve a <span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> importer for the given <em>path_item</em>.</p>
149
<p>The returned importer is cached 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> if it was
150
newly created by a path hook.</p>
151
<p>If there is no importer, a wrapper around the basic import machinery is
152
returned. This wrapper is never inserted into the importer cache (<code class="docutils literal"><span class="pre">None</span></code>
153
is inserted instead).</p>
154
<p>The cache (or part of it) can be cleared manually if a rescan of
155
<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 necessary.</p>
158
<dl class="function">
159
<dt id="pkgutil.get_loader">
160
<code class="descclassname">pkgutil.</code><code class="descname">get_loader</code><span class="sig-paren">(</span><em>module_or_name</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.get_loader" title="Permalink to this definition">¶</a></dt>
161
<dd><p>Get a <span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> “loader” object for <em>module_or_name</em>.</p>
162
<p>If the module or package is accessible via the normal import mechanism, a
163
wrapper around the relevant part of that machinery is returned. Returns
164
<code class="docutils literal"><span class="pre">None</span></code> if the module cannot be found or imported. If the named module is
165
not already imported, its containing package (if any) is imported, in order
166
to establish the package <code class="docutils literal"><span class="pre">__path__</span></code>.</p>
167
<p>This function uses <a class="reference internal" href="#pkgutil.iter_importers" title="pkgutil.iter_importers"><code class="xref py py-func docutils literal"><span class="pre">iter_importers()</span></code></a>, and is thus subject to the same
168
limitations regarding platform-specific special import locations such as the
169
Windows registry.</p>
172
<dl class="function">
173
<dt id="pkgutil.iter_importers">
174
<code class="descclassname">pkgutil.</code><code class="descname">iter_importers</code><span class="sig-paren">(</span><em>fullname=''</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.iter_importers" title="Permalink to this definition">¶</a></dt>
175
<dd><p>Yield <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> importers for the given module name.</p>
176
<p>If fullname contains a ‘.’, the importers will be for the package containing
177
fullname, otherwise they will be importers for <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>,
178
<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 Python’s “classic” import machinery, in that order. If
179
the named module is in a package, that package is imported as a side effect
180
of invoking this function.</p>
181
<p>Non-<span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> mechanisms (e.g. the Windows registry) used by the standard
182
import machinery to find files in alternative locations are partially
183
supported, but are searched <em>after</em> <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>. Normally, these
184
locations are searched <em>before</em> <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>, preventing <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>
185
entries from shadowing them.</p>
186
<p>For this to cause a visible difference in behaviour, there must be a module
187
or package name that is accessible via both <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 one of the
188
non-<span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> file system mechanisms. In this case, the emulation will find
189
the former version, while the builtin import mechanism will find the latter.</p>
190
<p>Items of the following types can be affected by this discrepancy:
191
<code class="docutils literal"><span class="pre">imp.C_EXTENSION</span></code>, <code class="docutils literal"><span class="pre">imp.PY_SOURCE</span></code>, <code class="docutils literal"><span class="pre">imp.PY_COMPILED</span></code>,
192
<code class="docutils literal"><span class="pre">imp.PKG_DIRECTORY</span></code>.</p>
195
<dl class="function">
196
<dt id="pkgutil.iter_modules">
197
<code class="descclassname">pkgutil.</code><code class="descname">iter_modules</code><span class="sig-paren">(</span><em>path=None</em>, <em>prefix=''</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.iter_modules" title="Permalink to this definition">¶</a></dt>
198
<dd><p>Yields <code class="docutils literal"><span class="pre">(module_loader,</span> <span class="pre">name,</span> <span class="pre">ispkg)</span></code> for all submodules on <em>path</em>, or, if
199
path is <code class="docutils literal"><span class="pre">None</span></code>, all top-level modules on <code class="docutils literal"><span class="pre">sys.path</span></code>.</p>
200
<p><em>path</em> should be either <code class="docutils literal"><span class="pre">None</span></code> or a list of paths to look for modules in.</p>
201
<p><em>prefix</em> is a string to output on the front of every module name on output.</p>
204
<dl class="function">
205
<dt id="pkgutil.walk_packages">
206
<code class="descclassname">pkgutil.</code><code class="descname">walk_packages</code><span class="sig-paren">(</span><em>path=None</em>, <em>prefix=''</em>, <em>onerror=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.walk_packages" title="Permalink to this definition">¶</a></dt>
207
<dd><p>Yields <code class="docutils literal"><span class="pre">(module_loader,</span> <span class="pre">name,</span> <span class="pre">ispkg)</span></code> for all modules recursively on
208
<em>path</em>, or, if path is <code class="docutils literal"><span class="pre">None</span></code>, all accessible modules.</p>
209
<p><em>path</em> should be either <code class="docutils literal"><span class="pre">None</span></code> or a list of paths to look for modules in.</p>
210
<p><em>prefix</em> is a string to output on the front of every module name on output.</p>
211
<p>Note that this function must import all <em>packages</em> (<em>not</em> all modules!) on
212
the given <em>path</em>, in order to access the <code class="docutils literal"><span class="pre">__path__</span></code> attribute to find
214
<p><em>onerror</em> is a function which gets called with one argument (the name of the
215
package which was being imported) if any exception occurs while trying to
216
import a package. If no <em>onerror</em> function is supplied, <a class="reference internal" href="exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a>s
217
are caught and ignored, while all other exceptions are propagated,
218
terminating the search.</p>
220
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># list all modules python can access</span>
221
<span class="n">walk_packages</span><span class="p">()</span>
223
<span class="c1"># list all submodules of ctypes</span>
224
<span class="n">walk_packages</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">__path__</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">__name__</span> <span class="o">+</span> <span class="s1">'.'</span><span class="p">)</span>
229
<dl class="function">
230
<dt id="pkgutil.get_data">
231
<code class="descclassname">pkgutil.</code><code class="descname">get_data</code><span class="sig-paren">(</span><em>package</em>, <em>resource</em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.get_data" title="Permalink to this definition">¶</a></dt>
232
<dd><p>Get a resource from a package.</p>
233
<p>This is a wrapper for the <span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> loader <a class="reference internal" href="#pkgutil.get_data" title="pkgutil.get_data"><code class="xref py py-func docutils literal"><span class="pre">get_data()</span></code></a> API. The
234
<em>package</em> argument should be the name of a package, in standard module format
235
(<code class="docutils literal"><span class="pre">foo.bar</span></code>). The <em>resource</em> argument should be in the form of a relative
236
filename, using <code class="docutils literal"><span class="pre">/</span></code> as the path separator. The parent directory name
237
<code class="docutils literal"><span class="pre">..</span></code> is not allowed, and nor is a rooted name (starting with a <code class="docutils literal"><span class="pre">/</span></code>).</p>
238
<p>The function returns a binary string that is the contents of the specified
240
<p>For packages located in the filesystem, which have already been imported,
241
this is the rough equivalent of:</p>
242
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">d</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">package</span><span class="p">]</span><span class="o">.</span><span class="n">__file__</span><span class="p">)</span>
243
<span class="n">data</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">resource</span><span class="p">),</span> <span class="s1">'rb'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
246
<p>If the package cannot be located or loaded, or it uses a <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> loader
247
which does not support <a class="reference internal" href="#pkgutil.get_data" title="pkgutil.get_data"><code class="xref py py-func docutils literal"><span class="pre">get_data()</span></code></a>, then <code class="docutils literal"><span class="pre">None</span></code> is returned.</p>
248
<div class="versionadded">
249
<p><span class="versionmodified">New in version 2.6.</span></p>
259
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
260
<div class="sphinxsidebarwrapper">
261
<h4>Previous topic</h4>
262
<p class="topless"><a href="zipimport.html"
263
title="previous chapter">31.4. <code class="docutils literal"><span class="pre">zipimport</span></code> — Import modules from Zip archives</a></p>
265
<p class="topless"><a href="modulefinder.html"
266
title="next chapter">31.6. <code class="docutils literal"><span class="pre">modulefinder</span></code> — Find modules used by a script</a></p>
268
<ul class="this-page-menu">
269
<li><a href="../bugs.html">Report a Bug</a></li>
270
<li><a href="../_sources/library/pkgutil.txt"
271
rel="nofollow">Show Source</a></li>
274
<div id="searchbox" style="display: none" role="search">
275
<h3>Quick search</h3>
276
<form class="search" action="../search.html" method="get">
277
<input type="text" name="q" />
278
<input type="submit" value="Go" />
279
<input type="hidden" name="check_keywords" value="yes" />
280
<input type="hidden" name="area" value="default" />
282
<p class="searchtip" style="font-size: 90%">
283
Enter search terms or a module, class or function name.
286
<script type="text/javascript">$('#searchbox').show(0);</script>
289
<div class="clearer"></div>
291
<div class="related" role="navigation" aria-label="related navigation">
294
<li class="right" style="margin-right: 10px">
295
<a href="../genindex.html" title="General Index"
298
<a href="../py-modindex.html" title="Python Module Index"
301
<a href="modulefinder.html" title="31.6. modulefinder — Find modules used by a script"
304
<a href="zipimport.html" title="31.4. zipimport — Import modules from Zip archives"
306
<li><img src="../_static/py.png" alt=""
307
style="vertical-align: middle; margin-top: -1px"/></li>
308
<li><a href="https://www.python.org/">Python</a> »</li>
310
<span class="version_switcher_placeholder">2.7.12</span>
311
<a href="../index.html">Documentation</a> »
314
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
315
<li class="nav-item nav-item-2"><a href="modules.html" >31. Importing Modules</a> »</li>
319
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
321
The Python Software Foundation is a non-profit corporation.
322
<a href="https://www.python.org/psf/donations/">Please donate.</a>
324
Last updated on Sep 20, 2016.
325
<a href="../bugs.html">Found a bug</a>?
327
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'