here’s the code necessary to be warning-clean with the introduced warnings to load a file’s contents “f” from package “p” with minimum dependencies (not introducing a backport package when the stdlib module exists) and maximum compatibility for the currently supported python versions (python3.6+)
dependencies:
importlib-metadata;python_version<"3.8"
importlib-resources;python_version<"3.7"
code:
if sys.version_info >= (3, 9):
from importlib.resources import files
s = (files('p') / 'f').read_text()
elif sys.version_info >= (3, 7):
from importlib.resources import read_text
s = read_text('p', 'f')
else:
from importlib_metadata import version
if version('importlib-resources').split('.') >= ['1', '3']:
from importlib_resources import files
s = (files('p') / 'f').read_text()
else:
from importlib_resources import read_text
s = read_text('p', 'f')
(and even this has some potential bugs due to unstructured version comparison)
if the warnings are removed and the legacy apis are allowed to live until 3.9 is EOL this is much simpler to do in a warning-clean way:
importlib-resources;python_version<"3.7"
if sys.version_info >= (3, 7):
import importlib.resources as importlib_resources
else:
import importlib_resources
s = importlib_resources.read_text('p', 'f')