blob: 0e8770c53edae89b8fbefe29ab793ede5bcd5dfa [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
32Using the built-in :func:`reload` function will fail if called on a module
33loaded from a ZIP archive; it is unlikely that :func:`reload` would be needed,
34since this would imply that the ZIP has been altered during runtime.
35
Georg Brandl8ec7f652007-08-15 14:28:01 +000036.. seealso::
37
Georg Brandl02677812008-03-15 00:20:19 +000038 `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_
Georg Brandl8ec7f652007-08-15 14:28:01 +000039 Documentation on the ZIP file format by Phil Katz, the creator of the format and
40 algorithms used.
41
42 :pep:`0273` - Import Modules from Zip Archives
43 Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
44 follows the specification in PEP 273, but uses an implementation written by Just
45 van Rossum that uses the import hooks described in PEP 302.
46
47 :pep:`0302` - New Import Hooks
48 The PEP to add the import hooks that help this module work.
49
50
Georg Brandle260ba22008-01-06 16:49:50 +000051This module defines an exception:
52
53.. exception:: ZipImportError
54
55 Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
56 so it can be caught as :exc:`ImportError`, too.
57
58
Georg Brandl8ec7f652007-08-15 14:28:01 +000059.. _zipimporter-objects:
60
61zipimporter Objects
62-------------------
63
Georg Brandle260ba22008-01-06 16:49:50 +000064:class:`zipimporter` is the class for importing ZIP files.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
66.. class:: zipimporter(archivepath)
67
Georg Brandle260ba22008-01-06 16:49:50 +000068 Create a new zipimporter instance. *archivepath* must be a path to a ZIP file.
Georg Brandl8ec7f652007-08-15 14:28:01 +000069 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
70 archive.
71
Georg Brandle260ba22008-01-06 16:49:50 +000072 *archivepath* can also contain a path within the ZIP file -- the importer
73 object will then look under that path instead of the ZIP file root. For
74 example, an *archivepath* of :file:`foo/bar.zip/lib` will look for modules
75 in the :file:`lib` directory inside the ZIP file :file:`foo/bar.zip`
76 (provided that it exists).
77
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
Benjamin Petersonc7b05922008-04-25 01:29:10 +000079 .. method:: find_module(fullname[, path])
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 Search for a module specified by *fullname*. *fullname* must be the fully
82 qualified (dotted) module name. It returns the zipimporter instance itself
83 if the module was found, or :const:`None` if it wasn't. The optional
84 *path* argument is ignored---it's there for compatibility with the
85 importer protocol.
Georg Brandl8ec7f652007-08-15 14:28:01 +000086
87
Benjamin Petersonc7b05922008-04-25 01:29:10 +000088 .. method:: get_code(fullname)
Georg Brandl8ec7f652007-08-15 14:28:01 +000089
Benjamin Petersonc7b05922008-04-25 01:29:10 +000090 Return the code object for the specified module. Raise
91 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl8ec7f652007-08-15 14:28:01 +000092
93
Benjamin Petersonc7b05922008-04-25 01:29:10 +000094 .. method:: get_data(pathname)
Georg Brandl8ec7f652007-08-15 14:28:01 +000095
Benjamin Petersonc7b05922008-04-25 01:29:10 +000096 Return the data associated with *pathname*. Raise :exc:`IOError` if the
97 file wasn't found.
Georg Brandl8ec7f652007-08-15 14:28:01 +000098
99
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000100 .. method:: get_source(fullname)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000101
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000102 Return the source code for the specified module. Raise
103 :exc:`ZipImportError` if the module couldn't be found, return
104 :const:`None` if the archive does contain the module, but has no source
105 for it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000106
107
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000108 .. method:: is_package(fullname)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000109
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000110 Return True if the module specified by *fullname* is a package. Raise
111 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
113
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000114 .. method:: load_module(fullname)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000116 Load the module specified by *fullname*. *fullname* must be the fully
117 qualified (dotted) module name. It returns the imported module, or raises
118 :exc:`ZipImportError` if it wasn't found.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000119
120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 .. attribute:: archive
Georg Brandle260ba22008-01-06 16:49:50 +0000122
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000123 The file name of the importer's associated ZIP file.
Georg Brandle260ba22008-01-06 16:49:50 +0000124
125
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000126 .. attribute:: prefix
Georg Brandle260ba22008-01-06 16:49:50 +0000127
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000128 The path within the ZIP file where modules are searched; see
129 :class:`zipimporter` for details.
Georg Brandle260ba22008-01-06 16:49:50 +0000130
Georg Brandl8ec7f652007-08-15 14:28:01 +0000131
132.. _zipimport-examples:
133
Georg Brandle260ba22008-01-06 16:49:50 +0000134Examples
135--------
136
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137Here is an example that imports a module from a ZIP archive - note that the
138:mod:`zipimport` module is not explicitly used. ::
139
140 $ unzip -l /tmp/example.zip
141 Archive: /tmp/example.zip
142 Length Date Time Name
143 -------- ---- ---- ----
144 8467 11-26-02 22:30 jwzthreading.py
145 -------- -------
146 8467 1 file
147 $ ./python
148 Python 2.3 (#1, Aug 1 2003, 19:54:32)
149 >>> import sys
150 >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
151 >>> import jwzthreading
152 >>> jwzthreading.__file__
153 '/tmp/example.zip/jwzthreading.py'
154