blob: 8ebe3c5b6468e3b0a7177324ae3adb5fe1086337 [file] [log] [blame]
Fred Drakeffcbab02005-01-19 05:42:50 +00001\section{\module{zipimport} ---
2 Import modules from Zip archives}
3
4\declaremodule{standard}{zipimport}
5\modulesynopsis{support for importing Python modules from ZIP archives.}
6\moduleauthor{Just van Rossum}{just@letterror.com}
7
8\versionadded{2.3}
9
10This module adds the ability to import Python modules (\file{*.py},
11\file{*.py[co]}) and packages from ZIP-format archives. It is usually
12not needed to use the \module{zipimport} module explicitly; it is
13automatically used by the builtin \keyword{import} mechanism for
14\code{sys.path} items that are paths to ZIP archives.
15
16Typically, \code{sys.path} is a list of directory names as strings. This
17module also allows an item of \code{sys.path} to be a string naming a ZIP
18file archive. The ZIP archive can contain a subdirectory structure to
19support package imports, and a path within the archive can be specified to
20only import from a subdirectory. For example, the path
21\file{/tmp/example.zip/lib/} would only import from the
22\file{lib/} subdirectory within the archive.
23
24Any files may be present in the ZIP archive, but only files \file{.py} and
25\file{.py[co]} are available for import. ZIP import of dynamic modules
26(\file{.pyd}, \file{.so}) is disallowed. Note that if an archive only
27contains \file{.py} files, Python will not attempt to modify the archive
28by adding the corresponding \file{.pyc} or \file{.pyo} file, meaning that
29if a ZIP archive doesn't contain \file{.pyc} files, importing may be rather
30slow.
31
Fred Drakeffcbab02005-01-19 05:42:50 +000032The available attributes of this module are:
33
Fredrik Lundh26075a12006-01-15 14:59:55 +000034\begin{excdesc}{ZipImportError}
Fred Drakeffcbab02005-01-19 05:42:50 +000035 Exception raised by zipimporter objects. It's a subclass of
36 \exception{ImportError}, so it can be caught as \exception{ImportError},
37 too.
38\end{excdesc}
39
40\begin{classdesc*}{zipimporter}
41 The class for importing ZIP files. See
42 ``\citetitle{zipimporter Objects}'' (section \ref{zipimporter-objects})
43 for constructor details.
44\end{classdesc*}
45
46
47\begin{seealso}
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048 \seetitle[http://www.pkware.com/business_and_developers/developer/appnote/]
49 {PKZIP Application Note}{Documentation on the ZIP file format by
50 Phil Katz, the creator of the format and algorithms used.}
Fred Drakeffcbab02005-01-19 05:42:50 +000051
52 \seepep{0273}{Import Modules from Zip Archives}{Written by James C.
53 Ahlstrom, who also provided an implementation. Python 2.3
54 follows the specification in PEP 273, but uses an
55 implementation written by Just van Rossum that uses the import
56 hooks described in PEP 302.}
57
58 \seepep{0302}{New Import Hooks}{The PEP to add the import hooks that help
59 this module work.}
60\end{seealso}
61
62
63\subsection{zipimporter Objects \label{zipimporter-objects}}
64
65\begin{classdesc}{zipimporter}{archivepath}
66 Create a new zipimporter instance. \var{archivepath} must be a path to
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000067 a zipfile. \exception{ZipImportError} is raised if \var{archivepath}
68 doesn't point to a valid ZIP archive.
Fred Drakeffcbab02005-01-19 05:42:50 +000069\end{classdesc}
70
71\begin{methoddesc}{find_module}{fullname\optional{, path}}
72 Search for a module specified by \var{fullname}. \var{fullname} must be
73 the fully qualified (dotted) module name. It returns the zipimporter
74 instance itself if the module was found, or \constant{None} if it wasn't.
75 The optional \var{path} argument is ignored---it's there for
76 compatibility with the importer protocol.
77\end{methoddesc}
78
79\begin{methoddesc}{get_code}{fullname}
80 Return the code object for the specified module. Raise
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081 \exception{ZipImportError} if the module couldn't be found.
Fred Drakeffcbab02005-01-19 05:42:50 +000082\end{methoddesc}
83
84\begin{methoddesc}{get_data}{pathname}
85 Return the data associated with \var{pathname}. Raise \exception{IOError}
86 if the file wasn't found.
87\end{methoddesc}
88
89\begin{methoddesc}{get_source}{fullname}
90 Return the source code for the specified module. Raise
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000091 \exception{ZipImportError} if the module couldn't be found, return
Fred Drakeffcbab02005-01-19 05:42:50 +000092 \constant{None} if the archive does contain the module, but has
93 no source for it.
94\end{methoddesc}
95
96\begin{methoddesc}{is_package}{fullname}
97 Return True if the module specified by \var{fullname} is a package.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098 Raise \exception{ZipImportError} if the module couldn't be found.
Fred Drakeffcbab02005-01-19 05:42:50 +000099\end{methoddesc}
100
101\begin{methoddesc}{load_module}{fullname}
102 Load the module specified by \var{fullname}. \var{fullname} must be the
103 fully qualified (dotted) module name. It returns the imported
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000104 module, or raises \exception{ZipImportError} if it wasn't found.
Fred Drakeffcbab02005-01-19 05:42:50 +0000105\end{methoddesc}
106
107\subsection{Examples}
108\nodename{zipimport Examples}
109
110Here is an example that imports a module from a ZIP archive - note that
111the \module{zipimport} module is not explicitly used.
112
113\begin{verbatim}
114$ unzip -l /tmp/example.zip
115Archive: /tmp/example.zip
116 Length Date Time Name
117 -------- ---- ---- ----
118 8467 11-26-02 22:30 jwzthreading.py
119 -------- -------
120 8467 1 file
121$ ./python
122Python 2.3 (#1, Aug 1 2003, 19:54:32)
123>>> import sys
124>>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
125>>> import jwzthreading
126>>> jwzthreading.__file__
127'/tmp/example.zip/jwzthreading.py'
128\end{verbatim}