使用Pydev中的python日志记录时出错



我在Aptana安装中使用PyDev和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:UsersTomaszworkspacebasicLogging.py", line 7, in <module>
logging.warning('Watch out!')  # will print a message to the console
AttributeError: module 'logging' has no attribute 'warning'

我搜索了一下,发现了类似的问题:python:安装日志模块,有类似的问题,但没有解决方案。显然问题不在于安装。当我从CMD运行完全相同的脚本时,我会得到正确的输出。目前,Pydev似乎在我的大多数脚本上都给了我错误。如果我回到以前运行良好的代码,现在我有了这个:

Traceback (most recent call last):
File "C:UsersTomaszworkspacepiClientFullQt.py", line 15, in <module>
from matplotlib.backends import qt_compat
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libsite-packagesmatplotlib__init__.py", line 122, in <module>
from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libsite-packagesmatplotlibcbook.py", line 33, in <module>
import numpy as np
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libsite-packagesnumpy__init__.py", line 180, in <module>
from . import add_newdocs
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libsite-packagesnumpyadd_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libsite-packagesnumpylib__init__.py", line 8, in <module>
from .type_check import *
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libsite-packagesnumpylibtype_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libsite-packagesnumpycore__init__.py", line 58, in <module>
from numpy.testing.nosetester import _numpy_tester
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libsite-packagesnumpytesting__init__.py", line 10, in <module>
from unittest import TestCase
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libunittest__init__.py", line 59, in <module>
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
File "C:UsersTomaszAppDataLocalProgramsPythonPython35-32libunittestcase.py", line 273, in <module>
class _CapturingHandler(logging.Handler):
AttributeError: module 'logging' has no attribute 'Handler'

我不知道这是怎么发生的。如果我执行print(sys.executable),它在两种情况下都会给出相同的路径C:UsersTomaszAppDataLocalProgramsPythonPython35-32python3.exe,CMD运行良好,Pydev给出错误。

我对Pydev中的一些python变量有一些问题(我想),但找不到如何修复它。

编辑:我看了这个问题,试着回答

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

C:UsersTomasz>python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
C:UsersTomaszAppDataLocalProgramsPythonPython35-32Libsite-packages

并且站点包已经在系统PYHONPATH 中

我尝试在窗口->首选项->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:UsersTomaszworkspaceSCT2pythongoodExampsloggingbasicLogging.py", line 3, in <module>
logger = logging.getLogger()
AttributeError: module 'logging' has no attribute 'getLogger'

如果我把它作为脚本在命令行中运行,它会很好!!

编辑:解决方案感谢@Samuel,我发现我犯了一个愚蠢的错误!在我开始玩这个库之前,我做了一个文件夹来保存我的脚本,愚蠢地称之为"日志记录"。显然重命名文件夹解决了问题!

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

import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.warning('Watch out!') 
logger.info('I told you so')

最新更新