blob: 3fd0595db5677848fb78445ebfc661886e81948f [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
Nick Coghlan9a1d6e32009-02-08 03:37:27 +000091 .. method:: get_filename(fullname)
92
93 Return the value ``__file__`` would be set to if the specified module
94 was imported. Raise :exc:`ZipImportError` if the module couldn't be
95 found.
96
97 .. versionadded:: 3.1
98
99
Benjamin Petersone41251e2008-04-25 01:59:09 +0000100 .. method:: get_source(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000106
107
Benjamin Petersone41251e2008-04-25 01:59:09 +0000108 .. method:: is_package(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 Return True if the module specified by *fullname* is a package. Raise
111 :exc:`ZipImportError` if the module couldn't be found.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 .. method:: load_module(fullname)
Georg Brandl116aa622007-08-15 14:28:22 +0000115
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000119
120
Benjamin Petersone41251e2008-04-25 01:59:09 +0000121 .. attribute:: archive
Christian Heimes7f044312008-01-06 17:05:40 +0000122
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000123 The file name of the importer's associated ZIP file, without a possible
124 subpath.
Christian Heimes7f044312008-01-06 17:05:40 +0000125
126
Benjamin Petersone41251e2008-04-25 01:59:09 +0000127 .. attribute:: prefix
Christian Heimes7f044312008-01-06 17:05:40 +0000128
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000129 The subpath within the ZIP file where modules are searched. This is the
130 empty string for zipimporter objects which point to the root of the ZIP
131 file.
132
133 The :attr:`archive` and :attr:`prefix` attributes, when combined with a
134 slash, equal the original *archivepath* argument given to the
135 :class:`zipimporter` constructor.
Christian Heimes7f044312008-01-06 17:05:40 +0000136
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138.. _zipimport-examples:
139
Christian Heimes7f044312008-01-06 17:05:40 +0000140Examples
141--------
142
Georg Brandl116aa622007-08-15 14:28:22 +0000143Here is an example that imports a module from a ZIP archive - note that the
144:mod:`zipimport` module is not explicitly used. ::
145
146 $ unzip -l /tmp/example.zip
147 Archive: /tmp/example.zip
148 Length Date Time Name
149 -------- ---- ---- ----
150 8467 11-26-02 22:30 jwzthreading.py
151 -------- -------
152 8467 1 file
153 $ ./python
Georg Brandl48310cd2009-01-03 21:18:54 +0000154 Python 2.3 (#1, Aug 1 2003, 19:54:32)
Georg Brandl116aa622007-08-15 14:28:22 +0000155 >>> import sys
156 >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
157 >>> import jwzthreading
158 >>> jwzthreading.__file__
159 '/tmp/example.zip/jwzthreading.py'
160