zipimport
--- Zip アーカイブからモジュールを import する¶
ソースコード: Lib/zipimport.py
このモジュールは、 Python モジュール (*.py
, *.pyc
) やパッケージを ZIP 形式のアーカイブから import できるようにします。通常、 zipimport
を明示的に使う必要はありません; 組み込みの import
は、 sys.path
の要素が ZIP アーカイブへのパスを指している場合にこのモジュールを自動的に使います。
普通、 sys.path
はディレクトリ名の文字列からなるリストです。このモジュールを使うと、 sys.path
の要素に ZIP ファイルアーカイブを示す文字列を使えるようになります。ZIP アーカイブにはサブディレクトリ構造を含めることができ、パッケージの import をサポートさせたり、アーカイブ内のパスを指定してサブディレクトリ下から import を行わせたりできます。例えば、 example.zip/lib/
のように指定すると、アーカイブ中の lib/
サブディレクトリ下だけから import を行います。
Any files may be present in the ZIP archive, but importers are only invoked for
.py
and .pyc
files. ZIP import of dynamic modules
(.pyd
, .so
) is disallowed. Note that if an archive only contains
.py
files, Python will not attempt to modify the archive by adding the
corresponding .pyc
file, meaning that if a ZIP archive
doesn't contain .pyc
files, importing may be rather slow.
バージョン 3.8 で変更: 以前は、アーカイブコメント付きの ZIP アーカイブはサポートされていませんでした。
参考
- PKZIP Application Note
ZIP ファイルフォーマットおよびアルゴリズムを作成した Phil Katz によるドキュメント。
- PEP 273 - Zip アーカイブからモジュールをインポートする
このモジュールの実装も行った、James C. Ahlstrom による PEP です。Python 2.3 は PEP 273 の仕様に従っていますが、Just van Rossum の書いた import フックによる実装を使っています。インポートフックは PEP 302 で解説されています。
importlib
- The implementation of the import machineryPackage providing the relevant protocols for all importers to implement.
このモジュールでは例外を一つ定義しています:
- exception zipimport.ZipImportError¶
zipimporter オブジェクトが送出する例外です。
ImportError
のサブクラスなので、ImportError
としても捕捉できます。
zipimporter オブジェクト¶
zipimporter
は ZIP ファイルを import するためのクラスです。
- class zipimport.zipimporter(archivepath)¶
新たな zipimporter インスタンスを生成します。 archivepath は ZIP ファイルへのパスまたは ZIP ファイル中の特定のパスへのパスでなければなりません。たとえば、
foo/bar.zip/lib
という archivepath の場合、foo/bar.zip
という ZIP ファイルの中のlib
ディレクトリにあるモジュールを (存在するものとして) 検索します。archivepath が有効な ZIP アーカイブを指していない場合、
ZipImportError
を送出します。- create_module(spec)¶
Implementation of
importlib.abc.Loader.create_module()
that returnsNone
to explicitly request the default semantics.バージョン 3.10 で追加.
- exec_module(module)¶
Implementation of
importlib.abc.Loader.exec_module()
.バージョン 3.10 で追加.
- find_loader(fullname, path=None)¶
An implementation of
importlib.abc.PathEntryFinder.find_loader()
.バージョン 3.10 で非推奨: 代わりに
find_spec()
を使用してください。
- find_module(fullname, path=None)¶
fullname で指定されたモジュールを検索します。 fullname は完全に修飾された (ドット表記の) モジュール名でなければなりません。モジュールが見つかった場合には zipimporter インスタンス自体を返し、そうでない場合には
None
を返します。オプションの path 引数は無視されます --- この引数は importer プロトコルとの互換性を保つためのものです。バージョン 3.10 で非推奨: 代わりに
find_spec()
を使用してください。
- find_spec(fullname, target=None)¶
An implementation of
importlib.abc.PathEntryFinder.find_spec()
.バージョン 3.10 で追加.
- get_code(fullname)¶
Return the code object for the specified module. Raise
ZipImportError
if the module couldn't be imported.
- get_filename(fullname)¶
Return the value
__file__
would be set to if the specified module was imported. RaiseZipImportError
if the module couldn't be imported.バージョン 3.1 で追加.
- get_source(fullname)¶
fullname で指定されたモジュールのソースコードを返します。モジュールが見つからない場合、
ZipImportError
を送出します。アーカイブにはモジュールがあるもののソースコードがない場合、None
を返します。
- is_package(fullname)¶
fullname で指定されたモジュールがパッケージの場合
True
を返します。モジュールを見つけられない場合ZipImportError
を送出します。
- load_module(fullname)¶
Load the module specified by fullname. fullname must be the fully qualified (dotted) module name. Returns the imported module on success, raises
ZipImportError
on failure.バージョン 3.10 で非推奨: 代わりに
exec_module()
を使用してください。
- invalidate_caches()¶
Clear out the internal cache of information about files found within the ZIP archive.
バージョン 3.10 で追加.
- archive¶
importer に関連付けられた ZIP ファイルのファイル名です。サブパスは含まれません。
- prefix¶
モジュールを検索する ZIP ファイル中のサブパスです。この文字列は ZIP ファイルのルートを指している zipimporter オブジェクトでは空です。
スラッシュでつなげると、
archive
とprefix
属性はzipimporter
コンストラクタに渡された元々の archivepath 引数と等しくなります。
使用例¶
モジュールを ZIP アーカイブから import する例を以下に示します - zipimport
モジュールが明示的に使われていないことに注意してください。
$ unzip -l example.zip
Archive: example.zip
Length Date Time Name
-------- ---- ---- ----
8467 11-26-02 22:30 jwzthreading.py
-------- -------
8467 1 file
$ ./python
Python 2.3 (#1, Aug 1 2003, 19:54:32)
>>> import sys
>>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path
>>> import jwzthreading
>>> jwzthreading.__file__
'example.zip/jwzthreading.py'