Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python logger not working when I run package through __main__.py

I have a python package that I use for data preprocessing which I usually run through a Makefile. I want to run it like so: python3 -m utils.features, so that’s why I created __main__.py. However, if I run it like this (instead of python3 -m utils.features.build_features) all the loggers stop producing output, both defined in this file and from other modules imported in build_features.py. Why does this happen?

My project structure:

utils/
├─ features/
│  ├─ __init__.py
│  ├─ __main__.py
│  ├─ build_features.py

__init__.py:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


__main__.py:

from .build_features import build_features

build_features()

build_features.py:

imports...

def build_features():
    logger = logging.getLogger(__name__)
    logger.info("Reading data")
    ...

    logger.info("Building features")
    ...

    logger.info("Saving data")
    ...


if __name__ == "__main__":
    log_fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    logging.basicConfig(level=logging.INFO, format=log_fmt)

    # find .env automagically by walking up directories until it's found, then
    # load up the .env entries as environment variables
    load_dotenv(find_dotenv())

    build_features()

>Solution :

That’s because you setup your loggers in the if __name__ == "__main__" of build_features.py.

Move the logger setup to __main__.py and it’ll work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading