"Unused import warning" and pylint

47,184

Solution 1

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.

Solution 2

In such cases, you can still explicitly tell pylint that this unused import in intended:

import loggingsetup # pylint: disable=unused-import

Notice the instruction is on the same line as the import so W0611 is only disabled for this line, and not for all the block below.

Solution 3

If you use pylint and flake8 you can ignore unused import warning in both tools in this way:

import loggingsetup  # noqa # pylint: disable=unused-import

Solution 4

your code should be in a function called once in the main script

Solution 5

As you have mentioned yourself wrapping it in a function and calling the setup explicitly would resolve this warning. And as Steven mentioned, this would be considered better code since it is more explicit about what you are doing.

If you worry about calling this function twice, you can of course use a module intern flag to allow execution of the function body only once.

__initialized = False

def init():
    if not __initialized:
        __initialized = True
        #DoStuff
Share:
47,184
Retsam
Author by

Retsam

Updated on January 07, 2022

Comments

  • Retsam
    Retsam over 2 years

    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.