Search code examples
pydevsuppress-warnings

Suppress warnings in PyDev


I use the following at the beginning of all modules in my Python project:

import setup_loggers

setup_loggers is a module that does exactly that. The import statement makes sure that no matter which module is loaded first, the loggers are setup and ready.

However, as I don't use setup_loggers module later in the file, I receive a PyDev warning (a small yellow marker). I get this warning for all my modules, thus it blocks me from seeing other warnings in the PyDev Package Explorer.

Is there a way to suppress the warning for a specific line (the import line above) in PyDev?
Any other ideas on how to overcome this annoyance?


Solution

  • In PyDev, whenever there's an error in a line, you can press Ctrl+1 and it'll show an option to ignore that warning in that line (in this case, it'll add a comment: #@UnusedImport -- which you could add manually -- in that line and that error/warning will be ignored).

    Now, on to a better strategy for you (so that you don't have to import that module everywhere): in Python, when you do the import of a package, the parents will be imported before.

    I.e.:

    /my_project
    /my_project/__init__.py
    /my_project/submodule.py
    /my_project/package
    /my_project/package/__init__.py
    

    When you import the my_project.submodule or my_project.package, it'll first have to import (and execute) the code in /my_project/__init__.py

    So, a better strategy for you would be only adding that import to the /my_project/__init__.py (and whenever any submodule is imported, the loggers would be already setup).

    It just wouldn't work if you have a collection of files that are scattered in the PYTHONPATH root and on the file you execute as your __main__ (as it won't import that file, it'll just get its contents and execute it -- but whenever that file imports anything from /my_project, things would be setup).