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