| :file:`.py` files, Python will not attempt to modify the archive by adding the |
| corresponding :file:`.pyc` or :file:`.pyo` file, meaning that if a ZIP archive |
| doesn't contain :file:`.pyc` files, importing may be rather slow. |
| |
| Using the built-in :func:`reload` function will fail if called on a module |
| loaded from a ZIP archive; it is unlikely that :func:`reload` would be needed, |
| since this would imply that the ZIP has been altered during runtime. |
| |
n | The available attributes of this module are: |
| |
| |
| .. exception:: ZipImportError |
| |
| Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`, |
| so it can be caught as :exc:`ImportError`, too. |
| |
| |
| .. class:: zipimporter |
| |
| The class for importing ZIP files. See "zipimporter Objects" (section |
| :ref:`zipimporter-objects`) for constructor details. |
| |
| |
| .. seealso:: |
| |
n | `PKZIP Application Note <http://www.pkware.com/business_and_developers/developer/appnote/>`_ |
n | `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_ |
| Documentation on the ZIP file format by Phil Katz, the creator of the format and |
| algorithms used. |
| |
| :pep:`0273` - Import Modules from Zip Archives |
| Written by James C. Ahlstrom, who also provided an implementation. Python 2.3 |
| follows the specification in PEP 273, but uses an implementation written by Just |
| van Rossum that uses the import hooks described in PEP 302. |
| |
| :pep:`0302` - New Import Hooks |
| The PEP to add the import hooks that help this module work. |
| |
| |
n | This module defines an exception: |
| |
| .. exception:: ZipImportError |
| |
| Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`, |
| so it can be caught as :exc:`ImportError`, too. |
| |
| |
| .. _zipimporter-objects: |
| |
| zipimporter Objects |
| ------------------- |
| |
n | :class:`zipimporter` is the class for importing ZIP files. |
| |
| .. class:: zipimporter(archivepath) |
| |
n | Create a new zipimporter instance. *archivepath* must be a path to a zipfile. |
n | Create a new zipimporter instance. *archivepath* must be a path to a ZIP |
| file, or to a specific path within a ZIP file. For example, an *archivepath* |
| of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory |
| inside the ZIP file :file:`foo/bar.zip` (provided that it exists). |
| |
| :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP |
| archive. |
| |
n | .. method:: find_module(fullname[, path]) |
| |
n | .. method:: zipimporter.find_module(fullname[, path]) |
| |
| Search for a module specified by *fullname*. *fullname* must be the fully |
| Search for a module specified by *fullname*. *fullname* must be the fully |
| qualified (dotted) module name. It returns the zipimporter instance itself if |
| qualified (dotted) module name. It returns the zipimporter instance itself |
| the module was found, or :const:`None` if it wasn't. The optional *path* |
| if the module was found, or :const:`None` if it wasn't. The optional |
| argument is ignored---it's there for compatibility with the importer protocol. |
| *path* argument is ignored---it's there for compatibility with the |
| importer protocol. |
| |
| |
n | .. method:: zipimporter.get_code(fullname) |
n | .. method:: get_code(fullname) |
| |
n | Return the code object for the specified module. Raise :exc:`ZipImportError` if |
n | Return the code object for the specified module. Raise |
| the module couldn't be found. |
| :exc:`ZipImportError` if the module couldn't be found. |
| |
| |
n | .. method:: zipimporter.get_data(pathname) |
n | .. method:: get_data(pathname) |
| |
n | Return the data associated with *pathname*. Raise :exc:`IOError` if the file |
n | Return the data associated with *pathname*. Raise :exc:`IOError` if the |
| wasn't found. |
| file wasn't found. |
| |
| |
n | .. method:: zipimporter.get_source(fullname) |
n | .. method:: get_source(fullname) |
| |
n | Return the source code for the specified module. Raise :exc:`ZipImportError` if |
n | Return the source code for the specified module. Raise |
| the module couldn't be found, return :const:`None` if the archive does contain |
| :exc:`ZipImportError` if the module couldn't be found, return |
| the module, but has no source for it. |
| :const:`None` if the archive does contain the module, but has no source |
| for it. |
| |
| |
n | .. method:: zipimporter.is_package(fullname) |
n | .. method:: is_package(fullname) |
| |
n | Return True if the module specified by *fullname* is a package. Raise |
n | Return True if the module specified by *fullname* is a package. Raise |
| :exc:`ZipImportError` if the module couldn't be found. |
| :exc:`ZipImportError` if the module couldn't be found. |
| |
| |
n | .. method:: zipimporter.load_module(fullname) |
n | .. method:: load_module(fullname) |
| |
n | Load the module specified by *fullname*. *fullname* must be the fully qualified |
n | Load the module specified by *fullname*. *fullname* must be the fully |
| (dotted) module name. It returns the imported module, or raises |
| qualified (dotted) module name. It returns the imported module, or raises |
| :exc:`ZipImportError` if it wasn't found. |
| :exc:`ZipImportError` if it wasn't found. |
| |
n | |
| .. attribute:: archive |
| |
| The file name of the importer's associated ZIP file, without a possible |
| subpath. |
| |
| |
| .. attribute:: prefix |
| |
| The subpath within the ZIP file where modules are searched. This is the |
| empty string for zipimporter objects which point to the root of the ZIP |
| file. |
| |
| The :attr:`archive` and :attr:`prefix` attributes, when combined with a |
| slash, equal the original *archivepath* argument given to the |
| :class:`zipimporter` constructor. |
| |
| |
| .. _zipimport-examples: |
| |
| Examples |
| -------- |
n | |
| .. _zipimport examples: |
| |
| Here is an example that imports a module from a ZIP archive - note that the |
| :mod:`zipimport` module is not explicitly used. :: |
| |
| $ unzip -l /tmp/example.zip |
| Archive: /tmp/example.zip |
| Length Date Time Name |
| -------- ---- ---- ---- |
| 8467 11-26-02 22:30 jwzthreading.py |
| -------- ------- |
| 8467 1 file |
| $ ./python |
t | Python 2.3 (#1, Aug 1 2003, 19:54:32) |
t | Python 2.3 (#1, Aug 1 2003, 19:54:32) |
| >>> import sys |
| >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path |
| >>> import jwzthreading |
| >>> jwzthreading.__file__ |
| '/tmp/example.zip/jwzthreading.py' |
| |