AttributeError 'module' object has no attribute 'handlers'--Python子模块导入问题
来源:互联网 发布:白狐软件 编辑:程序博客网 时间:2024/05/19 04:52
想使用python的logging模块记录日志,
并使用RotatingFileHandler来处理日志以便于在日志文件超过指定的大小后会重新生成新的日志文件。
基本代码如下:
import logging
logger = logging.getLogger('mylogger')
logger.setLevel(logging.INFO)
fh=logging.handlers.RotatingFileHandler('/tmp/test.log', mode = 'a', maxBytes=10240, backupCount=3, encoding='utf-8')
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('hello logging')
logger.warning('hello logging')
logger.error('hello logging')
logger.critical('hello logging')
运行时报错:
AttributeError: '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来访问。
并使用RotatingFileHandler来处理日志以便于在日志文件超过指定的大小后会重新生成新的日志文件。
基本代码如下:
import logging
logger = logging.getLogger('mylogger')
logger.setLevel(logging.INFO)
fh=logging.handlers.RotatingFileHandler('/tmp/test.log', mode = 'a', maxBytes=10240, backupCount=3, encoding='utf-8')
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('hello logging')
logger.warning('hello logging')
logger.error('hello logging')
logger.critical('hello logging')
运行时报错:
AttributeError: '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来访问。
0 0
- AttributeError: 'module' object has no attribute 'handlers'--Python子模块导入问题
- AttributeError 'module' object has no attribute 'handlers'--Python子模块导入问题
- AttributeError: ‘module’ object has no attribute’ 问题
- [Python]attributeError:'module' object has no attribute 'dump'
- [python错误]builtins.AttributeError: 'module' object has no attribute 'request'
- python运行不了 AttributeError: module object has no attribute getdefaultlocale
- python AttributeError: 'module' object has no attribute 'setdefaultencoding'
- python运行不了 AttributeError: module object has no attribute getdefaultlocale
- python AttributeError: 'module' object has no attribute 'dumps'解决办法
- Python AttributeError: 'module' object has no attribute xxxx
- Python 【精】AttributeError: 'Module' object has no attribute 'STARTF_USESHOWINDOW'
- Python AttributeError: ‘module’ object has no attribute ‘A’
- AttributeError: 'module' object has no attribute 'face' 问题解决 opencv+python
- python 错误解析 AttributeError: 'module' object has no attribute 'dumps'
- 【python】AttributeError: 'module' object has no attribute 'HTTPSConnection'
- Python 2.7.3 AttributeError: 'module' object has no attribute 'urlopen'
- Python:AttributeError: ‘module’ object has no attribute ‘ArgumentParser’
- Python 错误:AttributeError: 'module' object has no attribute
- 和利时《项目经理、项目管理及核心能力建设》讲座圆满结束!
- UIWebView加载本地HTML5文件
- ArrayList与一般数组的比较
- 内存操作函数:_alloca
- 【转】Android 纯代码化编码1_关于布局
- AttributeError 'module' object has no attribute 'handlers'--Python子模块导入问题
- viewholder的使用
- 第十六章_动态注册和Servlet容器初始化
- 还是不行啊?
- android版本兼容的一点处理办法
- 计数+分治求海量数据中重复最多的一个
- fread 不能读取最后一个数据块
- allocating an object of abstract class type
- 文件下载 ftp