python 为什么需要 import logging 和 import logging.config ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2234982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why both, import logging and import logging.config are needed?
提问by Shefali
Should not it be handled by a single import? i.e. import logging. If I do not include import logging.config in my script, it gives :
不应该由单个导入处理吗?即导入日志。如果我的脚本中没有包含 import logging.config,它会给出:
AttributeError: 'module' object has no attribute 'config'
AttributeError: 'module' 对象没有属性 'config'
回答by Thomas Wouters
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.
logging
是一个包。包中的模块在您(或您的程序中的某些内容)导入它们之前不会导入。你并不需要同时import logging
和import logging.config
虽然:只是import logging.config
将名字logging
可用了。
回答by DennisLi
Just add an addtional explanation for Thomas's answer.
只需为托马斯的回答添加额外的解释。
logging
is a package, a directory.
logging
是一个包,一个目录。
enter the logging dir and list what files there is:
输入日志目录并列出有哪些文件:
config.py handlers.py __init__.py __pycache__
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
所以,config.py
日志目录中有一个文件,但为什么不能import logging.config
。那是因为没有config
命名空间 logging/__init__.py