Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame^] | 1 | |
| 2 | :mod:`zipimport` --- Import modules from Zip archives |
| 3 | ===================================================== |
| 4 | |
| 5 | .. module:: zipimport |
| 6 | :synopsis: support for importing Python modules from ZIP archives. |
| 7 | .. moduleauthor:: Just van Rossum <just@letterror.com> |
| 8 | |
| 9 | |
| 10 | .. versionadded:: 2.3 |
| 11 | |
| 12 | This module adds the ability to import Python modules (:file:`\*.py`, |
| 13 | :file:`\*.py[co]`) and packages from ZIP-format archives. It is usually not |
| 14 | needed to use the :mod:`zipimport` module explicitly; it is automatically used |
| 15 | by the builtin :keyword:`import` mechanism for ``sys.path`` items that are paths |
| 16 | to ZIP archives. |
| 17 | |
| 18 | Typically, ``sys.path`` is a list of directory names as strings. This module |
| 19 | also allows an item of ``sys.path`` to be a string naming a ZIP file archive. |
| 20 | The ZIP archive can contain a subdirectory structure to support package imports, |
| 21 | and a path within the archive can be specified to only import from a |
| 22 | subdirectory. For example, the path :file:`/tmp/example.zip/lib/` would only |
| 23 | import from the :file:`lib/` subdirectory within the archive. |
| 24 | |
| 25 | Any files may be present in the ZIP archive, but only files :file:`.py` and |
| 26 | :file:`.py[co]` are available for import. ZIP import of dynamic modules |
| 27 | (:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains |
| 28 | :file:`.py` files, Python will not attempt to modify the archive by adding the |
| 29 | corresponding :file:`.pyc` or :file:`.pyo` file, meaning that if a ZIP archive |
| 30 | doesn't contain :file:`.pyc` files, importing may be rather slow. |
| 31 | |
| 32 | Using the built-in :func:`reload` function will fail if called on a module |
| 33 | loaded from a ZIP archive; it is unlikely that :func:`reload` would be needed, |
| 34 | since this would imply that the ZIP has been altered during runtime. |
| 35 | |
| 36 | The available attributes of this module are: |
| 37 | |
| 38 | |
| 39 | .. exception:: ZipImportError |
| 40 | |
| 41 | Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`, |
| 42 | so it can be caught as :exc:`ImportError`, too. |
| 43 | |
| 44 | |
| 45 | .. class:: zipimporter |
| 46 | |
| 47 | The class for importing ZIP files. See section :ref:`zipimporter-objects` |
| 48 | for constructor details. |
| 49 | |
| 50 | |
| 51 | .. seealso:: |
| 52 | |
| 53 | `PKZIP Application Note <http://www.pkware.com/business_and_developers/developer/appnote/>`_ |
| 54 | Documentation on the ZIP file format by Phil Katz, the creator of the format and |
| 55 | algorithms used. |
| 56 | |
| 57 | :pep:`0273` - Import Modules from Zip Archives |
| 58 | Written by James C. Ahlstrom, who also provided an implementation. Python 2.3 |
| 59 | follows the specification in PEP 273, but uses an implementation written by Just |
| 60 | van Rossum that uses the import hooks described in PEP 302. |
| 61 | |
| 62 | :pep:`0302` - New Import Hooks |
| 63 | The PEP to add the import hooks that help this module work. |
| 64 | |
| 65 | |
| 66 | .. _zipimporter-objects: |
| 67 | |
| 68 | zipimporter Objects |
| 69 | ------------------- |
| 70 | |
| 71 | |
| 72 | .. class:: zipimporter(archivepath) |
| 73 | |
| 74 | Create a new zipimporter instance. *archivepath* must be a path to a zipfile. |
| 75 | :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP |
| 76 | archive. |
| 77 | |
| 78 | |
| 79 | .. method:: zipimporter.find_module(fullname[, path]) |
| 80 | |
| 81 | Search for a module specified by *fullname*. *fullname* must be the fully |
| 82 | qualified (dotted) module name. It returns the zipimporter instance itself if |
| 83 | the module was found, or :const:`None` if it wasn't. The optional *path* |
| 84 | argument is ignored---it's there for compatibility with the importer protocol. |
| 85 | |
| 86 | |
| 87 | .. method:: zipimporter.get_code(fullname) |
| 88 | |
| 89 | Return the code object for the specified module. Raise :exc:`ZipImportError` if |
| 90 | the module couldn't be found. |
| 91 | |
| 92 | |
| 93 | .. method:: zipimporter.get_data(pathname) |
| 94 | |
| 95 | Return the data associated with *pathname*. Raise :exc:`IOError` if the file |
| 96 | wasn't found. |
| 97 | |
| 98 | |
| 99 | .. method:: zipimporter.get_source(fullname) |
| 100 | |
| 101 | Return the source code for the specified module. Raise :exc:`ZipImportError` if |
| 102 | the module couldn't be found, return :const:`None` if the archive does contain |
| 103 | the module, but has no source for it. |
| 104 | |
| 105 | |
| 106 | .. method:: zipimporter.is_package(fullname) |
| 107 | |
| 108 | Return True if the module specified by *fullname* is a package. Raise |
| 109 | :exc:`ZipImportError` if the module couldn't be found. |
| 110 | |
| 111 | |
| 112 | .. method:: zipimporter.load_module(fullname) |
| 113 | |
| 114 | Load the module specified by *fullname*. *fullname* must be the fully qualified |
| 115 | (dotted) module name. It returns the imported module, or raises |
| 116 | :exc:`ZipImportError` if it wasn't found. |
| 117 | |
| 118 | |
| 119 | Examples |
| 120 | -------- |
| 121 | |
| 122 | .. _zipimport-examples: |
| 123 | |
| 124 | Here is an example that imports a module from a ZIP archive - note that the |
| 125 | :mod:`zipimport` module is not explicitly used. :: |
| 126 | |
| 127 | $ unzip -l /tmp/example.zip |
| 128 | Archive: /tmp/example.zip |
| 129 | Length Date Time Name |
| 130 | -------- ---- ---- ---- |
| 131 | 8467 11-26-02 22:30 jwzthreading.py |
| 132 | -------- ------- |
| 133 | 8467 1 file |
| 134 | $ ./python |
| 135 | Python 2.3 (#1, Aug 1 2003, 19:54:32) |
| 136 | >>> import sys |
| 137 | >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path |
| 138 | >>> import jwzthreading |
| 139 | >>> jwzthreading.__file__ |
| 140 | '/tmp/example.zip/jwzthreading.py' |
| 141 | |