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