Search code examples
pythondjangopylint

Pylint doesn't report wrong import order when using Django


Pylint doesn't report wrong import order when using Django. I have a file such as:

from feature_one.models import Area
from django.shortcuts import render

The import order is obviously wrong alphabetically and because django is a 3rd party, feature_one is my own code. Isort output:

    $ isort --check app/feature_one/views.py
    ERROR: pylint-wrong-import-order/app/feature_one/views.py Imports are incorrectly sorted and/or formatted.

    $ isort --diff app/feature_one/views.py
    --- pylint-wrong-import-order/app/feature_one/views.py:before   2021-04-19 11:08:35.849230
    +++ pylint-wrong-import-order/app/feature_one/views.py:after    2021-04-19 11:08:50.015937
    @@ -1,4 +1,4 @@
    +from django.shortcuts import render
     from feature_one.models import Area
    -from django.shortcuts import render
     
     # Create your views here.

So why doesn't Pylint complain?

Here's a repo which I created to demonstrate this: https://github.com/Gilwyad/pylint-wrong-import-order


Solution

  • I found that the wrong import order warning is only shown if I specify a directory name on the command line that is a Python package (has a file called init.py).

    This is not the case for the main Django directory, only for its subdirectories. So I have to specify all subdirectories (Django apps) as arguments. Example:

    cd app
    pylint feature_one