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

Talk notes

thepkg/ # is a package since has

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.


Use ‘new’ in-built python thing

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

in case error

    data/ # add this

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


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


abc # means abstract base class

Leave a Comment

Your email address will not be published. Required fields are marked *