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, Python module sources (.py) of a package and compilation artifacts (pycache) 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 asUnion[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. anchor can now be a non-package module and if omitted will default to the caller's module. package is still accepted for compatibility but will raise a
DeprecationWarning. Consider passing the anchor positionally or usingimportlib_resources >= 5.10for a compatible interface on older Pythons.
- importlib.resources.as_file(traversable)¶
Given a
Traversableobject representing a file or directory, typically fromimportlib.resources.files(), return a context manager for use in awithstatement. The context manager provides apathlib.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¶
An older, previously deprecated set of functions is still available. The main drawback of these functions is that they do not support directories: they assume all resources are located directly within a package.
- importlib.resources.Package¶
Whenever a function accepts a
Packageargument, you can pass in either amodule objector a module name as a string. You can only pass module objects whose__spec__.submodule_search_locationsis notNone.The
Packagetype is defined asUnion[str, ModuleType].
- importlib.resources.Resource¶
For resource arguments of the functions below, you can pass in the name of a resource as a string or a
path-like object.The
Resourcetype is defined asUnion[str, os.PathLike].
- importlib.resources.open_binary(package, resource)¶
Open for binary reading the resource within package.
package is either a name or a module object which conforms to the
Packagerequirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). This function returns atyping.BinaryIOinstance, a binary I/O stream open for reading.This function is roughly equivalent to:
files(package).joinpath(resource).open('rb')
- importlib.resources.open_text(package, resource, encoding='utf-8', errors='strict')¶
Open for text reading the resource within package. By default, the resource is opened for reading as UTF-8.
package is either a name or a module object which conforms to the
Packagerequirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). encoding and errors have the same meaning as with built-inopen().This function returns a
typing.TextIOinstance, a text I/O stream open for reading.This function is roughly equivalent to:
files(package).joinpath(resource).open('r', encoding=encoding)
- importlib.resources.read_binary(package, resource)¶
Read and return the contents of the resource within package as
bytes.package is either a name or a module object which conforms to the
Packagerequirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). This function returns the contents of the resource asbytes.This function is roughly equivalent to:
files(package).joinpath(resource).read_bytes()
- importlib.resources.read_text(package, resource, encoding='utf-8', errors='strict')¶
Read and return the contents of resource within package as a
str. By default, the contents are read as strict UTF-8.package is either a name or a module object which conforms to the
Packagerequirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). encoding and errors have the same meaning as with built-inopen(). This function returns the contents of the resource asstr.This function is roughly equivalent to:
files(package).joinpath(resource).read_text(encoding=encoding)
- importlib.resources.path(package, resource)¶
Return 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 apathlib.Pathobject.Exiting the context manager cleans up any temporary file created when the resource needs to be extracted from e.g. a zip file.
package is either a name or a module object which conforms to the
Packagerequirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory).This function is roughly equivalent to
as_file(files(package).joinpath(resource))
- importlib.resources.is_resource(package, name)¶
Return
Trueif there is a resource named name in the package, otherwiseFalse. This function does not consider directories to be resources. package is either a name or a module object which conforms to thePackagerequirements.This function is roughly equivalent to:
files(package).joinpath(resource).is_file()
- importlib.resources.contents(package)¶
Return an iterable over the named items within the package. The iterable returns
strresources (e.g. files) and non-resources (e.g. directories). The iterable does not recurse into subdirectories.package is either a name or a module object which conforms to the
Packagerequirements.This function is roughly equivalent to:
(resource.name for resource in files(package).iterdir() if resource.is_file())