@@ -108,6 +108,70 @@ An alternative build directory can be specified using the
108108:option: `build-dir ` config setting.
109109
110110
111+ Data files
112+ ----------
113+
114+ It is relatively common to install data files needed at runtime
115+ alongside the package's Python code or extension modules. For a Python
116+ package named ``package `` this would look like this:
117+
118+ .. code-block :: meson
119+
120+ py = import('python').find_installation()
121+
122+ py.install_sources(
123+ '__init__.py',
124+ subdir: 'package',
125+ )
126+
127+ install_data(
128+ 'data.txt',
129+ install_dir: py.get_install_dir() / 'package',
130+ )
131+
132+ custom_target(
133+ output: 'uuid.txt',
134+ command: [py, '-m', 'uuid''],
135+ capture: true,
136+ install: true,
137+ install_dir: py.get_install_dir() / 'package',
138+ )
139+
140+ In most circumstances, these files can be accessed deriving their
141+ filesystem path from the filesystem path of the Python module next to
142+ them via the ``__file__ `` special variable. For example, within the
143+ package ``__init__.py ``:
144+
145+ .. code-block :: python
146+
147+ import pathlib
148+
149+ data = pathlib.Path(__file__ ).parent.joinpath(' data.txt' ).read_text()
150+ uuid = pathlib.Path(__file__ ).parent.joinpath(' uuid.txt' ).read_text() # WRONG!
151+
152+ However, this does not work when modules are not loaded from a package
153+ installed in the Python library path in the filesystem but with a
154+ special module loader, as used to implement editable installs in
155+ ``meson-python ``. In the example above, the second read would fail
156+ when the package is installed in editable mode. For this reason, data
157+ files need to be accessed using :mod: `importlib.resources `. The code
158+ above should be replaced with:
159+
160+ .. code-block :: python
161+
162+ import importlib.resources
163+
164+ data = importlib.resources.files().joinpath(' data.txt' ).read_text()
165+ uuid = importlib.resources.files().joinpath(' uuid.txt' ).read_text()
166+
167+ :mod: `importlib.resources ` implements a virtual filesystem that allows
168+ to access individual files as if they were in their install location.
169+ However, there is not way to expose this file structure outside of the
170+ python runtime. In the example above, it is not possible to make the
171+ ``data.txt `` and ``uuid.txt `` files appear in the same fileystem
172+ directory.
173+
174+
111175.. _how-to-guides-editable-installs-verbose :
112176
113177Verbose mode
0 commit comments