blob: ed9c631469b6f650cb3a3b44af7f67468c5b676d [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
32 `PKZIP Application Note <http://www.pkware.com/business_and_developers/developer/appnote/>`_
33 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
73.. method:: zipimporter.find_module(fullname[, path])
74
75 Search for a module specified by *fullname*. *fullname* must be the fully
76 qualified (dotted) module name. It returns the zipimporter instance itself if
77 the module was found, or :const:`None` if it wasn't. The optional *path*
78 argument is ignored---it's there for compatibility with the importer protocol.
79
80
81.. method:: zipimporter.get_code(fullname)
82
83 Return the code object for the specified module. Raise :exc:`ZipImportError` if
84 the module couldn't be found.
85
86
87.. method:: zipimporter.get_data(pathname)
88
89 Return the data associated with *pathname*. Raise :exc:`IOError` if the file
90 wasn't found.
91
92
93.. method:: zipimporter.get_source(fullname)
94
95 Return the source code for the specified module. Raise :exc:`ZipImportError` if
96 the module couldn't be found, return :const:`None` if the archive does contain
97 the module, but has no source for it.
98
99
100.. method:: zipimporter.is_package(fullname)
101
102 Return True if the module specified by *fullname* is a package. Raise
103 :exc:`ZipImportError` if the module couldn't be found.
104
105
106.. method:: zipimporter.load_module(fullname)
107
108 Load the module specified by *fullname*. *fullname* must be the fully qualified
109 (dotted) module name. It returns the imported module, or raises
110 :exc:`ZipImportError` if it wasn't found.
111
112
Christian Heimes7f044312008-01-06 17:05:40 +0000113.. attribute:: zipimporter.archive
114
115 The file name of the importer's associated ZIP file.
116
117
118.. attribute:: zipimporter.prefix
119
120 The path within the ZIP file where modules are searched; see
121 :class:`zipimporter` for details.
122
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124.. _zipimport-examples:
125
Christian Heimes7f044312008-01-06 17:05:40 +0000126Examples
127--------
128
Georg Brandl116aa622007-08-15 14:28:22 +0000129Here is an example that imports a module from a ZIP archive - note that the
130:mod:`zipimport` module is not explicitly used. ::
131
132 $ unzip -l /tmp/example.zip
133 Archive: /tmp/example.zip
134 Length Date Time Name
135 -------- ---- ---- ----
136 8467 11-26-02 22:30 jwzthreading.py
137 -------- -------
138 8467 1 file
139 $ ./python
140 Python 2.3 (#1, Aug 1 2003, 19:54:32)
141 >>> import sys
142 >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
143 >>> import jwzthreading
144 >>> jwzthreading.__file__
145 '/tmp/example.zip/jwzthreading.py'
146