内容表

  • importlib.resources – Package resource reading, opening and access
    • 弃用函数

上一话题

importlib — 实现为 import

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 导入模块
  9. importlib.resources – Package resource reading, opening and access

importlib.resources – Package resource reading, opening and access ¶

源代码: Lib/importlib/resources/__init__.py


Added in version 3.7.

This module leverages Python’s import system to provide access to resources 在 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.

Resources are roughly akin to files inside directories, though it’s important to keep in mind that this is just a metaphor. Resources and packages do not have to exist as physical files and directories on the file system: for example, a package and its resources can be imported from a zip file using zipimport .

注意

This module provides functionality similar to pkg_resources Basic Resource Access without the performance overhead of that package. This makes reading resources included in packages easier, with more stable and consistent semantics.

The standalone backport of this module provides more information on using importlib.resources and migrating from pkg_resources to importlib.resources .

Loaders that wish to support resource reading should implement a get_resource_reader(fullname) method as specified by importlib.resources.abc.ResourceReader .

class importlib.resources. Anchor ¶

Represents an anchor for resources, either a module object or a module name as a string. Defined as Union[str, ModuleType] .

importlib.resources. 文件 ( anchor : Anchor | None = None ) ¶

返回 Traversable object representing the resource container (think directory) and its resources (think files). A Traversable may contain other containers (think subdirectories).

anchor 是可选 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.

Changed in version 3.12: 包 parameter was renamed to anchor . anchor can now be a non-package module and if omitted will default to the caller’s module. 包 is still accepted for compatibility but will raise a DeprecationWarning . Consider passing the anchor positionally or using importlib_resources >= 5.10 for a compatible interface on older Pythons.

importlib.resources. as_file ( traversable ) ¶

给定 Traversable object representing a file or directory, typically from importlib.resources.files() , return a context manager for use in a with statement. The context manager provides a pathlib.Path 对象。

Exiting the context manager cleans up any temporary file or directory created when the resource was extracted from e.g. a zip file.

使用 as_file when 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.

Changed in version 3.12: 添加支持 traversable representing a directory.

弃用函数 ¶

An older, deprecated set of functions is still available, but is scheduled for removal in a future version of Python. The main drawback of these functions is that they do not support directories: they assume all resources are located directly within a 包 .

importlib.resources. 包 ¶

Whenever a function accepts a Package argument, you can pass in either a module object or a module name as a string. You can only pass module objects whose __spec__.submodule_search_locations 不是 None .

The Package type is defined as Union[str, ModuleType] .

Deprecated since version 3.12.

importlib.resources. 资源 ¶

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 Resource type is defined as Union[str, os.PathLike] .

importlib.resources. open_binary ( 包 , resource ) ¶

Open for binary reading the resource 在 包 .

包 is either a name or a module object which conforms to the Package 要求。 resource is the name of the resource to open within 包 ; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). This function returns a typing.BinaryIO instance, a binary I/O stream open for reading.

Deprecated since version 3.11: Calls to this function can be replaced by:

files(package).joinpath(resource).open('rb')
													
importlib.resources. open_text ( 包 , resource , encoding = 'utf-8' , errors = 'strict' ) ¶

Open for text reading the resource 在 包 . By default, the resource is opened for reading as UTF-8.

包 is either a name or a module object which conforms to the Package 要求。 resource is the name of the resource to open within 包 ; 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-in open() .

This function returns a typing.TextIO instance, a text I/O stream open for reading.

Deprecated since version 3.11: Calls to this function can be replaced by:

files(package).joinpath(resource).open('r', encoding=encoding)
														
importlib.resources. read_binary ( 包 , resource ) ¶

Read and return the contents of the resource 在 包 as bytes .

包 is either a name or a module object which conforms to the Package 要求。 resource is the name of the resource to open within 包 ; 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 as bytes .

Deprecated since version 3.11: Calls to this function can be replaced by:

files(package).joinpath(resource).read_bytes()
															
importlib.resources. read_text ( 包 , resource , encoding = 'utf-8' , errors = 'strict' ) ¶

Read and return the contents of resource 在 包 作为 str . By default, the contents are read as strict UTF-8.

包 is either a name or a module object which conforms to the Package 要求。 resource is the name of the resource to open within 包 ; 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-in open() . This function returns the contents of the resource as str .

Deprecated since version 3.11: Calls to this function can be replaced by:

files(package).joinpath(resource).read_text(encoding=encoding)
																
importlib.resources. path ( 包 , resource ) ¶

Return the path to the resource as an actual file system path. This function returns a context manager for use in a with statement. The context manager provides a pathlib.Path 对象。

Exiting the context manager cleans up any temporary file created when the resource needs to be extracted from e.g. a zip file.

包 is either a name or a module object which conforms to the Package 要求。 resource is the name of the resource to open within 包 ; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory).

Deprecated since version 3.11: Calls to this function can be replaced using as_file() :

as_file(files(package).joinpath(resource))
																	
importlib.resources. is_resource ( 包 , name ) ¶

返回 True if there is a resource named name in the package, otherwise False . This function does not consider directories to be resources. 包 is either a name or a module object which conforms to the Package 要求。

Deprecated since version 3.11: Calls to this function can be replaced by:

files(package).joinpath(resource).is_file()
																		
importlib.resources. contents ( 包 ) ¶

Return an iterable over the named items within the package. The iterable returns str resources (e.g. files) and non-resources (e.g. directories). The iterable does not recurse into subdirectories.

包 is either a name or a module object which conforms to the Package 要求。

Deprecated since version 3.11: Calls to this function can be replaced by:

(resource.name for resource in files(package).iterdir() if resource.is_file())
																			

内容表

  • importlib.resources – Package resource reading, opening and access
    • 弃用函数

上一话题

importlib — 实现为 import

下一话题

importlib.resources.abc – 用于资源的抽象基类

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 导入模块
  9. importlib.resources – Package resource reading, opening and access

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1