Make pyflakes/pylint ignore unused imports
1 min read

Make pyflakes/pylint ignore unused imports

Make pyflakes/pylint ignore unused imports

Sometimes, you need to add an import without explicitly using it, just to have stuff defined. In such cases, both pyflakes and pylint generate messages, which can be annoying.

For example django-appconf has a class-based configuration like so:

# -*- coding: utf-8 -*-
"""
django appconf configuration file

Created on: 09/12/2013
"""
__author__ = 'laurivan'

from django.conf import settings
from appconf import AppConf


class PiclogueConf(AppConf):
    UPLOAD_ROOT = 'upload'
    IMAGE_WIDTH = 600
    IMAGE_HEIGHT = 250
    TOP_COUNT = 7
    HELP_MD = '''Formatting: *<em>Italics</em>*,
              **<strong>Bold</strong>**,
              Links as [text](http://url). Syntax is
              <a href="https://daringfireball.net/projects/markdown/syntax" target="_blank">Markdown</a>.'''

If I'm using the file like this, then I'll end up with both pylint and pyflakes complaining that settings is unused, although it actually is (when the file gets imported and parsed). You can silence pylint by placing a comment on the import line (or in a pylintrc file to have it as a global option):

# ...
from django.conf import settings  # pylint: disable=W0611
# ...

but there's nothing you can do to pyflakes. So, the solution found is somewhat hacky:

# ...
from django.conf import settings
from appconf import AppConf
assert settings
# ...

It's just using assert as a "usage" of the empty import.