cpython/Doc/library/zipimport.rst

219 lines
6.8 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:22 -03:00
:mod:`zipimport` --- Import modules from Zip archives
=====================================================
.. module:: zipimport
:synopsis: Support for importing Python modules from ZIP archives.
2007-08-15 11:28:22 -03:00
.. moduleauthor:: Just van Rossum <just@letterror.com>
**Source code:** :source:`Lib/zipimport.py`
--------------
2007-08-15 11:28:22 -03:00
This module adds the ability to import Python modules (:file:`\*.py`,
:file:`\*.pyc`) and packages from ZIP-format archives. It is usually not
2007-08-15 11:28:22 -03:00
needed to use the :mod:`zipimport` module explicitly; it is automatically used
by the built-in :keyword:`import` mechanism for :data:`sys.path` items that are paths
2007-08-15 11:28:22 -03:00
to ZIP archives.
Typically, :data:`sys.path` is a list of directory names as strings. This module
also allows an item of :data:`sys.path` to be a string naming a ZIP file archive.
2007-08-15 11:28:22 -03:00
The ZIP archive can contain a subdirectory structure to support package imports,
and a path within the archive can be specified to only import from a
subdirectory. For example, the path :file:`example.zip/lib/` would only
2007-08-15 11:28:22 -03:00
import from the :file:`lib/` subdirectory within the archive.
Any files may be present in the ZIP archive, but importers are only invoked for
:file:`.py` and :file:`.pyc` files. ZIP import of dynamic modules
2007-08-15 11:28:22 -03:00
(:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains
:file:`.py` files, Python will not attempt to modify the archive by adding the
corresponding :file:`.pyc` file, meaning that if a ZIP archive
2007-08-15 11:28:22 -03:00
doesn't contain :file:`.pyc` files, importing may be rather slow.
.. versionchanged:: 3.8
Previously, ZIP archives with an archive comment were not supported.
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77185 | andrew.kuchling | 2009-12-31 10:17:05 -0600 (Thu, 31 Dec 2009) | 1 line Add some items ........ r77186 | benjamin.peterson | 2009-12-31 10:28:24 -0600 (Thu, 31 Dec 2009) | 1 line update expat comment ........ r77187 | andrew.kuchling | 2009-12-31 10:38:53 -0600 (Thu, 31 Dec 2009) | 1 line Add various items ........ r77188 | benjamin.peterson | 2009-12-31 10:49:37 -0600 (Thu, 31 Dec 2009) | 1 line add another advancement ........ r77262 | andrew.kuchling | 2010-01-02 19:15:21 -0600 (Sat, 02 Jan 2010) | 1 line Add a few items ........ r77313 | benjamin.peterson | 2010-01-04 18:04:19 -0600 (Mon, 04 Jan 2010) | 1 line add a test about hashing array.array ........ r77317 | georg.brandl | 2010-01-05 12:14:52 -0600 (Tue, 05 Jan 2010) | 1 line Add Stefan. ........ r77331 | georg.brandl | 2010-01-06 11:43:06 -0600 (Wed, 06 Jan 2010) | 1 line Small fixes to test_cmd: fix signature of do_shell, remove duplicate import, add option to run the custom Cmd class. ........ r77332 | georg.brandl | 2010-01-06 12:02:16 -0600 (Wed, 06 Jan 2010) | 7 lines #5991: let completion for the "help" command include help topics. This also simplifies the Cmd.get_names() method implementation; it was written at a time where dir() didn't consider base class attributes. ........ r77333 | georg.brandl | 2010-01-06 12:26:08 -0600 (Wed, 06 Jan 2010) | 1 line #5950: document that zip files with comments are unsupported in zipimport. ........ r77337 | r.david.murray | 2010-01-06 21:09:08 -0600 (Wed, 06 Jan 2010) | 3 lines Add -W to the 'basics', 'opt', and 'all' test runs so that we get verbose information if a failure happens. ........ r77338 | r.david.murray | 2010-01-06 22:04:28 -0600 (Wed, 06 Jan 2010) | 2 lines Fix inadvertent checkin of debug line. ........
2010-01-09 14:53:06 -04:00
2007-08-15 11:28:22 -03:00
.. seealso::
`PKZIP Application Note <https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT>`_
2007-08-15 11:28:22 -03:00
Documentation on the ZIP file format by Phil Katz, the creator of the format and
algorithms used.
:pep:`273` - Import Modules from Zip Archives
2007-08-15 11:28:22 -03:00
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`.
2007-08-15 11:28:22 -03:00
:mod:`importlib` - The implementation of the import machinery
Package providing the relevant protocols for all importers to
implement.
2007-08-15 11:28:22 -03:00
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.
2007-08-15 11:28:22 -03:00
.. _zipimporter-objects:
zipimporter Objects
-------------------
:class:`zipimporter` is the class for importing ZIP files.
2007-08-15 11:28:22 -03:00
.. class:: zipimporter(archivepath)
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63066 | georg.brandl | 2008-05-11 10:56:04 -0400 (Sun, 11 May 2008) | 2 lines #2709 followup: better description of Tk's pros and cons. ........ r63067 | georg.brandl | 2008-05-11 11:05:13 -0400 (Sun, 11 May 2008) | 2 lines #1326: document and test zipimporter.archive and zipimporter.prefix. ........ r63068 | georg.brandl | 2008-05-11 11:07:39 -0400 (Sun, 11 May 2008) | 2 lines #2816: clarify error messages for EOF while scanning strings. ........ r63069 | georg.brandl | 2008-05-11 11:17:41 -0400 (Sun, 11 May 2008) | 3 lines #2787: Flush stdout after writing test name, helpful when running hanging or long-running tests. Patch by Adam Olsen. ........ r63070 | georg.brandl | 2008-05-11 11:20:16 -0400 (Sun, 11 May 2008) | 3 lines #2803: fix wrong invocation of heappush in seldom-reached code. Thanks to Matt Harden. ........ r63073 | benjamin.peterson | 2008-05-11 12:38:07 -0400 (Sun, 11 May 2008) | 2 lines broaden .bzrignore ........ r63076 | andrew.kuchling | 2008-05-11 15:15:52 -0400 (Sun, 11 May 2008) | 1 line Add message to test assertion ........ r63083 | andrew.kuchling | 2008-05-11 16:08:33 -0400 (Sun, 11 May 2008) | 1 line Try setting HOME env.var to fix test on Win32 ........ r63092 | georg.brandl | 2008-05-11 16:53:55 -0400 (Sun, 11 May 2008) | 2 lines #2809 followup: even better split docstring. ........ r63094 | georg.brandl | 2008-05-11 17:03:42 -0400 (Sun, 11 May 2008) | 4 lines - #2250: Exceptions raised during evaluation of names in rlcompleter's ``Completer.complete()`` method are now caught and ignored. ........ r63095 | georg.brandl | 2008-05-11 17:16:37 -0400 (Sun, 11 May 2008) | 2 lines Clarify os.strerror()s exception behavior. ........ r63097 | georg.brandl | 2008-05-11 17:34:10 -0400 (Sun, 11 May 2008) | 2 lines #2535: remove duplicated method. ........ r63104 | alexandre.vassalotti | 2008-05-11 19:04:27 -0400 (Sun, 11 May 2008) | 2 lines Moved the Queue module stub in lib-old. ........
2008-05-15 21:41:41 -03:00
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).
2007-08-15 11:28:22 -03:00
:exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
archive.
.. method:: create_module(spec)
Implementation of :meth:`importlib.abc.Loader.create_module` that returns
:const:`None` to explicitly request the default semantics.
.. versionadded:: 3.10
.. method:: exec_module(module)
Implementation of :meth:`importlib.abc.Loader.exec_module`.
.. versionadded:: 3.10
.. method:: find_loader(fullname, path=None)
An implementation of :meth:`importlib.abc.PathEntryFinder.find_loader`.
.. deprecated:: 3.10
Use :meth:`find_spec` instead.
.. method:: find_module(fullname, path=None)
2007-08-15 11:28:22 -03:00
Search for a module specified by *fullname*. *fullname* must be the fully
qualified (dotted) module name. It returns the zipimporter instance itself
if the module was found, or :const:`None` if it wasn't. The optional
*path* argument is ignored---it's there for compatibility with the
importer protocol.
2007-08-15 11:28:22 -03:00
.. deprecated:: 3.10
Use :meth:`find_spec` instead.
.. method:: find_spec(fullname, target=None)
An implementation of :meth:`importlib.abc.PathEntryFinder.find_spec`.
.. versionadded:: 3.10
2007-08-15 11:28:22 -03:00
.. method:: get_code(fullname)
2007-08-15 11:28:22 -03:00
Return the code object for the specified module. Raise
:exc:`ZipImportError` if the module couldn't be imported.
2007-08-15 11:28:22 -03:00
.. method:: get_data(pathname)
2007-08-15 11:28:22 -03:00
2011-10-12 15:10:51 -03:00
Return the data associated with *pathname*. Raise :exc:`OSError` if the
file wasn't found.
2007-08-15 11:28:22 -03:00
2011-10-12 15:10:51 -03:00
.. versionchanged:: 3.3
:exc:`IOError` used to be raised instead of :exc:`OSError`.
2007-08-15 11:28:22 -03:00
.. method:: get_filename(fullname)
Return the value ``__file__`` would be set to if the specified module
was imported. Raise :exc:`ZipImportError` if the module couldn't be
imported.
.. versionadded:: 3.1
.. method:: get_source(fullname)
2007-08-15 11:28:22 -03:00
Return the source code for the specified module. Raise
:exc:`ZipImportError` if the module couldn't be found, return
:const:`None` if the archive does contain the module, but has no source
for it.
2007-08-15 11:28:22 -03:00
.. method:: is_package(fullname)
2007-08-15 11:28:22 -03:00
Return ``True`` if the module specified by *fullname* is a package. Raise
:exc:`ZipImportError` if the module couldn't be found.
2007-08-15 11:28:22 -03:00
.. method:: load_module(fullname)
2007-08-15 11:28:22 -03:00
Load the module specified by *fullname*. *fullname* must be the fully
qualified (dotted) module name. Returns the imported module on success,
raises :exc:`ZipImportError` on failure.
2007-08-15 11:28:22 -03:00
.. deprecated:: 3.10
Use :meth:`exec_module` instead.
.. method:: invalidate_caches()
Clear out the internal cache of information about files found within
the ZIP archive.
.. versionadded:: 3.10
.. attribute:: archive
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63066 | georg.brandl | 2008-05-11 10:56:04 -0400 (Sun, 11 May 2008) | 2 lines #2709 followup: better description of Tk's pros and cons. ........ r63067 | georg.brandl | 2008-05-11 11:05:13 -0400 (Sun, 11 May 2008) | 2 lines #1326: document and test zipimporter.archive and zipimporter.prefix. ........ r63068 | georg.brandl | 2008-05-11 11:07:39 -0400 (Sun, 11 May 2008) | 2 lines #2816: clarify error messages for EOF while scanning strings. ........ r63069 | georg.brandl | 2008-05-11 11:17:41 -0400 (Sun, 11 May 2008) | 3 lines #2787: Flush stdout after writing test name, helpful when running hanging or long-running tests. Patch by Adam Olsen. ........ r63070 | georg.brandl | 2008-05-11 11:20:16 -0400 (Sun, 11 May 2008) | 3 lines #2803: fix wrong invocation of heappush in seldom-reached code. Thanks to Matt Harden. ........ r63073 | benjamin.peterson | 2008-05-11 12:38:07 -0400 (Sun, 11 May 2008) | 2 lines broaden .bzrignore ........ r63076 | andrew.kuchling | 2008-05-11 15:15:52 -0400 (Sun, 11 May 2008) | 1 line Add message to test assertion ........ r63083 | andrew.kuchling | 2008-05-11 16:08:33 -0400 (Sun, 11 May 2008) | 1 line Try setting HOME env.var to fix test on Win32 ........ r63092 | georg.brandl | 2008-05-11 16:53:55 -0400 (Sun, 11 May 2008) | 2 lines #2809 followup: even better split docstring. ........ r63094 | georg.brandl | 2008-05-11 17:03:42 -0400 (Sun, 11 May 2008) | 4 lines - #2250: Exceptions raised during evaluation of names in rlcompleter's ``Completer.complete()`` method are now caught and ignored. ........ r63095 | georg.brandl | 2008-05-11 17:16:37 -0400 (Sun, 11 May 2008) | 2 lines Clarify os.strerror()s exception behavior. ........ r63097 | georg.brandl | 2008-05-11 17:34:10 -0400 (Sun, 11 May 2008) | 2 lines #2535: remove duplicated method. ........ r63104 | alexandre.vassalotti | 2008-05-11 19:04:27 -0400 (Sun, 11 May 2008) | 2 lines Moved the Queue module stub in lib-old. ........
2008-05-15 21:41:41 -03:00
The file name of the importer's associated ZIP file, without a possible
subpath.
.. attribute:: prefix
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63066 | georg.brandl | 2008-05-11 10:56:04 -0400 (Sun, 11 May 2008) | 2 lines #2709 followup: better description of Tk's pros and cons. ........ r63067 | georg.brandl | 2008-05-11 11:05:13 -0400 (Sun, 11 May 2008) | 2 lines #1326: document and test zipimporter.archive and zipimporter.prefix. ........ r63068 | georg.brandl | 2008-05-11 11:07:39 -0400 (Sun, 11 May 2008) | 2 lines #2816: clarify error messages for EOF while scanning strings. ........ r63069 | georg.brandl | 2008-05-11 11:17:41 -0400 (Sun, 11 May 2008) | 3 lines #2787: Flush stdout after writing test name, helpful when running hanging or long-running tests. Patch by Adam Olsen. ........ r63070 | georg.brandl | 2008-05-11 11:20:16 -0400 (Sun, 11 May 2008) | 3 lines #2803: fix wrong invocation of heappush in seldom-reached code. Thanks to Matt Harden. ........ r63073 | benjamin.peterson | 2008-05-11 12:38:07 -0400 (Sun, 11 May 2008) | 2 lines broaden .bzrignore ........ r63076 | andrew.kuchling | 2008-05-11 15:15:52 -0400 (Sun, 11 May 2008) | 1 line Add message to test assertion ........ r63083 | andrew.kuchling | 2008-05-11 16:08:33 -0400 (Sun, 11 May 2008) | 1 line Try setting HOME env.var to fix test on Win32 ........ r63092 | georg.brandl | 2008-05-11 16:53:55 -0400 (Sun, 11 May 2008) | 2 lines #2809 followup: even better split docstring. ........ r63094 | georg.brandl | 2008-05-11 17:03:42 -0400 (Sun, 11 May 2008) | 4 lines - #2250: Exceptions raised during evaluation of names in rlcompleter's ``Completer.complete()`` method are now caught and ignored. ........ r63095 | georg.brandl | 2008-05-11 17:16:37 -0400 (Sun, 11 May 2008) | 2 lines Clarify os.strerror()s exception behavior. ........ r63097 | georg.brandl | 2008-05-11 17:34:10 -0400 (Sun, 11 May 2008) | 2 lines #2535: remove duplicated method. ........ r63104 | alexandre.vassalotti | 2008-05-11 19:04:27 -0400 (Sun, 11 May 2008) | 2 lines Moved the Queue module stub in lib-old. ........
2008-05-15 21:41:41 -03:00
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.
2007-08-15 11:28:22 -03:00
.. _zipimport-examples:
Examples
--------
2007-08-15 11:28:22 -03:00
Here is an example that imports a module from a ZIP archive - note that the
:mod:`zipimport` module is not explicitly used.
.. code-block:: shell-session
2007-08-15 11:28:22 -03:00
$ unzip -l example.zip
Archive: example.zip
2007-08-15 11:28:22 -03:00
Length Date Time Name
-------- ---- ---- ----
8467 11-26-02 22:30 jwzthreading.py
-------- -------
8467 1 file
$ ./python
2009-01-03 17:18:54 -04:00
Python 2.3 (#1, Aug 1 2003, 19:54:32)
2007-08-15 11:28:22 -03:00
>>> import sys
>>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path
2007-08-15 11:28:22 -03:00
>>> import jwzthreading
>>> jwzthreading.__file__
'example.zip/jwzthreading.py'