blob: 8ac3fb16bdb90d1eb7675d9ea84d3e1cde801d9d [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`zipimport` --- Import modules from Zip archives
2=====================================================
3
4.. module:: zipimport
Sanchit Khuranaf8a63162019-11-26 03:47:59 +05305 :synopsis: Support for importing Python modules from ZIP archives.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Just van Rossum <just@letterror.com>
8
Xtreake307e5c2019-05-15 21:48:35 +05309**Source code:** :source:`Lib/zipimport.py`
10
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040011--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl116aa622007-08-15 14:28:22 +000013This module adds the ability to import Python modules (:file:`\*.py`,
Xiang Zhang0710d752017-03-11 13:02:52 +080014:file:`\*.pyc`) and packages from ZIP-format archives. It is usually not
Georg Brandl116aa622007-08-15 14:28:22 +000015needed to use the :mod:`zipimport` module explicitly; it is automatically used
Ezio Melotti2d826e82011-06-25 19:40:06 +030016by the built-in :keyword:`import` mechanism for :data:`sys.path` items that are paths
Georg Brandl116aa622007-08-15 14:28:22 +000017to ZIP archives.
18
Ezio Melotti2d826e82011-06-25 19:40:06 +030019Typically, :data:`sys.path` is a list of directory names as strings. This module
20also allows an item of :data:`sys.path` to be a string naming a ZIP file archive.
Georg Brandl116aa622007-08-15 14:28:22 +000021The ZIP archive can contain a subdirectory structure to support package imports,
22and a path within the archive can be specified to only import from a
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +010023subdirectory. For example, the path :file:`example.zip/lib/` would only
Georg Brandl116aa622007-08-15 14:28:22 +000024import from the :file:`lib/` subdirectory within the archive.
25
26Any files may be present in the ZIP archive, but only files :file:`.py` and
Brett Cannonf299abd2015-04-13 14:21:02 -040027:file:`.pyc` are available for import. ZIP import of dynamic modules
Georg Brandl116aa622007-08-15 14:28:22 +000028(: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 Cannonf299abd2015-04-13 14:21:02 -040030corresponding :file:`.pyc` file, meaning that if a ZIP archive
Georg Brandl116aa622007-08-15 14:28:22 +000031doesn't contain :file:`.pyc` files, importing may be rather slow.
32
Zackery Spytz5a5ce062018-09-25 13:15:47 -060033.. versionchanged:: 3.8
34 Previously, ZIP archives with an archive comment were not supported.
Benjamin Petersona28e7022010-01-09 18:53:06 +000035
Georg Brandl116aa622007-08-15 14:28:22 +000036.. seealso::
37
Georg Brandl5d941342016-02-26 19:37:12 +010038 `PKZIP Application Note <https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT>`_
Georg Brandl116aa622007-08-15 14:28:22 +000039 Documentation on the ZIP file format by Phil Katz, the creator of the format and
40 algorithms used.
41
Victor Stinner766ad362010-05-14 14:36:18 +000042 :pep:`273` - Import Modules from Zip Archives
Georg Brandl116aa622007-08-15 14:28:22 +000043 Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
Stéphane Wirtel12e696b2018-10-27 00:58:26 +020044 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 Brandl116aa622007-08-15 14:28:22 +000046
Victor Stinner766ad362010-05-14 14:36:18 +000047 :pep:`302` - New Import Hooks
Georg Brandl116aa622007-08-15 14:28:22 +000048 The PEP to add the import hooks that help this module work.
49
50
Christian Heimes7f044312008-01-06 17:05:40 +000051This module defines an exception:
52
53.. exception:: ZipImportError
54
55 Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
56 so it can be caught as :exc:`ImportError`, too.
57
58
Georg Brandl116aa622007-08-15 14:28:22 +000059.. _zipimporter-objects:
60
61zipimporter Objects
62-------------------
63
Christian Heimes7f044312008-01-06 17:05:40 +000064:class:`zipimporter` is the class for importing ZIP files.
Georg Brandl116aa622007-08-15 14:28:22 +000065
66.. class:: zipimporter(archivepath)
67
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000068 Create a new zipimporter instance. *archivepath* must be a path to a ZIP
69 file, or to a specific path within a ZIP file. For example, an *archivepath*
70 of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory
71 inside the ZIP file :file:`foo/bar.zip` (provided that it exists).
72
Georg Brandl116aa622007-08-15 14:28:22 +000073 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
74 archive.
75
Benjamin Petersone41251e2008-04-25 01:59:09 +000076 .. method:: find_module(fullname[, path])
Georg Brandl116aa622007-08-15 14:28:22 +000077
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 Search for a module specified by *fullname*. *fullname* must be the fully
79 qualified (dotted) module name. It returns the zipimporter instance itself
80 if the module was found, or :const:`None` if it wasn't. The optional
81 *path* argument is ignored---it's there for compatibility with the
82 importer protocol.
Georg Brandl116aa622007-08-15 14:28:22 +000083
84
Benjamin Petersone41251e2008-04-25 01:59:09 +000085 .. method:: get_code(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +000086
Benjamin Petersone41251e2008-04-25 01:59:09 +000087 Return the code object for the specified module. Raise
88 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +000089
90
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 .. method:: get_data(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000092
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020093 Return the data associated with *pathname*. Raise :exc:`OSError` if the
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 file wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +000095
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020096 .. versionchanged:: 3.3
97 :exc:`IOError` used to be raised instead of :exc:`OSError`.
98
Georg Brandl116aa622007-08-15 14:28:22 +000099
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000100 .. method:: get_filename(fullname)
101
102 Return the value ``__file__`` would be set to if the specified module
103 was imported. Raise :exc:`ZipImportError` if the module couldn't be
104 found.
105
Georg Brandl67b21b72010-08-17 15:07:14 +0000106 .. versionadded:: 3.1
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000107
108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 .. method:: get_source(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 Return the source code for the specified module. Raise
112 :exc:`ZipImportError` if the module couldn't be found, return
113 :const:`None` if the archive does contain the module, but has no source
114 for it.
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116
Benjamin Petersone41251e2008-04-25 01:59:09 +0000117 .. method:: is_package(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000118
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200119 Return ``True`` if the module specified by *fullname* is a package. Raise
Benjamin Petersone41251e2008-04-25 01:59:09 +0000120 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122
Benjamin Petersone41251e2008-04-25 01:59:09 +0000123 .. method:: load_module(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000124
Benjamin Petersone41251e2008-04-25 01:59:09 +0000125 Load the module specified by *fullname*. *fullname* must be the fully
126 qualified (dotted) module name. It returns the imported module, or raises
127 :exc:`ZipImportError` if it wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129
Benjamin Petersone41251e2008-04-25 01:59:09 +0000130 .. attribute:: archive
Christian Heimes7f044312008-01-06 17:05:40 +0000131
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000132 The file name of the importer's associated ZIP file, without a possible
133 subpath.
Christian Heimes7f044312008-01-06 17:05:40 +0000134
135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 .. attribute:: prefix
Christian Heimes7f044312008-01-06 17:05:40 +0000137
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000138 The subpath within the ZIP file where modules are searched. This is the
139 empty string for zipimporter objects which point to the root of the ZIP
140 file.
141
142 The :attr:`archive` and :attr:`prefix` attributes, when combined with a
143 slash, equal the original *archivepath* argument given to the
144 :class:`zipimporter` constructor.
Christian Heimes7f044312008-01-06 17:05:40 +0000145
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147.. _zipimport-examples:
148
Christian Heimes7f044312008-01-06 17:05:40 +0000149Examples
150--------
151
Georg Brandl116aa622007-08-15 14:28:22 +0000152Here is an example that imports a module from a ZIP archive - note that the
Martin Panter1050d2d2016-07-26 11:18:21 +0200153:mod:`zipimport` module is not explicitly used.
154
155.. code-block:: shell-session
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100157 $ unzip -l example.zip
158 Archive: example.zip
Georg Brandl116aa622007-08-15 14:28:22 +0000159 Length Date Time Name
160 -------- ---- ---- ----
161 8467 11-26-02 22:30 jwzthreading.py
162 -------- -------
163 8467 1 file
164 $ ./python
Georg Brandl48310cd2009-01-03 21:18:54 +0000165 Python 2.3 (#1, Aug 1 2003, 19:54:32)
Georg Brandl116aa622007-08-15 14:28:22 +0000166 >>> import sys
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100167 >>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path
Georg Brandl116aa622007-08-15 14:28:22 +0000168 >>> import jwzthreading
169 >>> jwzthreading.__file__
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100170 'example.zip/jwzthreading.py'