blob: 2cf508b85c1411d5ab523505bdb7b2d7af7abbb1 [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.
6.. moduleauthor:: Just van Rossum <just@letterror.com>
7
8
Georg Brandl116aa622007-08-15 14:28:22 +00009This module adds the ability to import Python modules (:file:`\*.py`,
10:file:`\*.py[co]`) and packages from ZIP-format archives. It is usually not
11needed to use the :mod:`zipimport` module explicitly; it is automatically used
Ezio Melotti2d826e82011-06-25 19:40:06 +030012by the built-in :keyword:`import` mechanism for :data:`sys.path` items that are paths
Georg Brandl116aa622007-08-15 14:28:22 +000013to ZIP archives.
14
Ezio Melotti2d826e82011-06-25 19:40:06 +030015Typically, :data:`sys.path` is a list of directory names as strings. This module
16also allows an item of :data:`sys.path` to be a string naming a ZIP file archive.
Georg Brandl116aa622007-08-15 14:28:22 +000017The ZIP archive can contain a subdirectory structure to support package imports,
18and a path within the archive can be specified to only import from a
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +010019subdirectory. For example, the path :file:`example.zip/lib/` would only
Georg Brandl116aa622007-08-15 14:28:22 +000020import from the :file:`lib/` subdirectory within the archive.
21
22Any files may be present in the ZIP archive, but only files :file:`.py` and
23:file:`.py[co]` are available for import. ZIP import of dynamic modules
24(:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains
25:file:`.py` files, Python will not attempt to modify the archive by adding the
26corresponding :file:`.pyc` or :file:`.pyo` file, meaning that if a ZIP archive
27doesn't contain :file:`.pyc` files, importing may be rather slow.
28
Benjamin Petersona28e7022010-01-09 18:53:06 +000029ZIP archives with an archive comment are currently not supported.
30
Georg Brandl116aa622007-08-15 14:28:22 +000031.. seealso::
32
Christian Heimesdd15f6c2008-03-16 00:07:10 +000033 `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_
Georg Brandl116aa622007-08-15 14:28:22 +000034 Documentation on the ZIP file format by Phil Katz, the creator of the format and
35 algorithms used.
36
Victor Stinner766ad362010-05-14 14:36:18 +000037 :pep:`273` - Import Modules from Zip Archives
Georg Brandl116aa622007-08-15 14:28:22 +000038 Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
39 follows the specification in PEP 273, but uses an implementation written by Just
40 van Rossum that uses the import hooks described in PEP 302.
41
Victor Stinner766ad362010-05-14 14:36:18 +000042 :pep:`302` - New Import Hooks
Georg Brandl116aa622007-08-15 14:28:22 +000043 The PEP to add the import hooks that help this module work.
44
45
Christian Heimes7f044312008-01-06 17:05:40 +000046This module defines an exception:
47
48.. exception:: ZipImportError
49
50 Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
51 so it can be caught as :exc:`ImportError`, too.
52
53
Georg Brandl116aa622007-08-15 14:28:22 +000054.. _zipimporter-objects:
55
56zipimporter Objects
57-------------------
58
Christian Heimes7f044312008-01-06 17:05:40 +000059:class:`zipimporter` is the class for importing ZIP files.
Georg Brandl116aa622007-08-15 14:28:22 +000060
61.. class:: zipimporter(archivepath)
62
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000063 Create a new zipimporter instance. *archivepath* must be a path to a ZIP
64 file, or to a specific path within a ZIP file. For example, an *archivepath*
65 of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory
66 inside the ZIP file :file:`foo/bar.zip` (provided that it exists).
67
Georg Brandl116aa622007-08-15 14:28:22 +000068 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
69 archive.
70
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 .. method:: find_module(fullname[, path])
Georg Brandl116aa622007-08-15 14:28:22 +000072
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 Search for a module specified by *fullname*. *fullname* must be the fully
74 qualified (dotted) module name. It returns the zipimporter instance itself
75 if the module was found, or :const:`None` if it wasn't. The optional
76 *path* argument is ignored---it's there for compatibility with the
77 importer protocol.
Georg Brandl116aa622007-08-15 14:28:22 +000078
79
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 .. method:: get_code(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +000081
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 Return the code object for the specified module. Raise
83 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +000084
85
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 .. method:: get_data(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000087
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020088 Return the data associated with *pathname*. Raise :exc:`OSError` if the
Benjamin Petersone41251e2008-04-25 01:59:09 +000089 file wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +000090
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020091 .. versionchanged:: 3.3
92 :exc:`IOError` used to be raised instead of :exc:`OSError`.
93
Georg Brandl116aa622007-08-15 14:28:22 +000094
Nick Coghlan9a1d6e32009-02-08 03:37:27 +000095 .. method:: get_filename(fullname)
96
97 Return the value ``__file__`` would be set to if the specified module
98 was imported. Raise :exc:`ZipImportError` if the module couldn't be
99 found.
100
Georg Brandl67b21b72010-08-17 15:07:14 +0000101 .. versionadded:: 3.1
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000102
103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 .. method:: get_source(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Benjamin Petersone41251e2008-04-25 01:59:09 +0000106 Return the source code for the specified module. Raise
107 :exc:`ZipImportError` if the module couldn't be found, return
108 :const:`None` if the archive does contain the module, but has no source
109 for it.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111
Benjamin Petersone41251e2008-04-25 01:59:09 +0000112 .. method:: is_package(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200114 Return ``True`` if the module specified by *fullname* is a package. Raise
Benjamin Petersone41251e2008-04-25 01:59:09 +0000115 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 .. method:: load_module(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Benjamin Petersone41251e2008-04-25 01:59:09 +0000120 Load the module specified by *fullname*. *fullname* must be the fully
121 qualified (dotted) module name. It returns the imported module, or raises
122 :exc:`ZipImportError` if it wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124
Benjamin Petersone41251e2008-04-25 01:59:09 +0000125 .. attribute:: archive
Christian Heimes7f044312008-01-06 17:05:40 +0000126
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000127 The file name of the importer's associated ZIP file, without a possible
128 subpath.
Christian Heimes7f044312008-01-06 17:05:40 +0000129
130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 .. attribute:: prefix
Christian Heimes7f044312008-01-06 17:05:40 +0000132
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000133 The subpath within the ZIP file where modules are searched. This is the
134 empty string for zipimporter objects which point to the root of the ZIP
135 file.
136
137 The :attr:`archive` and :attr:`prefix` attributes, when combined with a
138 slash, equal the original *archivepath* argument given to the
139 :class:`zipimporter` constructor.
Christian Heimes7f044312008-01-06 17:05:40 +0000140
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142.. _zipimport-examples:
143
Christian Heimes7f044312008-01-06 17:05:40 +0000144Examples
145--------
146
Georg Brandl116aa622007-08-15 14:28:22 +0000147Here is an example that imports a module from a ZIP archive - note that the
148:mod:`zipimport` module is not explicitly used. ::
149
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100150 $ unzip -l example.zip
151 Archive: example.zip
Georg Brandl116aa622007-08-15 14:28:22 +0000152 Length Date Time Name
153 -------- ---- ---- ----
154 8467 11-26-02 22:30 jwzthreading.py
155 -------- -------
156 8467 1 file
157 $ ./python
Georg Brandl48310cd2009-01-03 21:18:54 +0000158 Python 2.3 (#1, Aug 1 2003, 19:54:32)
Georg Brandl116aa622007-08-15 14:28:22 +0000159 >>> import sys
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100160 >>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path
Georg Brandl116aa622007-08-15 14:28:22 +0000161 >>> import jwzthreading
162 >>> jwzthreading.__file__
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100163 'example.zip/jwzthreading.py'
Georg Brandl116aa622007-08-15 14:28:22 +0000164