Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`pkgutil` --- Package extension utility |
| 2 | ============================================ |
| 3 | |
| 4 | .. module:: pkgutil |
| 5 | :synopsis: Utilities to support extension of packages. |
| 6 | |
| 7 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 8 | This module provides functions to manipulate packages: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
| 10 | |
| 11 | .. function:: extend_path(path, name) |
| 12 | |
| 13 | Extend the search path for the modules which comprise a package. Intended use is |
| 14 | to place the following code in a package's :file:`__init__.py`:: |
| 15 | |
| 16 | from pkgutil import extend_path |
| 17 | __path__ = extend_path(__path__, __name__) |
| 18 | |
| 19 | This will add to the package's ``__path__`` all subdirectories of directories on |
| 20 | ``sys.path`` named after the package. This is useful if one wants to distribute |
| 21 | different parts of a single logical package as multiple directories. |
| 22 | |
| 23 | It also looks for :file:`\*.pkg` files beginning where ``*`` matches the *name* |
| 24 | argument. This feature is similar to :file:`\*.pth` files (see the :mod:`site` |
| 25 | module for more information), except that it doesn't special-case lines starting |
| 26 | with ``import``. A :file:`\*.pkg` file is trusted at face value: apart from |
| 27 | checking for duplicates, all entries found in a :file:`\*.pkg` file are added to |
| 28 | the path, regardless of whether they exist on the filesystem. (This is a |
| 29 | feature.) |
| 30 | |
| 31 | If the input path is not a list (as is the case for frozen packages) it is |
| 32 | returned unchanged. The input path is not modified; an extended copy is |
| 33 | returned. Items are only appended to the copy at the end. |
| 34 | |
| 35 | It is assumed that ``sys.path`` is a sequence. Items of ``sys.path`` that are |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 36 | not strings referring to existing directories are ignored. Unicode items on |
| 37 | ``sys.path`` that cause errors when used as filenames may cause this function |
| 38 | to raise an exception (in line with :func:`os.path.isdir` behavior). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 40 | .. function:: get_data(package, resource) |
| 41 | |
| 42 | Get a resource from a package. |
| 43 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 44 | This is a wrapper for the PEP 302 loader :func:`get_data` API. The package |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 45 | argument should be the name of a package, in standard module format |
| 46 | (foo.bar). The resource argument should be in the form of a relative |
| 47 | filename, using ``/`` as the path separator. The parent directory name |
| 48 | ``..`` is not allowed, and nor is a rooted name (starting with a ``/``). |
| 49 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 50 | The function returns a binary string that is the contents of the |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 51 | specified resource. |
| 52 | |
| 53 | For packages located in the filesystem, which have already been imported, |
| 54 | this is the rough equivalent of:: |
| 55 | |
| 56 | d = os.path.dirname(sys.modules[package].__file__) |
| 57 | data = open(os.path.join(d, resource), 'rb').read() |
| 58 | |
| 59 | If the package cannot be located or loaded, or it uses a PEP 302 loader |
| 60 | which does not support :func:`get_data`, then None is returned. |