rest25/library/zipimport.rst => rest262/library/zipimport.rst
28:file:`.py` files, Python will not attempt to modify the archive by adding the
29corresponding :file:`.pyc` or :file:`.pyo` file, meaning that if a ZIP archive
30doesn't contain :file:`.pyc` files, importing may be rather slow.
31
32Using the built-in :func:`reload` function will fail if called on a module
33loaded from a ZIP archive; it is unlikely that :func:`reload` would be needed,
34since this would imply that the ZIP has been altered during runtime.
35
n36-The available attributes of this module are:
37- 
38- 
39-.. exception:: ZipImportError
40- 
41-   Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
42-   so it can be caught as :exc:`ImportError`, too.
43- 
44- 
45-.. class:: zipimporter
46- 
47-   The class for importing ZIP files.  See "zipimporter Objects" (section
48-   :ref:`zipimporter-objects`) for constructor details.
49- 
50- 
51.. seealso::
52
n53-   `PKZIP Application Note <http://www.pkware.com/business_and_developers/developer/appnote/>`_
n38+   `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_
54      Documentation on the ZIP file format by Phil Katz, the creator of the format and
55      algorithms used.
56
57   :pep:`0273` - Import Modules from Zip Archives
58      Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
59      follows the specification in PEP 273, but uses an implementation written by Just
60      van Rossum that uses the import hooks described in PEP 302.
61
62   :pep:`0302` - New Import Hooks
63      The PEP to add the import hooks that help this module work.
64
65
n51+This module defines an exception:
52+ 
53+.. exception:: ZipImportError
54+ 
55+   Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
56+   so it can be caught as :exc:`ImportError`, too.
57+ 
58+ 
66.. _zipimporter-objects:
67
68zipimporter Objects
69-------------------
70
n64+:class:`zipimporter` is the class for importing ZIP files.
71
72.. class:: zipimporter(archivepath)
73
n74-   Create a new zipimporter instance. *archivepath* must be a path to a zipfile.
n68+   Create a new zipimporter instance. *archivepath* must be a path to a ZIP
69+   file, or to a specific path within a ZIP file.  For example, an *archivepath*
70+   of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory
71+   inside the ZIP file :file:`foo/bar.zip` (provided that it exists).
72+ 
75   :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
76   archive.
77
n76+   .. method:: find_module(fullname[, path])
78
n79-.. method:: zipimporter.find_module(fullname[, path])
80- 
81-   Search for a module specified by *fullname*. *fullname* must be the fully
78+      Search for a module specified by *fullname*. *fullname* must be the fully
82-   qualified (dotted) module name. It returns the zipimporter instance itself if
79+      qualified (dotted) module name. It returns the zipimporter instance itself
83-   the module was found, or :const:`None` if it wasn't. The optional *path*
80+      if the module was found, or :const:`None` if it wasn't. The optional
84-   argument is ignored---it's there for  compatibility with the importer protocol.
81+      *path* argument is ignored---it's there for compatibility with the
82+      importer protocol.
85
86
n87-.. method:: zipimporter.get_code(fullname)
n85+   .. method:: get_code(fullname)
88
n89-   Return the code object for the specified module. Raise :exc:`ZipImportError` if
n87+      Return the code object for the specified module. Raise
90-   the module couldn't be found.
88+      :exc:`ZipImportError` if the module couldn't be found.
91
92
n93-.. method:: zipimporter.get_data(pathname)
n91+   .. method:: get_data(pathname)
94
n95-   Return the data associated with *pathname*. Raise :exc:`IOError` if the file
n93+      Return the data associated with *pathname*. Raise :exc:`IOError` if the
96-   wasn't found.
94+      file wasn't found.
97
98
n99-.. method:: zipimporter.get_source(fullname)
n97+   .. method:: get_source(fullname)
100
n101-   Return the source code for the specified module. Raise :exc:`ZipImportError` if
n99+      Return the source code for the specified module. Raise
102-   the module couldn't be found, return :const:`None` if the archive does contain
100+      :exc:`ZipImportError` if the module couldn't be found, return
103-   the module, but has no source for it.
101+      :const:`None` if the archive does contain the module, but has no source
102+      for it.
104
105
n106-.. method:: zipimporter.is_package(fullname)
n105+   .. method:: is_package(fullname)
107
n108-   Return True if the module specified by *fullname* is a package. Raise
n107+      Return True if the module specified by *fullname* is a package. Raise
109-   :exc:`ZipImportError` if the module couldn't be found.
108+      :exc:`ZipImportError` if the module couldn't be found.
110
111
n112-.. method:: zipimporter.load_module(fullname)
n111+   .. method:: load_module(fullname)
113
n114-   Load the module specified by *fullname*. *fullname* must be the fully qualified
n113+      Load the module specified by *fullname*. *fullname* must be the fully
115-   (dotted) module name. It returns the imported module, or raises
114+      qualified (dotted) module name. It returns the imported module, or raises
116-   :exc:`ZipImportError` if it wasn't found.
115+      :exc:`ZipImportError` if it wasn't found.
117
n117+ 
118+   .. attribute:: archive
119+ 
120+      The file name of the importer's associated ZIP file, without a possible
121+      subpath.
122+ 
123+ 
124+   .. attribute:: prefix
125+ 
126+      The subpath within the ZIP file where modules are searched.  This is the
127+      empty string for zipimporter objects which point to the root of the ZIP
128+      file.
129+ 
130+   The :attr:`archive` and :attr:`prefix` attributes, when combined with a
131+   slash, equal the original *archivepath* argument given to the
132+   :class:`zipimporter` constructor.
133+ 
134+ 
135+.. _zipimport-examples:
118
119Examples
120--------
n121- 
122-.. _zipimport examples:
123
124Here is an example that imports a module from a ZIP archive - note that the
125:mod:`zipimport` module is not explicitly used. ::
126
127   $ unzip -l /tmp/example.zip
128   Archive:  /tmp/example.zip
129     Length     Date   Time    Name
130    --------    ----   ----    ----
131        8467  11-26-02 22:30   jwzthreading.py
132    --------                   -------
133        8467                   1 file
134   $ ./python
t135-   Python 2.3 (#1, Aug 1 2003, 19:54:32) 
t151+   Python 2.3 (#1, Aug 1 2003, 19:54:32)
136   >>> import sys
137   >>> sys.path.insert(0, '/tmp/example.zip')  # Add .zip file to front of path
138   >>> import jwzthreading
139   >>> jwzthreading.__file__
140   '/tmp/example.zip/jwzthreading.py'
141
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op