@@ -14,12 +14,13 @@ This module leverages Python's import system to provide access to *resources*
14
14
within *packages *.
15
15
16
16
"Resources" are file-like resources associated with a module or package in
17
- Python. The resources may be contained directly in a package or within a
18
- subdirectory contained in that package. Resources may be text or binary. As a
19
- result, Python module sources (.py) of a package and compilation artifacts
20
- (pycache) are technically de-facto resources of that package. In practice,
21
- however, resources are primarily those non-Python artifacts exposed
22
- specifically by the package author.
17
+ Python. The resources may be contained directly in a package, within a
18
+ subdirectory contained in that package, or adjacent to modules outside a
19
+ package. Resources may be text or binary. As a result, Python module sources
20
+ (.py) of a package and compilation artifacts (pycache) are technically
21
+ de-facto resources of that package. In practice, however, resources are
22
+ primarily those non-Python artifacts exposed specifically by the package
23
+ author.
23
24
24
25
Resources can be opened or read in either binary or text mode.
25
26
@@ -49,27 +50,34 @@ for example, a package and its resources can be imported from a zip file using
49
50
``get_resource_reader(fullname) `` method as specified by
50
51
:class: `importlib.resources.abc.ResourceReader `.
51
52
52
- .. data :: Package
53
+ .. data :: Anchor
53
54
54
- Whenever a function accepts a ``Package `` argument, you can pass in
55
- either a :class: `module object <types.ModuleType> ` or a module name
56
- as a string. You can only pass module objects whose
57
- ``__spec__.submodule_search_locations `` is not ``None ``.
55
+ Represents an anchor for resources, either a :class: `module object
56
+ <types.ModuleType> ` or a module name as a string. Defined as
57
+ ``Union[str, ModuleType] ``.
58
58
59
- The ``Package `` type is defined as ``Union[str, ModuleType] ``.
60
-
61
- .. function :: files(package)
59
+ .. function :: files(anchor: Optional[Anchor] = None)
62
60
63
61
Returns a :class: `~importlib.resources.abc.Traversable ` object
64
- representing the resource container for the package (think directory)
65
- and its resources (think files). A Traversable may contain other
66
- containers (think subdirectories).
62
+ representing the resource container (think directory) and its resources
63
+ (think files). A Traversable may contain other containers (think
64
+ subdirectories).
67
65
68
- *package * is either a name or a module object which conforms to the
69
- :data: `Package ` requirements.
66
+ *anchor * is an optional :data: `Anchor `. If the anchor is a
67
+ package, resources are resolved from that package. If a module,
68
+ resources are resolved adjacent to that module (in the same package
69
+ or the package root). If the anchor is omitted, the caller's module
70
+ is used.
70
71
71
72
.. versionadded :: 3.9
72
73
74
+ .. versionchanged :: 3.12
75
+ "package" parameter was renamed to "anchor". "anchor" can now
76
+ be a non-package module and if omitted will default to the caller's
77
+ module. "package" is still accepted for compatibility but will raise
78
+ a DeprecationWarning. Consider using ``importlib_resources >= 5.10 ``
79
+ for a compatible interface on older Pythons.
80
+
73
81
.. function :: as_file(traversable)
74
82
75
83
Given a :class: `~importlib.resources.abc.Traversable ` object representing
@@ -86,6 +94,7 @@ for example, a package and its resources can be imported from a zip file using
86
94
87
95
.. versionadded :: 3.9
88
96
97
+
89
98
Deprecated functions
90
99
--------------------
91
100
@@ -94,6 +103,18 @@ scheduled for removal in a future version of Python.
94
103
The main drawback of these functions is that they do not support
95
104
directories: they assume all resources are located directly within a *package *.
96
105
106
+ .. data :: Package
107
+
108
+ Whenever a function accepts a ``Package `` argument, you can pass in
109
+ either a :class: `module object <types.ModuleType> ` or a module name
110
+ as a string. You can only pass module objects whose
111
+ ``__spec__.submodule_search_locations `` is not ``None ``.
112
+
113
+ The ``Package `` type is defined as ``Union[str, ModuleType] ``.
114
+
115
+ .. deprecated :: 3.12
116
+
117
+
97
118
.. data :: Resource
98
119
99
120
For *resource * arguments of the functions below, you can pass in
@@ -102,6 +123,7 @@ directories: they assume all resources are located directly within a *package*.
102
123
103
124
The ``Resource `` type is defined as ``Union[str, os.PathLike] ``.
104
125
126
+
105
127
.. function :: open_binary(package, resource)
106
128
107
129
Open for binary reading the *resource * within *package *.
0 commit comments