简体   繁体   中英

Executable access to yaml configuration file using setuptools

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?

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

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

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

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'],
    
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM