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:
__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.