blob: 8972c06764269a50ad3461c0808b2365801468eb [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
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
Georg Brandl116aa622007-08-15 14:28:22 +000010This module adds the ability to import Python modules (:file:`\*.py`,
11:file:`\*.py[co]`) and packages from ZIP-format archives. It is usually not
12needed to use the :mod:`zipimport` module explicitly; it is automatically used
13by the builtin :keyword:`import` mechanism for ``sys.path`` items that are paths
14to ZIP archives.
15
16Typically, ``sys.path`` is a list of directory names as strings. This module
17also allows an item of ``sys.path`` to be a string naming a ZIP file archive.
18The ZIP archive can contain a subdirectory structure to support package imports,
19and a path within the archive can be specified to only import from a
20subdirectory. For example, the path :file:`/tmp/example.zip/lib/` would only
21import from the :file:`lib/` subdirectory within the archive.
22
23Any files may be present in the ZIP archive, but only files :file:`.py` and
24:file:`.py[co]` are available for import. ZIP import of dynamic modules
25(:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains
26:file:`.py` files, Python will not attempt to modify the archive by adding the
27corresponding :file:`.pyc` or :file:`.pyo` file, meaning that if a ZIP archive
28doesn't contain :file:`.pyc` files, importing may be rather slow.
29
Georg Brandl116aa622007-08-15 14:28:22 +000030.. seealso::
31
Christian Heimesdd15f6c2008-03-16 00:07:10 +000032 `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_
Georg Brandl116aa622007-08-15 14:28:22 +000033 Documentation on the ZIP file format by Phil Katz, the creator of the format and
34 algorithms used.
35
36 :pep:`0273` - Import Modules from Zip Archives
37 Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
38 follows the specification in PEP 273, but uses an implementation written by Just
39 van Rossum that uses the import hooks described in PEP 302.
40
41 :pep:`0302` - New Import Hooks
42 The PEP to add the import hooks that help this module work.
43
44
Christian Heimes7f044312008-01-06 17:05:40 +000045This module defines an exception:
46
47.. exception:: ZipImportError
48
49 Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
50 so it can be caught as :exc:`ImportError`, too.
51
52
Georg Brandl116aa622007-08-15 14:28:22 +000053.. _zipimporter-objects:
54
55zipimporter Objects
56-------------------
57
Christian Heimes7f044312008-01-06 17:05:40 +000058:class:`zipimporter` is the class for importing ZIP files.
Georg Brandl116aa622007-08-15 14:28:22 +000059
60.. class:: zipimporter(archivepath)
61
Christian Heimes7f044312008-01-06 17:05:40 +000062 Create a new zipimporter instance. *archivepath* must be a path to a ZIP file.
Georg Brandl116aa622007-08-15 14:28:22 +000063 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
64 archive.
65
Christian Heimes7f044312008-01-06 17:05:40 +000066 *archivepath* can also contain a path within the ZIP file -- the importer
67 object will then look under that path instead of the ZIP file root. For
68 example, an *archivepath* of :file:`foo/bar.zip/lib` will look for modules
69 in the :file:`lib` directory inside the ZIP file :file:`foo/bar.zip`
70 (provided that it exists).
71
Georg Brandl116aa622007-08-15 14:28:22 +000072
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 .. method:: find_module(fullname[, path])
Georg Brandl116aa622007-08-15 14:28:22 +000074
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 Search for a module specified by *fullname*. *fullname* must be the fully
76 qualified (dotted) module name. It returns the zipimporter instance itself
77 if the module was found, or :const:`None` if it wasn't. The optional
78 *path* argument is ignored---it's there for compatibility with the
79 importer protocol.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 .. method:: get_code(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +000083
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 Return the code object for the specified module. Raise
85 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +000086
87
Benjamin Petersone41251e2008-04-25 01:59:09 +000088 .. method:: get_data(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000089
Benjamin Petersone41251e2008-04-25 01:59:09 +000090 Return the data associated with *pathname*. Raise :exc:`IOError` if the
91 file wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 .. method:: get_source(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +000095
Benjamin Petersone41251e2008-04-25 01:59:09 +000096 Return the source code for the specified module. Raise
97 :exc:`ZipImportError` if the module couldn't be found, return
98 :const:`None` if the archive does contain the module, but has no source
99 for it.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101
Benjamin Petersone41251e2008-04-25 01:59:09 +0000102 .. method:: is_package(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 Return True if the module specified by *fullname* is a package. Raise
105 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107
Benjamin Petersone41251e2008-04-25 01:59:09 +0000108 .. method:: load_module(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 Load the module specified by *fullname*. *fullname* must be the fully
111 qualified (dotted) module name. It returns the imported module, or raises
112 :exc:`ZipImportError` if it wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114
Benjamin Petersone41251e2008-04-25 01:59:09 +0000115 .. attribute:: archive
Christian Heimes7f044312008-01-06 17:05:40 +0000116
Benjamin Petersone41251e2008-04-25 01:59:09 +0000117 The file name of the importer's associated ZIP file.
Christian Heimes7f044312008-01-06 17:05:40 +0000118
119
Benjamin Petersone41251e2008-04-25 01:59:09 +0000120 .. attribute:: prefix
Christian Heimes7f044312008-01-06 17:05:40 +0000121
Benjamin Petersone41251e2008-04-25 01:59:09 +0000122 The path within the ZIP file where modules are searched; see
123 :class:`zipimporter` for details.
Christian Heimes7f044312008-01-06 17:05:40 +0000124
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. _zipimport-examples:
127
Christian Heimes7f044312008-01-06 17:05:40 +0000128Examples
129--------
130
Georg Brandl116aa622007-08-15 14:28:22 +0000131Here is an example that imports a module from a ZIP archive - note that the
132:mod:`zipimport` module is not explicitly used. ::
133
134 $ unzip -l /tmp/example.zip
135 Archive: /tmp/example.zip
136 Length Date Time Name
137 -------- ---- ---- ----
138 8467 11-26-02 22:30 jwzthreading.py
139 -------- -------
140 8467 1 file
141 $ ./python
142 Python 2.3 (#1, Aug 1 2003, 19:54:32)
143 >>> import sys
144 >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
145 >>> import jwzthreading
146 >>> jwzthreading.__file__
147 '/tmp/example.zip/jwzthreading.py'
148