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 |
Sanchit Khurana | f8a6316 | 2019-11-26 03:47:59 +0530 | [diff] [blame] | 5 | :synopsis: Support for importing Python modules from ZIP archives. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Just van Rossum <just@letterror.com> |
| 8 | |
Xtreak | e307e5c | 2019-05-15 21:48:35 +0530 | [diff] [blame] | 9 | **Source code:** :source:`Lib/zipimport.py` |
| 10 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | This module adds the ability to import Python modules (:file:`\*.py`, |
Xiang Zhang | 0710d75 | 2017-03-11 13:02:52 +0800 | [diff] [blame] | 14 | :file:`\*.pyc`) and packages from ZIP-format archives. It is usually not |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | needed to use the :mod:`zipimport` module explicitly; it is automatically used |
Ezio Melotti | 2d826e8 | 2011-06-25 19:40:06 +0300 | [diff] [blame] | 16 | by the built-in :keyword:`import` mechanism for :data:`sys.path` items that are paths |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | to ZIP archives. |
| 18 | |
Ezio Melotti | 2d826e8 | 2011-06-25 19:40:06 +0300 | [diff] [blame] | 19 | Typically, :data:`sys.path` is a list of directory names as strings. This module |
| 20 | also allows an item of :data:`sys.path` to be a string naming a ZIP file archive. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | The ZIP archive can contain a subdirectory structure to support package imports, |
| 22 | and a path within the archive can be specified to only import from a |
Petri Lehtinen | 9f74c6c | 2013-02-23 19:26:56 +0100 | [diff] [blame] | 23 | subdirectory. For example, the path :file:`example.zip/lib/` would only |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | import from the :file:`lib/` subdirectory within the archive. |
| 25 | |
| 26 | Any files may be present in the ZIP archive, but only files :file:`.py` and |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 27 | :file:`.pyc` are available for import. ZIP import of dynamic modules |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | (:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains |
| 29 | :file:`.py` files, Python will not attempt to modify the archive by adding the |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 30 | corresponding :file:`.pyc` file, meaning that if a ZIP archive |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | doesn't contain :file:`.pyc` files, importing may be rather slow. |
| 32 | |
Zackery Spytz | 5a5ce06 | 2018-09-25 13:15:47 -0600 | [diff] [blame] | 33 | .. versionchanged:: 3.8 |
| 34 | Previously, ZIP archives with an archive comment were not supported. |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 35 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | .. seealso:: |
| 37 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 38 | `PKZIP Application Note <https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT>`_ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | Documentation on the ZIP file format by Phil Katz, the creator of the format and |
| 40 | algorithms used. |
| 41 | |
Victor Stinner | 766ad36 | 2010-05-14 14:36:18 +0000 | [diff] [blame] | 42 | :pep:`273` - Import Modules from Zip Archives |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | Written by James C. Ahlstrom, who also provided an implementation. Python 2.3 |
Stéphane Wirtel | 12e696b | 2018-10-27 00:58:26 +0200 | [diff] [blame] | 44 | follows the specification in :pep:`273`, but uses an implementation written by Just |
| 45 | van Rossum that uses the import hooks described in :pep:`302`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
Brett Cannon | d2e94bb | 2020-11-13 15:14:58 -0800 | [diff] [blame] | 47 | :mod:`importlib` - The implementation of the import machinery |
| 48 | Package providing the relevant protocols for all importers to |
| 49 | implement. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
| 51 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 52 | This module defines an exception: |
| 53 | |
| 54 | .. exception:: ZipImportError |
| 55 | |
| 56 | Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`, |
| 57 | so it can be caught as :exc:`ImportError`, too. |
| 58 | |
| 59 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | .. _zipimporter-objects: |
| 61 | |
| 62 | zipimporter Objects |
| 63 | ------------------- |
| 64 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 65 | :class:`zipimporter` is the class for importing ZIP files. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
| 67 | .. class:: zipimporter(archivepath) |
| 68 | |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 69 | Create a new zipimporter instance. *archivepath* must be a path to a ZIP |
| 70 | file, or to a specific path within a ZIP file. For example, an *archivepath* |
| 71 | of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory |
| 72 | inside the ZIP file :file:`foo/bar.zip` (provided that it exists). |
| 73 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP |
| 75 | archive. |
| 76 | |
Brett Cannon | d2e94bb | 2020-11-13 15:14:58 -0800 | [diff] [blame] | 77 | .. method:: create_module(spec) |
| 78 | |
| 79 | Implementation of :meth:`importlib.abc.Loader.create_module` that returns |
| 80 | :const:`None` to explicitly request the default semantics. |
| 81 | |
| 82 | .. versionadded:: 3.10 |
| 83 | |
| 84 | |
| 85 | .. method:: exec_module(module) |
| 86 | |
| 87 | Implementation of :meth:`importlib.abc.Loader.exec_module`. |
| 88 | |
| 89 | .. versionadded:: 3.10 |
| 90 | |
| 91 | |
| 92 | .. method:: find_loader(fullname, path=None) |
| 93 | |
| 94 | An implementation of :meth:`importlib.abc.PathEntryFinder.find_loader`. |
| 95 | |
| 96 | .. deprecated:: 3.10 |
| 97 | |
| 98 | Use :meth:`find_spec` instead. |
| 99 | |
| 100 | |
| 101 | .. method:: find_module(fullname, path=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 103 | Search for a module specified by *fullname*. *fullname* must be the fully |
| 104 | qualified (dotted) module name. It returns the zipimporter instance itself |
| 105 | if the module was found, or :const:`None` if it wasn't. The optional |
| 106 | *path* argument is ignored---it's there for compatibility with the |
| 107 | importer protocol. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
Brett Cannon | d2e94bb | 2020-11-13 15:14:58 -0800 | [diff] [blame] | 109 | .. deprecated:: 3.10 |
| 110 | |
| 111 | Use :meth:`find_spec` instead. |
| 112 | |
| 113 | |
| 114 | .. method:: find_spec(fullname, target=None) |
| 115 | |
| 116 | An implementation of :meth:`importlib.abc.PathEntryFinder.find_spec`. |
| 117 | |
| 118 | .. versionadded:: 3.10 |
| 119 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 121 | .. method:: get_code(fullname) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 123 | Return the code object for the specified module. Raise |
Irit Katriel | fb34096 | 2020-12-19 00:09:54 +0000 | [diff] [blame] | 124 | :exc:`ZipImportError` if the module couldn't be imported. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
| 126 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 127 | .. method:: get_data(pathname) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 129 | Return the data associated with *pathname*. Raise :exc:`OSError` if the |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 130 | file wasn't found. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 132 | .. versionchanged:: 3.3 |
| 133 | :exc:`IOError` used to be raised instead of :exc:`OSError`. |
| 134 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 136 | .. method:: get_filename(fullname) |
| 137 | |
| 138 | Return the value ``__file__`` would be set to if the specified module |
| 139 | was imported. Raise :exc:`ZipImportError` if the module couldn't be |
Irit Katriel | fb34096 | 2020-12-19 00:09:54 +0000 | [diff] [blame] | 140 | imported. |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 141 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 142 | .. versionadded:: 3.1 |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 143 | |
| 144 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 145 | .. method:: get_source(fullname) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 147 | Return the source code for the specified module. Raise |
| 148 | :exc:`ZipImportError` if the module couldn't be found, return |
| 149 | :const:`None` if the archive does contain the module, but has no source |
| 150 | for it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
| 152 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 153 | .. method:: is_package(fullname) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 154 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 155 | Return ``True`` if the module specified by *fullname* is a package. Raise |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 156 | :exc:`ZipImportError` if the module couldn't be found. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
| 158 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 159 | .. method:: load_module(fullname) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 161 | Load the module specified by *fullname*. *fullname* must be the fully |
Irit Katriel | fb34096 | 2020-12-19 00:09:54 +0000 | [diff] [blame] | 162 | qualified (dotted) module name. Returns the imported module on success, |
| 163 | raises :exc:`ZipImportError` on failure. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
Brett Cannon | d2e94bb | 2020-11-13 15:14:58 -0800 | [diff] [blame] | 165 | .. deprecated:: 3.10 |
| 166 | |
| 167 | Use :meth:`exec_module` instead. |
| 168 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 169 | .. attribute:: archive |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 170 | |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 171 | The file name of the importer's associated ZIP file, without a possible |
| 172 | subpath. |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 173 | |
| 174 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 175 | .. attribute:: prefix |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 176 | |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 177 | The subpath within the ZIP file where modules are searched. This is the |
| 178 | empty string for zipimporter objects which point to the root of the ZIP |
| 179 | file. |
| 180 | |
| 181 | The :attr:`archive` and :attr:`prefix` attributes, when combined with a |
| 182 | slash, equal the original *archivepath* argument given to the |
| 183 | :class:`zipimporter` constructor. |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 184 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 185 | |
| 186 | .. _zipimport-examples: |
| 187 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 188 | Examples |
| 189 | -------- |
| 190 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 191 | Here is an example that imports a module from a ZIP archive - note that the |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 192 | :mod:`zipimport` module is not explicitly used. |
| 193 | |
| 194 | .. code-block:: shell-session |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 195 | |
Petri Lehtinen | 9f74c6c | 2013-02-23 19:26:56 +0100 | [diff] [blame] | 196 | $ unzip -l example.zip |
| 197 | Archive: example.zip |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | Length Date Time Name |
| 199 | -------- ---- ---- ---- |
| 200 | 8467 11-26-02 22:30 jwzthreading.py |
| 201 | -------- ------- |
| 202 | 8467 1 file |
| 203 | $ ./python |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 204 | Python 2.3 (#1, Aug 1 2003, 19:54:32) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 205 | >>> import sys |
Petri Lehtinen | 9f74c6c | 2013-02-23 19:26:56 +0100 | [diff] [blame] | 206 | >>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 207 | >>> import jwzthreading |
| 208 | >>> jwzthreading.__file__ |
Petri Lehtinen | 9f74c6c | 2013-02-23 19:26:56 +0100 | [diff] [blame] | 209 | 'example.zip/jwzthreading.py' |