【mysql】logging模块命名冲突

来源:互联网 发布:男生长发 知乎 编辑:程序博客网 时间:2024/06/05 19:49

在写接口框架时,需要使用到日志,调用logging模块

首先创建logging.py文件

编写代码

import logginglogging.debug('有bug')

运行代码后,提示错误

Traceback (most recent call last):  File "E:/gitlab/SWQA_API/log/logging.py", line 2, in <module>    import logging  File "E:\gitlab\SWQA_API\log\logging.py", line 4, in <module>    logging.debug('有bug')AttributeError: module 'logging' has no attribute 'debug'

为什么会提示 module 'logging' has no attribute 'debug' 这样的错误呢?

难道是没有引入logging 模块么?使用 pip install logging 安装logging模块,问题仍存在

是否因为python兼容性的问题,不存在debug 这个方法?

查看logging的__init__.py 文件,debug 方法的定义

def debug(self, msg, *args, **kwargs):     """     Log 'msg % args' with severity 'DEBUG'.     To pass exception information, use the keyword argument exc_info with     a true value, e.g.     logger.debug("Houston, we have a %s", "thorny problem", exc_info=1)     """     if self.isEnabledFor(DEBUG):         self._log(DEBUG, msg, args, **kwargs)

可以看到,有logging 这个模块,而且有debug 这个方法

那换一个方法尝试呢

import logginglogger = logging.getLogger("example")

仍然报错:

Traceback (most recent call last):  File "E:/gitlab/SWQA_API/log/logging.py", line 2, in <module>    import logging  File "E:\gitlab\SWQA_API\log\logging.py", line 4, in <module>    logger = logging.getLogger("example")AttributeError: module 'logging' has no attribute 'getLogger'

原来问题出在一开始的文件名命名,与文件名冲突有关

Something strange is going on here with your Python configuration. Have you added the directory containing logging.py to your Python path? This is not a pandas bug – pandas is simply using import logging, which it expects to be the standard library logging module

https://github.com/pandas-dev/pandas/issues/10167

有一个名为logging.py的文件,则需要重命名,故将logging.py 文件名修改为 log.py,重新执行,可正常运行

原创粉丝点击