Skip to content

Make pyflakes/pylint ignore unused imports

February 12th, 2014 - Software(1 min)

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>*,
              Links as [text](http://url). Syntax is
              <a href="" 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.

Share on

A little experiment: