Notes of Get your resources faster, with importlib.resources by Barry Warsaw


software engineering

Talk notes

thepkg/ # is a package since has __init__.py
    __init__.py
    a.py
    b.py
    data/
        sample.dat

You need to get data files (templates, certifs etc) from your package

You do

import thepkg
from pathlib import Path

pkg = Path.(thepkg.__file__).parent
path = pkg / 'data' / 'sample.dat'

Problem as __file__ not necessarily a real file on system

Package: pkg_resources: import-time side effects

goes over syspath, if you have many, takes time.

Solution:

Use ‘new’ in-built python thing

from importlib.resources import read_binary
contents = read_binary('thepkg,data', 'sample.data')
# or read_binary(thepkg,data, 'sample.data')

in case error

thepkg/ 
    __init__.py
    a.py
    b.py
    data/
        __init__.py # add this
        sample.dat

Pakage? Any importable module with a __path__ attribute.

Resource? Any readable object inside package, like files

subdirs and subpackages not resources

namespace packages cannot contain resources

importlib.resources

read_text -> str

open_binary -> BinaryIO (use with open_b…)

open_text -> TextIO

path(package, resource) -> Iterator[path]

with path … as lib

contents(package) -> list[str]

is_resource(package, name: str) # use with contents

loader.get_resource_reader

abc # means abstract base class

Written by

Abdur-Rahmaan Janhangeer

Chef

Python author of 7+ years having worked for Python companies around the world

Suggested Posts

Python: Making Imports Callable

I wanted to do import module module() I thought this did not exist in Python but Chris Angelico, a...

Read article

Python Generators: The In-depth Article You've Always Wanted

Table of contents Why were Python generators introduced? How do Python Generators differ from norma...

Read article

The Zen Of Python: A Most In Depth Article

Note: I wrote a quite complete article on the Zen but for some reason it went down in seo history. I...

Read article
Free Flask Course