blob: f2b23586a24ab1aaa108f3761f9e8566c31c2a34 [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
10.. versionadded:: 2.3
11
12This module adds the ability to import Python modules (:file:`\*.py`,
13:file:`\*.py[co]`) and packages from ZIP-format archives. It is usually not
14needed to use the :mod:`zipimport` module explicitly; it is automatically used
15by the builtin :keyword:`import` mechanism for ``sys.path`` items that are paths
16to ZIP archives.
17
18Typically, ``sys.path`` is a list of directory names as strings. This module
19also allows an item of ``sys.path`` to be a string naming a ZIP file archive.
20The ZIP archive can contain a subdirectory structure to support package imports,
21and a path within the archive can be specified to only import from a
22subdirectory. For example, the path :file:`/tmp/example.zip/lib/` would only
23import from the :file:`lib/` subdirectory within the archive.
24
25Any files may be present in the ZIP archive, but only files :file:`.py` and
26:file:`.py[co]` are available for import. ZIP import of dynamic modules
27(:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains
28:file:`.py` files, Python will not attempt to modify the archive by adding the
29corresponding :file:`.pyc` or :file:`.pyo` file, meaning that if a ZIP archive
30doesn't contain :file:`.pyc` files, importing may be rather slow.
31
32The available attributes of this module are:
33
34
35.. exception:: ZipImportError
36
37 Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
38 so it can be caught as :exc:`ImportError`, too.
39
40
41.. class:: zipimporter
42
43 The class for importing ZIP files. See section :ref:`zipimporter-objects`
44 for constructor details.
45
46
47.. seealso::
48
49 `PKZIP Application Note <http://www.pkware.com/business_and_developers/developer/appnote/>`_
50 Documentation on the ZIP file format by Phil Katz, the creator of the format and
51 algorithms used.
52
53 :pep:`0273` - Import Modules from Zip Archives
54 Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
55 follows the specification in PEP 273, but uses an implementation written by Just
56 van Rossum that uses the import hooks described in PEP 302.
57
58 :pep:`0302` - New Import Hooks
59 The PEP to add the import hooks that help this module work.
60
61
62.. _zipimporter-objects:
63
64zipimporter Objects
65-------------------
66
67
68.. class:: zipimporter(archivepath)
69
70 Create a new zipimporter instance. *archivepath* must be a path to a zipfile.
71 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
72 archive.
73
74
75.. method:: zipimporter.find_module(fullname[, path])
76
77 Search for a module specified by *fullname*. *fullname* must be the fully
78 qualified (dotted) module name. It returns the zipimporter instance itself if
79 the module was found, or :const:`None` if it wasn't. The optional *path*
80 argument is ignored---it's there for compatibility with the importer protocol.
81
82
83.. method:: zipimporter.get_code(fullname)
84
85 Return the code object for the specified module. Raise :exc:`ZipImportError` if
86 the module couldn't be found.
87
88
89.. method:: zipimporter.get_data(pathname)
90
91 Return the data associated with *pathname*. Raise :exc:`IOError` if the file
92 wasn't found.
93
94
95.. method:: zipimporter.get_source(fullname)
96
97 Return the source code for the specified module. Raise :exc:`ZipImportError` if
98 the module couldn't be found, return :const:`None` if the archive does contain
99 the module, but has no source for it.
100
101
102.. method:: zipimporter.is_package(fullname)
103
104 Return True if the module specified by *fullname* is a package. Raise
105 :exc:`ZipImportError` if the module couldn't be found.
106
107
108.. method:: zipimporter.load_module(fullname)
109
110 Load the module specified by *fullname*. *fullname* must be the fully qualified
111 (dotted) module name. It returns the imported module, or raises
112 :exc:`ZipImportError` if it wasn't found.
113
114
115Examples
116--------
117
118.. _zipimport-examples:
119
120Here is an example that imports a module from a ZIP archive - note that the
121:mod:`zipimport` module is not explicitly used. ::
122
123 $ unzip -l /tmp/example.zip
124 Archive: /tmp/example.zip
125 Length Date Time Name
126 -------- ---- ---- ----
127 8467 11-26-02 22:30 jwzthreading.py
128 -------- -------
129 8467 1 file
130 $ ./python
131 Python 2.3 (#1, Aug 1 2003, 19:54:32)
132 >>> import sys
133 >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
134 >>> import jwzthreading
135 >>> jwzthreading.__file__
136 '/tmp/example.zip/jwzthreading.py'
137