Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`pkgutil` --- Package extension utility |
| 3 | ============================================ |
| 4 | |
| 5 | .. module:: pkgutil |
| 6 | :synopsis: Utilities to support extension of packages. |
| 7 | |
| 8 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | This module provides a single function: |
| 10 | |
| 11 | |
| 12 | .. function:: extend_path(path, name) |
| 13 | |
| 14 | Extend the search path for the modules which comprise a package. Intended use is |
| 15 | to place the following code in a package's :file:`__init__.py`:: |
| 16 | |
| 17 | from pkgutil import extend_path |
| 18 | __path__ = extend_path(__path__, __name__) |
| 19 | |
| 20 | This will add to the package's ``__path__`` all subdirectories of directories on |
| 21 | ``sys.path`` named after the package. This is useful if one wants to distribute |
| 22 | different parts of a single logical package as multiple directories. |
| 23 | |
| 24 | It also looks for :file:`\*.pkg` files beginning where ``*`` matches the *name* |
| 25 | argument. This feature is similar to :file:`\*.pth` files (see the :mod:`site` |
| 26 | module for more information), except that it doesn't special-case lines starting |
| 27 | with ``import``. A :file:`\*.pkg` file is trusted at face value: apart from |
| 28 | checking for duplicates, all entries found in a :file:`\*.pkg` file are added to |
| 29 | the path, regardless of whether they exist on the filesystem. (This is a |
| 30 | feature.) |
| 31 | |
| 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 | |
| 36 | It is assumed that ``sys.path`` is a sequence. Items of ``sys.path`` that are |
| 37 | not (Unicode or 8-bit) strings referring to existing directories are ignored. |
| 38 | Unicode items on ``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 | |