Fred Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 1 | \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 | |
| 10 | This module adds the ability to import Python modules (\file{*.py}, |
| 11 | \file{*.py[co]}) and packages from ZIP-format archives. It is usually |
| 12 | not needed to use the \module{zipimport} module explicitly; it is |
| 13 | automatically used by the builtin \keyword{import} mechanism for |
| 14 | \code{sys.path} items that are paths to ZIP archives. |
| 15 | |
| 16 | Typically, \code{sys.path} is a list of directory names as strings. This |
| 17 | module also allows an item of \code{sys.path} to be a string naming a ZIP |
| 18 | file archive. The ZIP archive can contain a subdirectory structure to |
| 19 | support package imports, and a path within the archive can be specified to |
| 20 | only 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 | |
| 24 | Any 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 |
| 27 | contains \file{.py} files, Python will not attempt to modify the archive |
| 28 | by adding the corresponding \file{.pyc} or \file{.pyo} file, meaning that |
| 29 | if a ZIP archive doesn't contain \file{.pyc} files, importing may be rather |
| 30 | slow. |
| 31 | |
Fred Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 32 | The available attributes of this module are: |
| 33 | |
Fredrik Lundh | 26075a1 | 2006-01-15 14:59:55 +0000 | [diff] [blame] | 34 | \begin{excdesc}{ZipImportError} |
Fred Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 35 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 48 | \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 Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 51 | |
| 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 67 | a zipfile. \exception{ZipImportError} is raised if \var{archivepath} |
| 68 | doesn't point to a valid ZIP archive. |
Fred Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 69 | \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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 81 | \exception{ZipImportError} if the module couldn't be found. |
Fred Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 82 | \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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | \exception{ZipImportError} if the module couldn't be found, return |
Fred Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 92 | \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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 98 | Raise \exception{ZipImportError} if the module couldn't be found. |
Fred Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 99 | \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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 104 | module, or raises \exception{ZipImportError} if it wasn't found. |
Fred Drake | ffcbab0 | 2005-01-19 05:42:50 +0000 | [diff] [blame] | 105 | \end{methoddesc} |
| 106 | |
| 107 | \subsection{Examples} |
| 108 | \nodename{zipimport Examples} |
| 109 | |
| 110 | Here is an example that imports a module from a ZIP archive - note that |
| 111 | the \module{zipimport} module is not explicitly used. |
| 112 | |
| 113 | \begin{verbatim} |
| 114 | $ unzip -l /tmp/example.zip |
| 115 | Archive: /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 |
| 122 | Python 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} |