blob: 5a59f28a05cf74dbbf78cf7ce624c2d4e44ff72b [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`pkgutil` --- Package extension utility
3============================================
4
5.. module:: pkgutil
Georg Brandlc524cff2010-11-26 08:42:45 +00006 :synopsis: Utilities for the import system.
Georg Brandl116aa622007-08-15 14:28:22 +00007
Georg Brandlc524cff2010-11-26 08:42:45 +00008This module provides utilities for the import system, in particular package
9support.
Georg Brandl116aa622007-08-15 14:28:22 +000010
11
12.. function:: extend_path(path, name)
13
Georg Brandlc524cff2010-11-26 08:42:45 +000014 Extend the search path for the modules which comprise a package. Intended
15 use is to place the following code in a package's :file:`__init__.py`::
Georg Brandl116aa622007-08-15 14:28:22 +000016
17 from pkgutil import extend_path
18 __path__ = extend_path(__path__, __name__)
19
Georg Brandlc524cff2010-11-26 08:42:45 +000020 This will add to the package's ``__path__`` all subdirectories of directories
21 on ``sys.path`` named after the package. This is useful if one wants to
22 distribute different parts of a single logical package as multiple
23 directories.
Georg Brandl116aa622007-08-15 14:28:22 +000024
Georg Brandlc524cff2010-11-26 08:42:45 +000025 It also looks for :file:`\*.pkg` files beginning where ``*`` matches the
26 *name* argument. This feature is similar to :file:`\*.pth` files (see the
27 :mod:`site` module for more information), except that it doesn't special-case
28 lines starting with ``import``. A :file:`\*.pkg` file is trusted at face
29 value: apart from checking for duplicates, all entries found in a
30 :file:`\*.pkg` file are added to the path, regardless of whether they exist
31 on the filesystem. (This is a feature.)
Georg Brandl116aa622007-08-15 14:28:22 +000032
33 If the input path is not a list (as is the case for frozen packages) it is
34 returned unchanged. The input path is not modified; an extended copy is
35 returned. Items are only appended to the copy at the end.
36
Georg Brandlc524cff2010-11-26 08:42:45 +000037 It is assumed that :data:`sys.path` is a sequence. Items of :data:`sys.path`
38 that are not strings referring to existing directories are ignored. Unicode
39 items on :data:`sys.path` that cause errors when used as filenames may cause
40 this function to raise an exception (in line with :func:`os.path.isdir`
41 behavior).
42
43
44.. class:: ImpImporter(dirname=None)
45
46 :pep:`302` Importer that wraps Python's "classic" import algorithm.
47
48 If *dirname* is a string, a :pep:`302` importer is created that searches that
49 directory. If *dirname* is ``None``, a :pep:`302` importer is created that
50 searches the current :data:`sys.path`, plus any modules that are frozen or
51 built-in.
52
53 Note that :class:`ImpImporter` does not currently support being used by
54 placement on :data:`sys.meta_path`.
55
56
57.. class:: ImpLoader(fullname, file, filename, etc)
58
59 :pep:`302` Loader that wraps Python's "classic" import algorithm.
60
61
62.. function:: find_loader(fullname)
63
64 Find a :pep:`302` "loader" object for *fullname*.
65
66 If *fullname* contains dots, path must be the containing package's
67 ``__path__``. Returns ``None`` if the module cannot be found or imported.
68 This function uses :func:`iter_importers`, and is thus subject to the same
69 limitations regarding platform-specific special import locations such as the
70 Windows registry.
71
72
73.. function:: get_importer(path_item)
74
75 Retrieve a :pep:`302` importer for the given *path_item*.
76
77 The returned importer is cached in :data:`sys.path_importer_cache` if it was
78 newly created by a path hook.
79
80 If there is no importer, a wrapper around the basic import machinery is
Éric Araujob7ae2092010-12-15 22:38:50 +000081 returned. This wrapper is never inserted into the importer cache (``None``
82 is inserted instead).
Georg Brandlc524cff2010-11-26 08:42:45 +000083
84 The cache (or part of it) can be cleared manually if a rescan of
85 :data:`sys.path_hooks` is necessary.
86
87
88.. function:: get_loader(module_or_name)
89
90 Get a :pep:`302` "loader" object for *module_or_name*.
91
92 If the module or package is accessible via the normal import mechanism, a
93 wrapper around the relevant part of that machinery is returned. Returns
94 ``None`` if the module cannot be found or imported. If the named module is
95 not already imported, its containing package (if any) is imported, in order
96 to establish the package ``__path__``.
97
98 This function uses :func:`iter_importers`, and is thus subject to the same
99 limitations regarding platform-specific special import locations such as the
100 Windows registry.
101
102
103.. function:: iter_importers(fullname='')
104
105 Yield :pep:`302` importers for the given module name.
106
107 If fullname contains a '.', the importers will be for the package containing
108 fullname, otherwise they will be importers for :data:`sys.meta_path`,
109 :data:`sys.path`, and Python's "classic" import machinery, in that order. If
110 the named module is in a package, that package is imported as a side effect
111 of invoking this function.
112
113 Non-:pep:`302` mechanisms (e.g. the Windows registry) used by the standard
114 import machinery to find files in alternative locations are partially
115 supported, but are searched *after* :data:`sys.path`. Normally, these
116 locations are searched *before* :data:`sys.path`, preventing :data:`sys.path`
117 entries from shadowing them.
118
119 For this to cause a visible difference in behaviour, there must be a module
120 or package name that is accessible via both :data:`sys.path` and one of the
121 non-:pep:`302` file system mechanisms. In this case, the emulation will find
122 the former version, while the builtin import mechanism will find the latter.
123
124 Items of the following types can be affected by this discrepancy:
125 ``imp.C_EXTENSION``, ``imp.PY_SOURCE``, ``imp.PY_COMPILED``,
126 ``imp.PKG_DIRECTORY``.
127
128
129.. function:: iter_modules(path=None, prefix='')
130
131 Yields ``(module_loader, name, ispkg)`` for all submodules on *path*, or, if
132 path is ``None``, all top-level modules on ``sys.path``.
133
134 *path* should be either ``None`` or a list of paths to look for modules in.
135
136 *prefix* is a string to output on the front of every module name on output.
137
138
139.. function:: walk_packages(path=None, prefix='', onerror=None)
140
141 Yields ``(module_loader, name, ispkg)`` for all modules recursively on
142 *path*, or, if path is ``None``, all accessible modules.
143
144 *path* should be either ``None`` or a list of paths to look for modules in.
145
146 *prefix* is a string to output on the front of every module name on output.
147
148 Note that this function must import all *packages* (*not* all modules!) on
149 the given *path*, in order to access the ``__path__`` attribute to find
150 submodules.
151
152 *onerror* is a function which gets called with one argument (the name of the
153 package which was being imported) if any exception occurs while trying to
154 import a package. If no *onerror* function is supplied, :exc:`ImportError`\s
155 are caught and ignored, while all other exceptions are propagated,
156 terminating the search.
157
158 Examples::
159
160 # list all modules python can access
161 walk_packages()
162
163 # list all submodules of ctypes
164 walk_packages(ctypes.__path__, ctypes.__name__ + '.')
165
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Christian Heimesdae2a892008-04-19 00:55:37 +0000167.. function:: get_data(package, resource)
168
169 Get a resource from a package.
170
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000171 This is a wrapper for the PEP 302 loader :func:`get_data` API. The package
Christian Heimesdae2a892008-04-19 00:55:37 +0000172 argument should be the name of a package, in standard module format
173 (foo.bar). The resource argument should be in the form of a relative
174 filename, using ``/`` as the path separator. The parent directory name
175 ``..`` is not allowed, and nor is a rooted name (starting with a ``/``).
176
Georg Brandlc524cff2010-11-26 08:42:45 +0000177 The function returns a binary string that is the contents of the specified
178 resource.
Christian Heimesdae2a892008-04-19 00:55:37 +0000179
180 For packages located in the filesystem, which have already been imported,
181 this is the rough equivalent of::
182
Georg Brandlc524cff2010-11-26 08:42:45 +0000183 d = os.path.dirname(sys.modules[package].__file__)
184 data = open(os.path.join(d, resource), 'rb').read()
Christian Heimesdae2a892008-04-19 00:55:37 +0000185
186 If the package cannot be located or loaded, or it uses a PEP 302 loader
Georg Brandlc524cff2010-11-26 08:42:45 +0000187 which does not support :func:`get_data`, then ``None`` is returned.