Accessing Data Resources for Python Poetry Based Packages
Asked Answered
M

3

8

I recently started experimenting with Poetry for package and dependency management, and I am still getting used to the differences between it and my experience with setuptools. Specifically, I would appreciate help in understanding how to handle the following scenario.

I have a data file that I want to bundle with my package stored in a package subdirectory. Using setup.py I would specify the file and directory names in the setup.py file and then access the file in my code using the pkg_resources API.

What is the equivalent approach using Poetry and pyproject.toml?

Microelectronics answered 6/6, 2020 at 15:34 Comment(0)
S
3

Unlike setuptools poetry bundles automatically all files within your package folder into your package, unless you haven't explicit excluded them in your .gitignore or the pyproject.toml.

So after the package is installed, you can access them with pkg_resources.

Spaniard answered 6/6, 2020 at 16:13 Comment(1)
pkg_resources is deprecated, is it possible to use importlib.resources instead?Wiseacre
E
1

sinoroc's answer is deprecated. The new method since python 3.9 is:

p = importlib.resources.as_file(importlib.resources.files('resources') / 'resource.toml')
with p as f:
    my_toml = tomllib.load(f.open('rb'))    # as an example

Unfortunately, that doesn't really answer the original post. I don't have a solution for poetry yet.

Equality answered 28/6, 2023 at 11:39 Comment(1)
It's the same with Poetry. Once the import package is installed and the package data files inside it, then it does not matter if it was a Poatry-based package or not, importlib.resources will work.Glede

© 2022 - 2024 — McMap. All rights reserved.