从Pydev

时间:2016-05-23 18:43:17

标签: python logging pydev aptana

我在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:\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'

我搜索并发现了类似问题的问题:python : install logging module但是没有解决方法。 显然问题不在于安装。当我从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运行良好,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

网站包已经在System PYHONPATH

我尝试在窗口中恢复默认值 - &gt;偏好 - &gt; PyDev - &gt; Iterpreters - &gt; 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,我发现我犯了绝对愚蠢的错误! 在我开始使用库之前,我创建了一个文件夹来保存我的脚本,然后愚蠢地将其称为“日志记录”。显然重命名文件夹解决了问题!

1 个答案:

答案 0 :(得分:1)

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

import logging

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

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