blob: 6845bf7a43cb31b682c20479f63102aebfaeab31 [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
30The available attributes of this module are:
31
32
33.. exception:: ZipImportError
34
35 Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
36 so it can be caught as :exc:`ImportError`, too.
37
38
39.. class:: zipimporter
40
41 The class for importing ZIP files. See section :ref:`zipimporter-objects`
42 for constructor details.
43
44
45.. seealso::
46
47 `PKZIP Application Note <http://www.pkware.com/business_and_developers/developer/appnote/>`_
48 Documentation on the ZIP file format by Phil Katz, the creator of the format and
49 algorithms used.
50
51 :pep:`0273` - Import Modules from Zip Archives
52 Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
53 follows the specification in PEP 273, but uses an implementation written by Just
54 van Rossum that uses the import hooks described in PEP 302.
55
56 :pep:`0302` - New Import Hooks
57 The PEP to add the import hooks that help this module work.
58
59
60.. _zipimporter-objects:
61
62zipimporter Objects
63-------------------
64
65
66.. class:: zipimporter(archivepath)
67
68 Create a new zipimporter instance. *archivepath* must be a path to a zipfile.
69 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
70 archive.
71
72
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
113Examples
114--------
115
116.. _zipimport-examples:
117
118Here is an example that imports a module from a ZIP archive - note that the
119:mod:`zipimport` module is not explicitly used. ::
120
121 $ unzip -l /tmp/example.zip
122 Archive: /tmp/example.zip
123 Length Date Time Name
124 -------- ---- ---- ----
125 8467 11-26-02 22:30 jwzthreading.py
126 -------- -------
127 8467 1 file
128 $ ./python
129 Python 2.3 (#1, Aug 1 2003, 19:54:32)
130 >>> import sys
131 >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
132 >>> import jwzthreading
133 >>> jwzthreading.__file__
134 '/tmp/example.zip/jwzthreading.py'
135