从 Pydev 使用 python 日志记录时出错

Error using python logging from Pydev

我正在使用 PyDev 和来自 Aptana 安装的 Python 3.5。一切正常,直到我决定探索我以前从未使用过的日志记录模块。我从教程中的新脚本开始:

import logging
logging.warning('Watch out!')  # will print a message to the console
logging.info('I told you so')  # will not print anything

在 Pydev 中我有这个错误:

Traceback (most recent call last):
  File     "C:\Users\Tomasz\workspace\basicLogging.py", line 7, in <module>
    logging.warning('Watch out!')  # will print a message to the console
AttributeError: module 'logging' has no attribute 'warning'

我搜索并发现类似问题: 有类似问题但没有解决方案。 显然问题不在于安装。当我 运行 来自 CMD 的完全相同的脚本时,我有正确的输出。 目前,Pydev 似乎在我的大部分脚本上都给我错误。如果我回到之前工作正常的代码,现在我有这个:

Traceback (most recent call last):
  File "C:\Users\Tomasz\workspace\piClientFullQt.py", line 15, in <module>
    from matplotlib.backends import qt_compat
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\__init__.py", line 122, in <module>
    from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\cbook.py", line 33, in <module>
    import numpy as np
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\site-packages\numpy\__init__.py", line 180, in <module>
    from . import add_newdocs
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\site-packages\numpy\core\__init__.py", line 58, in <module>
    from numpy.testing.nosetester import _numpy_tester
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\site-packages\numpy\testing\__init__.py", line 10, in <module>
    from unittest import TestCase
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\unittest\__init__.py", line 59, in <module>
    from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
  File "C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\lib\unittest\case.py", line 273, in <module>
    class _CapturingHandler(logging.Handler):
AttributeError: module 'logging' has no attribute 'Handler'

我不确定这是怎么发生的。 如果我这样做 print(sys.executable) 它在两种情况下都给出相同的路径 C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\python3.exe,CMD 运行ning 正常而 Pydev 给出错误。

我在 Pydev 中遇到一些 python 变量的问题(我认为)但找不到解决方法。

编辑: 我查看了 this 个问题并尝试了答案

python 解释器的位置是正确的,看起来我拥有我需要的所有库

C:\Users\Tomasz>python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\Lib\site-packages

站点包已经在系统 PYHONPATH 中

我尝试在 Window -> 首选项 -> PyDev -> Iterpreters -> Python 解释器

中恢复默认值

编辑: 按照@Samuel 建议我尝试:

import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

logging.warning('Watch out!')  # will print a message to the console
logging.info('I told you so')  # will not print anything

在 PyDev 中我有:

Traceback (most recent call last):
  File "C:\Users\Tomasz\workspace\SCT2python\goodExamps\logging\basicLogging.py", line 3, in <module>
    logger = logging.getLogger()
AttributeError: module 'logging' has no attribute 'getLogger'

如果我 运行 它作为脚本在命令行中工作,它工作正常!!

编辑:解决方案 感谢@Samuel,我发现我犯了绝对愚蠢的错误! 在开始使用该库之前,我创建了一个文件夹来保存我的脚本,我愚蠢地将它命名为 "logging"。显然重命名文件夹解决了问题!

您需要初始化记录器实例:

import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

logger.warning('Watch out!') 
logger.info('I told you so')