Search code examples
pythonwarningspylint

"Unused import warning" and pylint


So I'm working on a project in Python and trying to keep it up to standards with pylint and just generally . So, I have a source file, (We'll just call it a.py)

#a.py
import loggingsetup

def foo():
   log.info("This is a log message")

But, I want to control what the logging looks like, so in loggingsetup I have something like:

#loggingsetup.py
import logging

logging.root.setLevel(logging.DEBUG)

consoleOut = logging.StreamHandler()
consoleOut.setLevel(logging.INFO)  
consoleOut.setFormatter(logging.Formatter("\t"+logging.BASIC_FORMAT))
logging.root.addHandler(consoleOut)

#etc

Now, this seems to work alright. I suppose as a preliminary question I should ask if this is the right way to go about this, or if there's a different way of structuring my code that would be preferable.

But my main question is that when I run pylint on a.py I get a warning like "unused import - import loggingsetup", since I'm not actually calling any methods or functions from loggingsetup.

I could do something like redefine the body of loggingsetup as a function and call it, but it seems silly and error-prone (I'd have to worry about calling it twice if I did import loggingsetup from somewhere else, and if I understand how python handles imports, that's not an issue with my current setup).

I could obviously just tell pylint to ignore the warning, but I thought I'd ask here first to make sure that this isn't actually something that I should handle differently.


Solution

  • The approach I would use is to use loggingsetup as a sort of wrapper for logging.

    import logging
    
    # set up logging config here
    
    from logging import *
    

    Then in your other modules you:

    import loggingsetup as logging
    

    You might want to use a name other than loggingsetup in this case, e.g. tweaked_logging or logging_with_my_settings.