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