[Python] Circular Import During `dictConfig` in Python Logging

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    I have a custom logger with the below structure, it uses dictConfig to read the default.json file for the logger. It works fine, but when I wanted to nest under a project root, I got an error while importing it. Any reason why that might happen? I double checked the path and it see to be correct.

    Initial Structure

    tealogger/
    - configuration/
    - default.json
    - __init__.py
    - formatter.py
    - ...


    Nested Structure

    projectroot/
    - tealogger/
    - configuration/
    - default.json
    - __init__.py
    - formatter.py
    - ...


    Error

    Traceback (most recent call last):
    File "/opt/python/substance3d_scene_automation_110_m_1089/lib/python3.11/logging/config.py", line 400, in resolve
    found = getattr(found, frag)
    ^^^^^^^^^^^^^^^^^^^^
    AttributeError: cannot access submodule 'tealogg' of module 'projectroot' (most likely due to a circular import)

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/opt/python/substance3d_scene_automation_110_m_1089/lib/python3.11/logging/config.py", line 552, in configure
    formatters[name] = self.configure_formatter(
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/python/substance3d_scene_automation_110_m_1089/lib/python3.11/logging/config.py", line 664, in configure_formatter
    result = self.configure_custom(config)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/python/substance3d_scene_automation_110_m_1089/lib/python3.11/logging/config.py", line 479, in configure_custom
    c = self.resolve(c)
    ^^^^^^^^^^^^^^^
    File "/opt/python/substance3d_scene_automation_110_m_1089/lib/python3.11/logging/config.py", line 403, in resolve
    found = getattr(found, frag)
    ^^^^^^^^^^^^^^^^^^^^
    AttributeError: cannot access submodule 'tealogg' of module 'projectroot' (most likely due to a circular import)

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/Users/yk/Desktop/Project/Repository/Adobe/3DIQE/projectroot/projectroot/tealogger/__init__.py", line 128, in <module>
    tealogger = TeaLogger(
    ^^^^^^^
    File "/Users/yk/Desktop/Project/Repository/Adobe/3DIQE/projectroot/projectroot/tealogger/__init__.py", line 100, in __new__
    logging.config.dictConfig(DEFAULT_CONFIGURATION)
    File "/opt/python/substance3d_scene_automation_110_m_1089/lib/python3.11/logging/config.py", line 823, in dictConfig
    dictConfigClass(config).configure()
    File "/opt/python/substance3d_scene_automation_110_m_1089/lib/python3.11/logging/config.py", line 555, in configure
    raise ValueError('Unable to configure '
    ValueError: Unable to configure formatter 'color'

    Continue reading...

Compartilhe esta Página