Why are both "import logging" and "import logging.config" needed?
Asked Answered
B

2

53

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'

Bora answered 10/2, 2010 at 6:56 Comment(0)
R
79

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.

Ricci answered 10/2, 2010 at 7:3 Comment(3)
Can you explain HOW does importing logging.config automatically make logging available as well?Square
I recommend you look at David Beazley's deep dive into Python's import routine, @crusarovid. That's how I learned the ugly details.Fineness
I took a look at David Beazley's deep Dive as Don Kirkby suggested. It's very long, and the answer to this particular issue is not easy to find therein. I'd just treat it as a brute fact that if you import a package's submodule (e.g. logging.config) then you also import the contents of the package's __init__.py file. In this case config from hereClemmie
S
2

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

Silva answered 13/8, 2019 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.