# Deprecating importlib.resources legacy API

**URL:** <https://discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386>\
**Category:** Core Development\
**Created:** [October 21, 2021, 3:34pm UTC](https://discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386 "2021-10-21T15:34:52Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![asottile](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/asottile/32/2201_2.png) [@asottile](https://discuss.python.org/u/asottile)\
**Post date:** [October 22, 2021, 7:19am UTC](https://discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386/15 "2021-10-22T07:19:50Z")

</div>

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:

```auto
importlib-metadata;python_version<"3.8"
importlib-resources;python_version<"3.7"

```

code:

```python
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:

```auto
importlib-resources;python_version<"3.7"

```

```python
if sys.version_info >= (3, 7):
    import importlib.resources as importlib_resources
else:
    import importlib_resources

s = importlib_resources.read_text('p', 'f')

```

---

_[View the full topic](https://discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386)._
