5
5
===========================
6
6
7
7
``importlib_resources `` is a library that leverages Python's import system to
8
- provide access to *resources * within *packages *. Given that this library is
9
- built on top of the import system, it is highly efficient and easy to use.
10
- This library's philosophy is that, if you can import a package, you can access
11
- resources within that package. Resources can be opened or read, in either
12
- binary or text mode.
8
+ provide access to *resources * within *packages * and alongside * modules *. Given
9
+ that this library is built on top of the import system, it is highly efficient
10
+ and easy to use. This library's philosophy is that, if one can import a
11
+ module, one can access resources associated with that module. Resources can be
12
+ opened or read, in either binary or text mode.
13
13
14
14
What exactly do we mean by "a resource"? It's easiest to think about the
15
15
metaphor of files and directories on the file system, though it's important to
@@ -23,11 +23,14 @@ If you have a file system layout such as::
23
23
one/
24
24
__init__.py
25
25
resource1.txt
26
+ module1.py
26
27
resources1/
27
28
resource1.1.txt
28
29
two/
29
30
__init__.py
30
31
resource2.txt
32
+ standalone.py
33
+ resource3.txt
31
34
32
35
then the directories are ``data ``, ``data/one ``, and ``data/two ``. Each of
33
36
these are also Python packages by virtue of the fact that they all contain
@@ -48,11 +51,14 @@ package directory, so
48
51
``data/one/resource1.txt `` and ``data/two/resource2.txt `` are both resources,
49
52
as are the ``__init__.py `` files in all the directories.
50
53
51
- Resources are always accessed relative to the package that they live in.
52
- ``resource1.txt `` and ``resources1/resource1.1.txt `` are resources within
53
- the ``data.one `` package, and
54
- ``two/resource2.txt `` is a resource within the
55
- ``data `` package.
54
+ Resources in packages are always accessed relative to the package that they
55
+ live in. ``resource1.txt `` and ``resources1/resource1.1.txt `` are resources
56
+ within the ``data.one `` package, and ``two/resource2.txt `` is a resource
57
+ within the ``data `` package.
58
+
59
+ Resources may also be referenced relative to another *anchor *, a module in a
60
+ package (``data.one.module1 ``) or a standalone module (``standalone ``). In
61
+ this case, resources are loaded from the same loader that loaded that module.
56
62
57
63
58
64
Example
@@ -103,13 +109,13 @@ using ``importlib_resources`` would look like::
103
109
eml = files('email.tests.data').joinpath('message.eml').read_text()
104
110
105
111
106
- Packages or package names
107
- =========================
112
+ Anchors
113
+ =======
108
114
109
- All of the ``importlib_resources `` APIs take a * package * as their first
110
- parameter, but this can either be a package name (as a ``str ``) or an actual
111
- module object, though the module * must * be a package . If a string is
112
- passed in, it must name an importable Python package , and this is first
115
+ All of the ``importlib_resources `` APIs take an * anchor * as their first
116
+ parameter, which can either be a package name (as a ``str ``) or an actual
117
+ module object. If a string is
118
+ passed in, it must name an importable Python module , and this is first
113
119
imported. Thus the above example could also be written as::
114
120
115
121
import email.tests.data
0 commit comments