blob: aa1831dbc60de97d58a7ba5187af7d00b08a1734 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`zipimport` --- Import modules from Zip archives
2=====================================================
3
4.. module:: zipimport
5 :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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04009--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
Georg Brandl116aa622007-08-15 14:28:22 +000011This module adds the ability to import Python modules (:file:`\*.py`,
Xiang Zhang0710d752017-03-11 13:02:52 +080012:file:`\*.pyc`) and packages from ZIP-format archives. It is usually not
Georg Brandl116aa622007-08-15 14:28:22 +000013needed to use the :mod:`zipimport` module explicitly; it is automatically used
Ezio Melotti2d826e82011-06-25 19:40:06 +030014by the built-in :keyword:`import` mechanism for :data:`sys.path` items that are paths
Georg Brandl116aa622007-08-15 14:28:22 +000015to ZIP archives.
16
Ezio Melotti2d826e82011-06-25 19:40:06 +030017Typically, :data:`sys.path` is a list of directory names as strings. This module
18also allows an item of :data:`sys.path` to be a string naming a ZIP file archive.
Georg Brandl116aa622007-08-15 14:28:22 +000019The ZIP archive can contain a subdirectory structure to support package imports,
20and a path within the archive can be specified to only import from a
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +010021subdirectory. For example, the path :file:`example.zip/lib/` would only
Georg Brandl116aa622007-08-15 14:28:22 +000022import from the :file:`lib/` subdirectory within the archive.
23
24Any files may be present in the ZIP archive, but only files :file:`.py` and
Brett Cannonf299abd2015-04-13 14:21:02 -040025:file:`.pyc` are available for import. ZIP import of dynamic modules
Georg Brandl116aa622007-08-15 14:28:22 +000026(:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains
27:file:`.py` files, Python will not attempt to modify the archive by adding the
Brett Cannonf299abd2015-04-13 14:21:02 -040028corresponding :file:`.pyc` file, meaning that if a ZIP archive
Georg Brandl116aa622007-08-15 14:28:22 +000029doesn't contain :file:`.pyc` files, importing may be rather slow.
30
Zackery Spytz5a5ce062018-09-25 13:15:47 -060031.. versionchanged:: 3.8
32 Previously, ZIP archives with an archive comment were not supported.
Benjamin Petersona28e7022010-01-09 18:53:06 +000033
Georg Brandl116aa622007-08-15 14:28:22 +000034.. seealso::
35
Georg Brandl5d941342016-02-26 19:37:12 +010036 `PKZIP Application Note <https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT>`_
Georg Brandl116aa622007-08-15 14:28:22 +000037 Documentation on the ZIP file format by Phil Katz, the creator of the format and
38 algorithms used.
39
Victor Stinner766ad362010-05-14 14:36:18 +000040 :pep:`273` - Import Modules from Zip Archives
Georg Brandl116aa622007-08-15 14:28:22 +000041 Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
Stéphane Wirtel12e696b2018-10-27 00:58:26 +020042 follows the specification in :pep:`273`, but uses an implementation written by Just
43 van Rossum that uses the import hooks described in :pep:`302`.
Georg Brandl116aa622007-08-15 14:28:22 +000044
Victor Stinner766ad362010-05-14 14:36:18 +000045 :pep:`302` - New Import Hooks
Georg Brandl116aa622007-08-15 14:28:22 +000046 The PEP to add the import hooks that help this module work.
47
48
Christian Heimes7f044312008-01-06 17:05:40 +000049This module defines an exception:
50
51.. exception:: ZipImportError
52
53 Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
54 so it can be caught as :exc:`ImportError`, too.
55
56
Georg Brandl116aa622007-08-15 14:28:22 +000057.. _zipimporter-objects:
58
59zipimporter Objects
60-------------------
61
Christian Heimes7f044312008-01-06 17:05:40 +000062:class:`zipimporter` is the class for importing ZIP files.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64.. class:: zipimporter(archivepath)
65
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000066 Create a new zipimporter instance. *archivepath* must be a path to a ZIP
67 file, or to a specific path within a ZIP file. For example, an *archivepath*
68 of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory
69 inside the ZIP file :file:`foo/bar.zip` (provided that it exists).
70
Georg Brandl116aa622007-08-15 14:28:22 +000071 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
72 archive.
73
Benjamin Petersone41251e2008-04-25 01:59:09 +000074 .. method:: find_module(fullname[, path])
Georg Brandl116aa622007-08-15 14:28:22 +000075
Benjamin Petersone41251e2008-04-25 01:59:09 +000076 Search for a module specified by *fullname*. *fullname* must be the fully
77 qualified (dotted) module name. It returns the zipimporter instance itself
78 if the module was found, or :const:`None` if it wasn't. The optional
79 *path* argument is ignored---it's there for compatibility with the
80 importer protocol.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 .. method:: get_code(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +000084
Benjamin Petersone41251e2008-04-25 01:59:09 +000085 Return the code object for the specified module. Raise
86 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
Benjamin Petersone41251e2008-04-25 01:59:09 +000089 .. method:: get_data(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000090
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020091 Return the data associated with *pathname*. Raise :exc:`OSError` if the
Benjamin Petersone41251e2008-04-25 01:59:09 +000092 file wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +000093
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020094 .. versionchanged:: 3.3
95 :exc:`IOError` used to be raised instead of :exc:`OSError`.
96
Georg Brandl116aa622007-08-15 14:28:22 +000097
Nick Coghlan9a1d6e32009-02-08 03:37:27 +000098 .. method:: get_filename(fullname)
99
100 Return the value ``__file__`` would be set to if the specified module
101 was imported. Raise :exc:`ZipImportError` if the module couldn't be
102 found.
103
Georg Brandl67b21b72010-08-17 15:07:14 +0000104 .. versionadded:: 3.1
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000105
106
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 .. method:: get_source(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 Return the source code for the specified module. Raise
110 :exc:`ZipImportError` if the module couldn't be found, return
111 :const:`None` if the archive does contain the module, but has no source
112 for it.
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114
Benjamin Petersone41251e2008-04-25 01:59:09 +0000115 .. method:: is_package(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000116
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200117 Return ``True`` if the module specified by *fullname* is a package. Raise
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120
Benjamin Petersone41251e2008-04-25 01:59:09 +0000121 .. method:: load_module(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Benjamin Petersone41251e2008-04-25 01:59:09 +0000123 Load the module specified by *fullname*. *fullname* must be the fully
124 qualified (dotted) module name. It returns the imported module, or raises
125 :exc:`ZipImportError` if it wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 .. attribute:: archive
Christian Heimes7f044312008-01-06 17:05:40 +0000129
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000130 The file name of the importer's associated ZIP file, without a possible
131 subpath.
Christian Heimes7f044312008-01-06 17:05:40 +0000132
133
Benjamin Petersone41251e2008-04-25 01:59:09 +0000134 .. attribute:: prefix
Christian Heimes7f044312008-01-06 17:05:40 +0000135
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000136 The subpath within the ZIP file where modules are searched. This is the
137 empty string for zipimporter objects which point to the root of the ZIP
138 file.
139
140 The :attr:`archive` and :attr:`prefix` attributes, when combined with a
141 slash, equal the original *archivepath* argument given to the
142 :class:`zipimporter` constructor.
Christian Heimes7f044312008-01-06 17:05:40 +0000143
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145.. _zipimport-examples:
146
Christian Heimes7f044312008-01-06 17:05:40 +0000147Examples
148--------
149
Georg Brandl116aa622007-08-15 14:28:22 +0000150Here is an example that imports a module from a ZIP archive - note that the
Martin Panter1050d2d2016-07-26 11:18:21 +0200151:mod:`zipimport` module is not explicitly used.
152
153.. code-block:: shell-session
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100155 $ unzip -l example.zip
156 Archive: example.zip
Georg Brandl116aa622007-08-15 14:28:22 +0000157 Length Date Time Name
158 -------- ---- ---- ----
159 8467 11-26-02 22:30 jwzthreading.py
160 -------- -------
161 8467 1 file
162 $ ./python
Georg Brandl48310cd2009-01-03 21:18:54 +0000163 Python 2.3 (#1, Aug 1 2003, 19:54:32)
Georg Brandl116aa622007-08-15 14:28:22 +0000164 >>> import sys
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100165 >>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path
Georg Brandl116aa622007-08-15 14:28:22 +0000166 >>> import jwzthreading
167 >>> jwzthreading.__file__
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100168 'example.zip/jwzthreading.py'