module ‘logging‘ has no attribute ‘handlers‘

报错信息

module 'logging' has no attribute 'handlers'

'module' object has no attribute 'handlers'

解决方法

原来导入logging模块后并没有自动导入其子模块handlers

import logging
import logging.handlers
……

重新运行,程序正常输出。

总结

Python程序中模块在被访问前必须导入,import logging仅导入了logging模块,而logging是一个拥有子模块的包,这些子模块没有被自动载入。所以在访问签需要明确的导入logging.handlers子模块。

但有时候,在导入一些包时并不需要额外的动作就能自动导入其子模块,这是因为在包的__init__.py文件中进行了这些操作。在另外一些情况下,你导入的另外一些东西也可能会导入logging.handlers模块。无论如何,只要保证访问前

对应的子模块已经导入便可。某些时候一个模块看起来是个包实际上并不是,比如os和os.path。os并非包,它只是提供了其他的模块叫path,你可以通过os.path来访问。

猜你喜欢

转载自blog.csdn.net/u012206617/article/details/125781189
今日推荐