blob: 82383bf49c73eedec569f831c6d97ec17050cbea [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
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000062 Create a new zipimporter instance. *archivepath* must be a path to a ZIP
63 file, or to a specific path within a ZIP file. For example, an *archivepath*
64 of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory
65 inside the ZIP file :file:`foo/bar.zip` (provided that it exists).
66
Georg Brandl116aa622007-08-15 14:28:22 +000067 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
68 archive.
69
Benjamin Petersone41251e2008-04-25 01:59:09 +000070 .. method:: find_module(fullname[, path])
Georg Brandl116aa622007-08-15 14:28:22 +000071
Benjamin Petersone41251e2008-04-25 01:59:09 +000072 Search for a module specified by *fullname*. *fullname* must be the fully
73 qualified (dotted) module name. It returns the zipimporter instance itself
74 if the module was found, or :const:`None` if it wasn't. The optional
75 *path* argument is ignored---it's there for compatibility with the
76 importer protocol.
Georg Brandl116aa622007-08-15 14:28:22 +000077
78
Benjamin Petersone41251e2008-04-25 01:59:09 +000079 .. method:: get_code(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +000080
Benjamin Petersone41251e2008-04-25 01:59:09 +000081 Return the code object for the specified module. Raise
82 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +000083
84
Benjamin Petersone41251e2008-04-25 01:59:09 +000085 .. method:: get_data(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000086
Benjamin Petersone41251e2008-04-25 01:59:09 +000087 Return the data associated with *pathname*. Raise :exc:`IOError` if the
88 file wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +000089
90
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 .. method:: get_source(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +000092
Benjamin Petersone41251e2008-04-25 01:59:09 +000093 Return the source code for the specified module. Raise
94 :exc:`ZipImportError` if the module couldn't be found, return
95 :const:`None` if the archive does contain the module, but has no source
96 for it.
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 .. method:: is_package(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 Return True if the module specified by *fullname* is a package. Raise
102 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104
Benjamin Petersone41251e2008-04-25 01:59:09 +0000105 .. method:: load_module(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 Load the module specified by *fullname*. *fullname* must be the fully
108 qualified (dotted) module name. It returns the imported module, or raises
109 :exc:`ZipImportError` if it wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111
Benjamin Petersone41251e2008-04-25 01:59:09 +0000112 .. attribute:: archive
Christian Heimes7f044312008-01-06 17:05:40 +0000113
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000114 The file name of the importer's associated ZIP file, without a possible
115 subpath.
Christian Heimes7f044312008-01-06 17:05:40 +0000116
117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 .. attribute:: prefix
Christian Heimes7f044312008-01-06 17:05:40 +0000119
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000120 The subpath within the ZIP file where modules are searched. This is the
121 empty string for zipimporter objects which point to the root of the ZIP
122 file.
123
124 The :attr:`archive` and :attr:`prefix` attributes, when combined with a
125 slash, equal the original *archivepath* argument given to the
126 :class:`zipimporter` constructor.
Christian Heimes7f044312008-01-06 17:05:40 +0000127
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129.. _zipimport-examples:
130
Christian Heimes7f044312008-01-06 17:05:40 +0000131Examples
132--------
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134Here is an example that imports a module from a ZIP archive - note that the
135:mod:`zipimport` module is not explicitly used. ::
136
137 $ unzip -l /tmp/example.zip
138 Archive: /tmp/example.zip
139 Length Date Time Name
140 -------- ---- ---- ----
141 8467 11-26-02 22:30 jwzthreading.py
142 -------- -------
143 8467 1 file
144 $ ./python
145 Python 2.3 (#1, Aug 1 2003, 19:54:32)
146 >>> import sys
147 >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
148 >>> import jwzthreading
149 >>> jwzthreading.__file__
150 '/tmp/example.zip/jwzthreading.py'
151