Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 833
rhurwitz Avatar asked May 30 '26 00:05

rhurwitz


2 Answers

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.

like image 121
finswimmer Avatar answered May 31 '26 18:05

finswimmer


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.

like image 28
Sam Avatar answered May 31 '26 16:05

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!