Tuesday, 14 February 2023

Add a data directory outside Python package directory

Given the following directory structure for a package my_package:

/
├── data/
│   ├── more_data/
│   └── foo.txt
├── my_package/
│   ├── __init__.py
│   └── stuff/
│       └── __init__.py
├── README.md
├── setup.cfg
├── setup.py

How can I make the data/ directory accessible (in the most Pythonic way) from within code, without using __file__ or other hacky solutions? I have tried using data_files in setup.py and the [options.package_data] in setup.cfg to no avail.

I would like to do something like:

dir_data = importlib.resources.files(data)
csv_files = dir_data.glob('*.csv')


from Add a data directory outside Python package directory

No comments:

Post a Comment