rhurwitz
rhurwitz

Reputation: 2747

Accessing Data Resources for Python Poetry Based Packages

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?

Upvotes: 9

Views: 7504

Answers (3)

Sam
Sam

Reputation: 339

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.

Upvotes: 1

finswimmer
finswimmer

Reputation: 15202

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.

Upvotes: 3

Related Questions