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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:07:59  来源:igfitidea点击:

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

pythonloggingimport

提问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

loggingis a package. Modules in packages aren't imported until you (or something in your program) imports them. You don't need both import loggingand import logging.configthough: just import logging.configwill make the name loggingavailable already.

logging是一个包。包中的模块在您(或您的程序中的某些内容)导入它们之前不会导入。你并不需要同时import loggingimport logging.config虽然:只是import logging.config将名字logging可用了。

回答by DennisLi

Just add an addtional explanation for Thomas's answer.

只需为托马斯的回答添加额外的解释。

loggingis 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.pyfile in logging directory, but why it can't import logging.config. That's because there is no confignamespace in logging/__init__.py

所以,config.py日志目录中有一个文件,但为什么不能import logging.config。那是因为没有config命名空间 logging/__init__.py