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