Search code examples
pythonpython-poetry

Publishing a python package where package loads a logging config file fails when imported into another package


I am trying to publish a package (wrapper) and then import it into another package (client) to use but having trouble with importing as the wrapper package makes use of sibling/children packages as well as a config.ini file for logging purposes.

This is what wrapper looks like:

.
├── wrapper
│   ├── __init__.py
│   ├── aiowrapper.py
│   ├── logger
│   │   └── config.ini
│   ├── models
│   │   ├── __init__.py
│   │   └── models.py
├── poetry.lock
├── pyproject.toml

In the wrapper, the following lines are what are causing me issues:

from .models.models import Position

logging.config.fileConfig('logger/config.ini', disable_existing_loggers=False)
log = logging.getLogger(__name__)

I am testing using this from a jupyter notebook in the client package. Here's the steps I follow:

  1. In wrapper - poetry build --no-cache
  2. In client - poetry add -D <path-to-tar-build-file
  3. In client jupyter notebook - from wrapper.aiowrapper import Session

This gives the following error:

---> 10 logging.config.fileConfig('logger/config.ini', disable_existing_loggers=False)
     11 log = logging.getLogger(__name__)
     13 class Session:

File ~/.pyenv/versions/3.10.4/lib/python3.10/logging/config.py:72, in fileConfig(fname, defaults, disable_existing_loggers, encoding)
     69         encoding = io.text_encoding(encoding)
     70         cp.read(fname, encoding=encoding)
---> 72 formatters = _create_formatters(cp)
     74 # critical section
     75 logging._acquireLock()

File ~/.pyenv/versions/3.10.4/lib/python3.10/logging/config.py:105, in _create_formatters(cp)
    103 def _create_formatters(cp):
...
    963     if key != self.default_section and not self.has_section(key):
--> 964         raise KeyError(key)
    965     return self._proxies[key]

KeyError: 'formatters'

Which suggests that the config file is not being found.

I've found this but is there a way to do this without adding it to the path like that?


Solution

  • tl;dr:

    from importlib_resources import files
    eml = files('email.tests.data').joinpath('message.eml').read_text()
    

    and then adjust that for your config.ini.