blob: 1b23b4cf44e321b1413f896432c2b4d774247fae [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`zipimport` --- Import modules from Zip archives
2=====================================================
3
4.. module:: zipimport
5 :synopsis: support for importing Python modules from ZIP archives.
6.. moduleauthor:: Just van Rossum <just@letterror.com>
7
8
Georg Brandl116aa622007-08-15 14:28:22 +00009This module adds the ability to import Python modules (:file:`\*.py`,
10:file:`\*.py[co]`) and packages from ZIP-format archives. It is usually not
11needed to use the :mod:`zipimport` module explicitly; it is automatically used
Georg Brandlc5605df2009-08-13 08:26:44 +000012by the built-in :keyword:`import` mechanism for ``sys.path`` items that are paths
Georg Brandl116aa622007-08-15 14:28:22 +000013to ZIP archives.
14
15Typically, ``sys.path`` is a list of directory names as strings. This module
16also allows an item of ``sys.path`` to be a string naming a ZIP file archive.
17The ZIP archive can contain a subdirectory structure to support package imports,
18and a path within the archive can be specified to only import from a
19subdirectory. For example, the path :file:`/tmp/example.zip/lib/` would only
20import from the :file:`lib/` subdirectory within the archive.
21
22Any files may be present in the ZIP archive, but only files :file:`.py` and
23:file:`.py[co]` are available for import. ZIP import of dynamic modules
24(:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains
25:file:`.py` files, Python will not attempt to modify the archive by adding the
26corresponding :file:`.pyc` or :file:`.pyo` file, meaning that if a ZIP archive
27doesn't contain :file:`.pyc` files, importing may be rather slow.
28
Georg Brandl116aa622007-08-15 14:28:22 +000029.. seealso::
30
Christian Heimesdd15f6c2008-03-16 00:07:10 +000031 `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_
Georg Brandl116aa622007-08-15 14:28:22 +000032 Documentation on the ZIP file format by Phil Katz, the creator of the format and
33 algorithms used.
34
35 :pep:`0273` - Import Modules from Zip Archives
36 Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
37 follows the specification in PEP 273, but uses an implementation written by Just
38 van Rossum that uses the import hooks described in PEP 302.
39
40 :pep:`0302` - New Import Hooks
41 The PEP to add the import hooks that help this module work.
42
43
Christian Heimes7f044312008-01-06 17:05:40 +000044This module defines an exception:
45
46.. exception:: ZipImportError
47
48 Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
49 so it can be caught as :exc:`ImportError`, too.
50
51
Georg Brandl116aa622007-08-15 14:28:22 +000052.. _zipimporter-objects:
53
54zipimporter Objects
55-------------------
56
Christian Heimes7f044312008-01-06 17:05:40 +000057:class:`zipimporter` is the class for importing ZIP files.
Georg Brandl116aa622007-08-15 14:28:22 +000058
59.. class:: zipimporter(archivepath)
60
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000061 Create a new zipimporter instance. *archivepath* must be a path to a ZIP
62 file, or to a specific path within a ZIP file. For example, an *archivepath*
63 of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory
64 inside the ZIP file :file:`foo/bar.zip` (provided that it exists).
65
Georg Brandl116aa622007-08-15 14:28:22 +000066 :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
67 archive.
68
Benjamin Petersone41251e2008-04-25 01:59:09 +000069 .. method:: find_module(fullname[, path])
Georg Brandl116aa622007-08-15 14:28:22 +000070
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 Search for a module specified by *fullname*. *fullname* must be the fully
72 qualified (dotted) module name. It returns the zipimporter instance itself
73 if the module was found, or :const:`None` if it wasn't. The optional
74 *path* argument is ignored---it's there for compatibility with the
75 importer protocol.
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 .. method:: get_code(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +000079
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 Return the code object for the specified module. Raise
81 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 .. method:: get_data(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 Return the data associated with *pathname*. Raise :exc:`IOError` if the
87 file wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +000088
89
Nick Coghlan9a1d6e32009-02-08 03:37:27 +000090 .. method:: get_filename(fullname)
91
92 Return the value ``__file__`` would be set to if the specified module
93 was imported. Raise :exc:`ZipImportError` if the module couldn't be
94 found.
95
96 .. versionadded:: 3.1
97
98
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 .. method:: get_source(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 Return the source code for the specified module. Raise
102 :exc:`ZipImportError` if the module couldn't be found, return
103 :const:`None` if the archive does contain the module, but has no source
104 for it.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 .. method:: is_package(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 Return True if the module specified by *fullname* is a package. Raise
110 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 .. method:: load_module(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Benjamin Petersone41251e2008-04-25 01:59:09 +0000115 Load the module specified by *fullname*. *fullname* must be the fully
116 qualified (dotted) module name. It returns the imported module, or raises
117 :exc:`ZipImportError` if it wasn't found.
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119
Benjamin Petersone41251e2008-04-25 01:59:09 +0000120 .. attribute:: archive
Christian Heimes7f044312008-01-06 17:05:40 +0000121
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000122 The file name of the importer's associated ZIP file, without a possible
123 subpath.
Christian Heimes7f044312008-01-06 17:05:40 +0000124
125
Benjamin Petersone41251e2008-04-25 01:59:09 +0000126 .. attribute:: prefix
Christian Heimes7f044312008-01-06 17:05:40 +0000127
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000128 The subpath within the ZIP file where modules are searched. This is the
129 empty string for zipimporter objects which point to the root of the ZIP
130 file.
131
132 The :attr:`archive` and :attr:`prefix` attributes, when combined with a
133 slash, equal the original *archivepath* argument given to the
134 :class:`zipimporter` constructor.
Christian Heimes7f044312008-01-06 17:05:40 +0000135
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137.. _zipimport-examples:
138
Christian Heimes7f044312008-01-06 17:05:40 +0000139Examples
140--------
141
Georg Brandl116aa622007-08-15 14:28:22 +0000142Here is an example that imports a module from a ZIP archive - note that the
143:mod:`zipimport` module is not explicitly used. ::
144
145 $ unzip -l /tmp/example.zip
146 Archive: /tmp/example.zip
147 Length Date Time Name
148 -------- ---- ---- ----
149 8467 11-26-02 22:30 jwzthreading.py
150 -------- -------
151 8467 1 file
152 $ ./python
Georg Brandl48310cd2009-01-03 21:18:54 +0000153 Python 2.3 (#1, Aug 1 2003, 19:54:32)
Georg Brandl116aa622007-08-15 14:28:22 +0000154 >>> import sys
155 >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
156 >>> import jwzthreading
157 >>> jwzthreading.__file__
158 '/tmp/example.zip/jwzthreading.py'
159