6

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?

rhurwitz
  • 2,557
  • 2
  • 10
  • 18

3 Answers3

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.

finswimmer
  • 10,896
  • 3
  • 34
  • 44
1

You could still use setuptools's pkg_resources.

You could also use one of those from Python's own standard library:

sinoroc
  • 18,409
  • 2
  • 39
  • 70
0

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.

Isotopp
  • 3,313
  • 1
  • 16
  • 17
Sam
  • 91
  • 5