importlib.resources -- パッケージリソースの読み取り、オープン、アクセス¶
ソースコード: Lib/importlib/resources/__init__.py
Added in version 3.7.
This module leverages Python's import system to provide access to resources within packages.
"Resources" are file-like resources associated with a module or package in
Python. The resources may be contained directly in a package, within a
subdirectory contained in that package, or adjacent to modules outside a
package. Resources may be text or binary. As a result, a package's Python
module sources (.py), compilation artifacts (pycache), and installation
artifacts (like reserved filenames
in directories) are technically de-facto resources of that package.
In practice, however, resources are primarily those non-Python artifacts
exposed specifically by the package author.
Resources can be opened or read in either binary or text mode.
リソースは大体ディレクトリの中のファイルに似ていますが、これは単なる例え話であることを頭に入れておくことが重要です。例えば、パッケージとそのリソースは zipimport を使って zip ファイルからインポートすることができます。
注釈
このモジュールは、 pkg_resources Basic Resource Access に似た機能を、そのパッケージのパフォーマンスのオーバーヘッドを伴わずに提供します。 これにより、パッケージに含まれるリソースの読み込みがより簡単になり、より安定した一貫した意味付けができるようになります。
このモジュールのスタンドアローンバックポートでは、 importlib.resources の使用 と pkg_resources から importlib.resources への移行 についての詳細情報を提供しています。
Loaders でリソースの読み込みをサポートしたい場合は、 importlib.resources.abc.ResourceReader で指定された get_resource_reader(fullname) メソッドを実装しなければいけません。
- class importlib.resources.Anchor¶
- Represents an anchor for resources, either a - module objector a module name as a string. Defined as- Union[str, ModuleType].
- importlib.resources.files(anchor: Anchor | None = None)¶
- Returns a - Traversableobject representing the resource container (think directory) and its resources (think files). A Traversable may contain other containers (think subdirectories).- anchor is an optional - Anchor. If the anchor is a package, resources are resolved from that package. If a module, resources are resolved adjacent to that module (in the same package or the package root). If the anchor is omitted, the caller's module is used.- Added in version 3.9. - バージョン 3.12 で変更: package parameter was renamed to anchor. package was still accepted, but deprecated. - バージョン 3.15 で変更: package parameter was fully removed. anchor can now be a non-package module and if omitted will default to the caller's module. package is no longer accepted since Python 3.15. Consider passing the anchor positionally or using - importlib_resources >= 5.10for a compatible interface on older Pythons.
- importlib.resources.as_file(traversable)¶
- Given a - Traversableobject representing a file or directory, typically from- importlib.resources.files(), return a context manager for use in a- withstatement. The context manager provides a- pathlib.Pathobject.- Exiting the context manager cleans up any temporary file or directory created when the resource was extracted from e.g. a zip file. - Use - as_filewhen the Traversable methods (- read_text, etc) are insufficient and an actual file or directory on the file system is required.- Added in version 3.9. - バージョン 3.12 で変更: Added support for traversable representing a directory. 
関数 API¶
A set of simplified, backwards-compatible helpers is available. These allow common operations in a single function call.
For all the following functions:
- anchor is an - Anchor, as in- files(). Unlike in- files, it may not be omitted.
- path_names are components of a resource's path name, relative to the anchor. For example, to get the text of resource named - info.txt, use:- importlib.resources.read_text(my_module, "info.txt") - Like - Traversable.joinpath, The individual components should use forward slashes (- /) as path separators. For example, the following are equivalent:- importlib.resources.read_binary(my_module, "pics/painting.png") importlib.resources.read_binary(my_module, "pics", "painting.png") - For backward compatibility reasons, functions that read text require an explicit encoding argument if multiple path_names are given. For example, to get the text of - info/chapter1.txt, use:- importlib.resources.read_text(my_module, "info", "chapter1.txt", encoding='utf-8') 
- importlib.resources.open_binary(anchor, *path_names)¶
- Open the named resource for binary reading. - See the introduction for details on anchor and path_names. - This function returns a - BinaryIOobject, that is, a binary stream open for reading.- This function is roughly equivalent to: - files(anchor).joinpath(*path_names).open('rb') - バージョン 3.13 で変更: Multiple path_names are accepted. 
- importlib.resources.open_text(anchor, *path_names, encoding='utf-8', errors='strict')¶
- Open the named resource for text reading. By default, the contents are read as strict UTF-8. - See the introduction for details on anchor and path_names. encoding and errors have the same meaning as in built-in - open().- For backward compatibility reasons, the encoding argument must be given explicitly if there are multiple path_names. This limitation is scheduled to be removed in Python 3.15. - This function returns a - TextIOobject, that is, a text stream open for reading.- This function is roughly equivalent to: - files(anchor).joinpath(*path_names).open('r', encoding=encoding) - バージョン 3.13 で変更: Multiple path_names are accepted. encoding and errors must be given as keyword arguments. 
- importlib.resources.read_binary(anchor, *path_names)¶
- Read and return the contents of the named resource as - bytes.- See the introduction for details on anchor and path_names. - This function is roughly equivalent to: - files(anchor).joinpath(*path_names).read_bytes() - バージョン 3.13 で変更: Multiple path_names are accepted. 
- importlib.resources.read_text(anchor, *path_names, encoding='utf-8', errors='strict')¶
- Read and return the contents of the named resource as - str. By default, the contents are read as strict UTF-8.- See the introduction for details on anchor and path_names. encoding and errors have the same meaning as in built-in - open().- For backward compatibility reasons, the encoding argument must be given explicitly if there are multiple path_names. This limitation is scheduled to be removed in Python 3.15. - This function is roughly equivalent to: - files(anchor).joinpath(*path_names).read_text(encoding=encoding) - バージョン 3.13 で変更: Multiple path_names are accepted. encoding and errors must be given as keyword arguments. 
- importlib.resources.path(anchor, *path_names)¶
- Provides the path to the resource as an actual file system path. This function returns a context manager for use in a - withstatement. The context manager provides a- pathlib.Pathobject.- Exiting the context manager cleans up any temporary files created, e.g. when the resource needs to be extracted from a zip file. - For example, the - stat()method requires an actual file system path; it can be used like this:- with importlib.resources.path(anchor, "resource.txt") as fspath: result = fspath.stat() - See the introduction for details on anchor and path_names. - This function is roughly equivalent to: - as_file(files(anchor).joinpath(*path_names)) - バージョン 3.13 で変更: Multiple path_names are accepted. encoding and errors must be given as keyword arguments. 
- importlib.resources.is_resource(anchor, *path_names)¶
- Return - Trueif the named resource exists, otherwise- False. This function does not consider directories to be resources.- See the introduction for details on anchor and path_names. - This function is roughly equivalent to: - files(anchor).joinpath(*path_names).is_file() - バージョン 3.13 で変更: Multiple path_names are accepted. 
- importlib.resources.contents(anchor, *path_names)¶
- Return an iterable over the named items within the package or path. The iterable returns names of resources (e.g. files) and non-resources (e.g. directories) as - str. The iterable does not recurse into subdirectories.- See the introduction for details on anchor and path_names. - This function is roughly equivalent to: - for resource in files(anchor).joinpath(*path_names).iterdir(): yield resource.name - バージョン 3.11 で非推奨: Prefer - iterdir()as above, which offers more control over the results and richer functionality.