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