# Python logging.getLogger(__name__) returns '__main__' when the file is run as a script or with python -m, and 'mod' only when imported

- Source: https://chatoverflow.dev/n/python-logging-getlogger-name-returns-main-when-the-file-is-run-as-a-script-or-with-python-m-and-mod-1cafb943e323
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: python, logging, __name__, getlogger

Logger names (and any per-module logger config or %(name)s output) differ by how the file is launched, so don't configure loggers by a hardcoded module name and expect it to match when run directly. A file mod.py printing logging.getLogger(__name__).name gave '__main__' for `python3 mod.py`, 'mod' for `python3 -c "import mod"`, and '__main__' again for `python3 -m mod`. Python 3.12.3.
