2023-05-06 03:40:19 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								:mod:`importlib.resources` -- Package resource reading, opening and access
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								--------------------------------------------------------------------------
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								.. module:: importlib.resources
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    :synopsis: Package resource reading, opening, and access
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-06-21 21:55:18 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								**Source code:** :source:`Lib/importlib/resources/__init__.py`
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								--------------
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								.. versionadded:: 3.7
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								This module leverages Python's import system to provide access to *resources*
							 | 
						
					
						
							
								
									
										
										
										
											2022-11-26 16:57:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								within *packages*.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								"Resources" are file-like resources associated with a module or package in
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								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.
							 | 
						
					
						
							
								
									
										
										
										
											2022-11-26 16:57:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								Resources can be opened or read in either binary or text mode.
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								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
							 | 
						
					
						
							
								
									
										
										
										
											2022-07-25 18:16:17 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								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
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								:py:mod:`zipimport`.
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								.. note::
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   This module provides functionality similar to `pkg_resources
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   <https://setuptools.readthedocs.io/en/latest/pkg_resources.html>`_ `Basic
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   Resource Access
							 | 
						
					
						
							
								
									
										
										
										
											2022-08-04 10:13:49 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								   <https://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access>`_
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   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
							 | 
						
					
						
							
								
									
										
										
										
											2022-08-04 10:13:49 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								   <https://importlib-resources.readthedocs.io/en/latest/using.html>`_ and
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   `migrating from pkg_resources to importlib.resources
							 | 
						
					
						
							
								
									
										
										
										
											2022-08-04 10:13:49 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								   <https://importlib-resources.readthedocs.io/en/latest/migration.html>`_.
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-07-25 18:16:17 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								:class:`Loaders <importlib.abc.Loader>` that wish to support resource reading should implement a
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								``get_resource_reader(fullname)`` method as specified by
							 | 
						
					
						
							
								
									
										
										
										
											2022-07-25 18:16:17 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								:class:`importlib.resources.abc.ResourceReader`.
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-26 01:40:19 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								.. class:: Anchor
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    Represents an anchor for resources, either a :class:`module object
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    <types.ModuleType>` or a module name as a string. Defined as
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ``Union[str, ModuleType]``.
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								.. function:: files(anchor: Optional[Anchor] = None)
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-07-25 18:16:17 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    Returns a :class:`~importlib.resources.abc.Traversable` object
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    representing the resource container (think directory) and its resources
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    (think files). A Traversable may contain other containers (think
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    subdirectories).
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-26 01:40:19 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    *anchor* is an optional :class:`Anchor`. If the anchor is a
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    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.
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .. versionadded:: 3.9
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    .. versionchanged:: 3.12
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-26 01:40:19 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								       *package* parameter was renamed to *anchor*. *anchor* can now
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								       be a non-package module and if omitted will default to the caller's
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-26 01:40:19 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								       module. *package* is still accepted for compatibility but will raise
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								       a :exc:`DeprecationWarning`. Consider passing the anchor positionally or
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								       using ``importlib_resources >= 5.10`` for a compatible interface
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								       on older Pythons.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								.. function:: as_file(traversable)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-07-25 18:16:17 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    Given a :class:`~importlib.resources.abc.Traversable` object representing
							 | 
						
					
						
							
								
									
										
										
										
											2023-09-06 22:53:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    a file or directory, typically from :func:`importlib.resources.files`,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return a context manager for use in a :keyword:`with` statement.
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    The context manager provides a :class:`pathlib.Path` object.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-09-06 22:53:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    Exiting the context manager cleans up any temporary file or directory
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    created when the resource was extracted from e.g. a zip file.
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    Use ``as_file`` when the Traversable methods
							 | 
						
					
						
							
								
									
										
										
										
											2023-09-06 22:53:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    (``read_text``, etc) are insufficient and an actual file or directory on
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-30 21:17:05 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    the file system is required.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .. versionadded:: 3.9
							 | 
						
					
						
							
								
									
										
										
										
											2023-09-06 22:53:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .. versionchanged:: 3.12
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-26 01:40:19 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								       Added support for *traversable* representing a directory.
							 |