0

I have code that reads a configuration file in a __init__.py file inside a certain module. Let's say I have a structure like this:

dir/
    setup.py
    src/
        __init__.py
        properties/
           config.yaml
        module/
           __init__.py ---> this file reads src/properties/config.yaml

The code to read is something like this:

with open(os.path.join(_ROOT, os.path.normpath('src/properties/config.yaml'))) as f:
    config = yaml.load(f)

Where _ROOT is defined in the top src/__init__.py as follows:

ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

Basically, _ROOT=dir.

This works like a charm in every platform, except when src is used as a zip package on which reading returns a FileNotFoundError: ... No such file or directory: ... package.zip\\src\\properties\\config.yaml.

Is there a way to address this problem, do I need to deal the situation when package is zipped, should I avoid loading the file inside the package code...?

I tried an extensive google search for this but I found nothing.

Thank you in advance.

J. Snow
  • 305
  • 2
  • 10
  • Possibly duplicated: https://stackoverflow.com/questions/39104/finding-a-file-in-a-python-module-distribution?rq=1 – J. Snow Sep 12 '18 at 14:17

1 Answers1

0

I was able to solve it with pkgutil.get_data!

import src
a = pkgutil.get_data('src', 'properties/config.yaml')
config = yaml.load(a)

Et voilá!

J. Snow
  • 305
  • 2
  • 10