Search code examples
pythonloggingimportpython-importpython-logging

Logging messages from __init__.py of an imported module in Python


I wrote some libraries and part of the code relies on environment variables.

Therefor the init.py files of those libraries have some logic to parse env files or set defaults.

Since this envvars influence the code, I set up a logger in init.py and I am logging whether an env file was found etc.

If I am now writing a skript and want to configure a logger, I am missing those messages if I don't configure the logger before the import. On the other hand imports should come first.

What is a better way to solve this topic?


Solution

  • Couple options:

    1. Break the "imports come first" rule -- it's not unheard of b/c there are sometimes good reasons to do this, but I'd consider it a bit of a hack in your case, i.e. what you should probably do is:

    2. Don't read the env files/vars at the top level, which executes on module import.

    It sounds like you're doing something like:

    # foo.py
    import os
    
    CONFIG = os.getenv('SOMETHING')  # this runs module is imported, i.e. the file is loaded
    
    # main.py
    import foo  # right here
    
    # do stuff, but toplevel code in foo.py has already executed
    

    Instead:

    # foo.py
    import os
    import functools as ft
    
    @ft.cache
    def config():
        return os.getenv('SOMETHING')  # inside a function, not top-level
    
    # main.py
    import foo
    
    # at this point, foo.config() has been defined, but not executed
    
    configure_logger()
    
    foo.config()
    foo.config()  # return value is cached, so same value is returned without re-executing
    
    # do stuff