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