0

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?

sohaib
  • 95
  • 2
  • 11
  • I've now tried moving the `config` file up a level to be in the same folder as `aiowrapper.py` but still has the same issue – sohaib Sep 14 '22 at 17:27
  • Once the library is installed, the path to the config file is not what you think it is. There are helper functions that will always deliver the right path. See here: https://stackoverflow.com/a/58941536 – sinoroc Sep 14 '22 at 18:06

1 Answers1

2

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.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
J_H
  • 17,926
  • 4
  • 24
  • 44
  • Awesome, thanks! That worked! Got it with: `from importlib_resources import files logging.config.fileConfig(files('logger').joinpath('config.ini'), disable_existing_loggers=False)` – sohaib Sep 15 '22 at 16:28