Executable access to yaml configuration file using setuptools

Viewed 1006

Executable produced using entry_points cannot find path to Yaml configuration file.

I am using setuptools to distribute a small application (mainly local to my office). I have the Yaml config file in the MANIFEST.in file, so that when by applications is built everything get installed to site-packages. This works, however the executable is installed in the virtual env bin folder and is no longer aware of the Yaml config file.

What is the best approach to specifying location of the Yaml file?

1 Answers

Put the yaml config in a python package alongside the code:

root
├── spam
│   ├── __init__.py
│   ├── eggs.py
│   └── config.yml
└── setup.py

and use importlib.resources (or importlib_resources backport for Python < 3.7) to access the file in code, e.g.

try:
    from importlib import resources as res
except ImportError:
    import importlib_resources as res

with res.open_binary('spam', 'config.yml') as fp:
    config = yaml.load(fp, Loader=yaml.Loader)
    ...

Mark the non-Python file to be included in source dist/wheel via package_data:

from setuptools import setup


setup(
    ...
    packages=['spam'],
    package_data={'spam': ['config.yml']}
)
Related