/Doc/library/zipimport.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 157 lines · 104 code · 53 blank · 0 comment · 0 complexity · 74d27ce421c6ab79e45bec112cbce792 MD5 · raw file

  1. :mod:`zipimport` --- Import modules from Zip archives
  2. =====================================================
  3. .. module:: zipimport
  4. :synopsis: support for importing Python modules from ZIP archives.
  5. .. moduleauthor:: Just van Rossum <just@letterror.com>
  6. .. versionadded:: 2.3
  7. This module adds the ability to import Python modules (:file:`\*.py`,
  8. :file:`\*.py[co]`) and packages from ZIP-format archives. It is usually not
  9. needed to use the :mod:`zipimport` module explicitly; it is automatically used
  10. by the builtin :keyword:`import` mechanism for ``sys.path`` items that are paths
  11. to ZIP archives.
  12. Typically, ``sys.path`` is a list of directory names as strings. This module
  13. also allows an item of ``sys.path`` to be a string naming a ZIP file archive.
  14. The ZIP archive can contain a subdirectory structure to support package imports,
  15. and a path within the archive can be specified to only import from a
  16. subdirectory. For example, the path :file:`/tmp/example.zip/lib/` would only
  17. import from the :file:`lib/` subdirectory within the archive.
  18. Any files may be present in the ZIP archive, but only files :file:`.py` and
  19. :file:`.py[co]` are available for import. ZIP import of dynamic modules
  20. (:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains
  21. :file:`.py` files, Python will not attempt to modify the archive by adding the
  22. corresponding :file:`.pyc` or :file:`.pyo` file, meaning that if a ZIP archive
  23. doesn't contain :file:`.pyc` files, importing may be rather slow.
  24. Using the built-in :func:`reload` function will fail if called on a module
  25. loaded from a ZIP archive; it is unlikely that :func:`reload` would be needed,
  26. since this would imply that the ZIP has been altered during runtime.
  27. .. seealso::
  28. `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_
  29. Documentation on the ZIP file format by Phil Katz, the creator of the format and
  30. algorithms used.
  31. :pep:`0273` - Import Modules from Zip Archives
  32. Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
  33. follows the specification in PEP 273, but uses an implementation written by Just
  34. van Rossum that uses the import hooks described in PEP 302.
  35. :pep:`0302` - New Import Hooks
  36. The PEP to add the import hooks that help this module work.
  37. This module defines an exception:
  38. .. exception:: ZipImportError
  39. Exception raised by zipimporter objects. It's a subclass of :exc:`ImportError`,
  40. so it can be caught as :exc:`ImportError`, too.
  41. .. _zipimporter-objects:
  42. zipimporter Objects
  43. -------------------
  44. :class:`zipimporter` is the class for importing ZIP files.
  45. .. class:: zipimporter(archivepath)
  46. Create a new zipimporter instance. *archivepath* must be a path to a ZIP
  47. file, or to a specific path within a ZIP file. For example, an *archivepath*
  48. of :file:`foo/bar.zip/lib` will look for modules in the :file:`lib` directory
  49. inside the ZIP file :file:`foo/bar.zip` (provided that it exists).
  50. :exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
  51. archive.
  52. .. method:: find_module(fullname[, path])
  53. Search for a module specified by *fullname*. *fullname* must be the fully
  54. qualified (dotted) module name. It returns the zipimporter instance itself
  55. if the module was found, or :const:`None` if it wasn't. The optional
  56. *path* argument is ignored---it's there for compatibility with the
  57. importer protocol.
  58. .. method:: get_code(fullname)
  59. Return the code object for the specified module. Raise
  60. :exc:`ZipImportError` if the module couldn't be found.
  61. .. method:: get_data(pathname)
  62. Return the data associated with *pathname*. Raise :exc:`IOError` if the
  63. file wasn't found.
  64. .. method:: get_source(fullname)
  65. Return the source code for the specified module. Raise
  66. :exc:`ZipImportError` if the module couldn't be found, return
  67. :const:`None` if the archive does contain the module, but has no source
  68. for it.
  69. .. method:: is_package(fullname)
  70. Return True if the module specified by *fullname* is a package. Raise
  71. :exc:`ZipImportError` if the module couldn't be found.
  72. .. method:: load_module(fullname)
  73. Load the module specified by *fullname*. *fullname* must be the fully
  74. qualified (dotted) module name. It returns the imported module, or raises
  75. :exc:`ZipImportError` if it wasn't found.
  76. .. attribute:: archive
  77. The file name of the importer's associated ZIP file, without a possible
  78. subpath.
  79. .. attribute:: prefix
  80. The subpath within the ZIP file where modules are searched. This is the
  81. empty string for zipimporter objects which point to the root of the ZIP
  82. file.
  83. The :attr:`archive` and :attr:`prefix` attributes, when combined with a
  84. slash, equal the original *archivepath* argument given to the
  85. :class:`zipimporter` constructor.
  86. .. _zipimport-examples:
  87. Examples
  88. --------
  89. Here is an example that imports a module from a ZIP archive - note that the
  90. :mod:`zipimport` module is not explicitly used. ::
  91. $ unzip -l /tmp/example.zip
  92. Archive: /tmp/example.zip
  93. Length Date Time Name
  94. -------- ---- ---- ----
  95. 8467 11-26-02 22:30 jwzthreading.py
  96. -------- -------
  97. 8467 1 file
  98. $ ./python
  99. Python 2.3 (#1, Aug 1 2003, 19:54:32)
  100. >>> import sys
  101. >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
  102. >>> import jwzthreading
  103. >>> jwzthreading.__file__
  104. '/tmp/example.zip/jwzthreading.py'