Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using python logging from Pydev

I am using PyDev with Python 3.5 from Aptana installation. All worked fine until I decided to explore logging module, which I never used before. I started with new script from the tutorial:

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

in Pydev I have this error:

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'

I searched and found questions like: python : install logging module with similar problem but no solution. Obviously the problem is not with installation. When I run exactly the same script from CMD I have correct output. At the moment it seems like Pydev gives me error on most of my scripts. If I come back to the code wich previously worked fine, now I have this:

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'

I am not sure how this happened. If I do print(sys.executable) it gives the same path C:\Users\Tomasz\AppData\Local\Programs\Python\Python35-32\python3.exe in both cases, CMD running fine and Pydev giving error.

I have some problem with some python variables in Pydev (I think) but can't find how to fix it.

EDIT: I look at this question and tried the answers

Location of python interpreter is correct and it looks like I have all libs what I need

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

And site-packages are already in System PYHONPATH

I tried Restore Defaults in Window -> Preferences -> PyDev -> Iterpreters -> Python Interpreter

EDIT: Following @Samuel advise I try:

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

and in PyDev I have:

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'

It works fine if I run it in command line as a script!!

EDIT: THE SOLUTION Thanks to @Samuel I figure out I made absolutely stupid mistake! Before I started playing with the library I made a folder to keep my scripts and stupidly I called it "logging". Obviously renaming the folder solved the problem!

like image 478
tomasz74 Avatar asked Nov 08 '22 14:11

tomasz74


1 Answers

You need to init your logger instance:

import logging

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

logger.warning('Watch out!') 
logger.info('I told you so')
like image 109
Samuel Avatar answered Nov 14 '22 21:11

Samuel