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’
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.
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
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’
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.
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