Why are both "import logging" and "import logging.config" needed?

Question:

Should it not be handled by a single import? i.e. import logging.

If I do not include import logging.config in my script, it gives:

AttributeError: ‘module’ object has no attribute ‘config’

Asked By: Shefali

||

Answers:

logging is a package. Modules in packages aren’t imported until you (or something in your program) imports them. You don’t need both import logging and import logging.config though: just import logging.config will make the name logging available already.

Answered By: Thomas Wouters

Just add an addtional explanation for Thomas’s answer.

logging is a package, a directory.

enter the logging dir and list what files there is:

config.py handlers.py __init__.py __pycache__

so, There is a config.py file in logging directory, but why it can’t import logging.config. That’s because there is no config namespace in logging/__init__.py

Answered By: DennisLi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.