Search code examples
pythondjangovisual-studio-codepylint

Pylint-django raising error about Django not being configured when that's not the case (VSCode)


Pylint-django worked just fine up to version 2.3.x but since 2.4.0 it reports an error on every python-django file:

Django was not configured. For more information runpylint --load-plugins=pylint_django --help-msg=django-not-configuredpylint(django-not-configured)

This happens on VSCode and I believe I have it correctly configured:

    {
      "python.linting.pylintArgs": [
          "--load-plugins",
          "pylint_django",
          "--load-plugins",
          "pylint_django.checkers.migrations",
          "--disable=C0114, C0115, W0222",
          "--disable=imported-auth-user",
          "--disable=invalid-name",
          "--disable=line-too-long"
      ]
    }

This worked perfectly fine, as I said, up to v.2.3.

I raised an issue on their repository but regrettably it was dismissed with little to no visible effort to address it.

For the time being I'm staying with v.2.3.0, which lints with no issues with the above configuration, but would like to know if this is a bug or otherwise.

Did any get into this issue or is it there anything else I'm missing?

Note:

The error message can be hid by adding this value in VSCode's settings.json:

  {
    "python.linting.pylintArgs": [
        [...]
        "--disable=django-not-configured",
    ]
  }

But I'm aware this is sweeping the dust under the carpet.


Solution

  • Add the --django-settings-module argument the VS Code settings:

      {
        "python.linting.pylintArgs": [
            [...]
            "--django-settings-module=<mainapp>.settings",
        ]
      }
    

    Change <mainapp> to be your main app directory. For example, if your settings.py was in sweetstuff/settings.py then the argument value would be sweetstuff.settings. This is the same format as you would import the settings module from inside a Python module or the Django shell.

    This problem came up for me in some Bitbucket Pipelines. I solved it there by creating the DJANGO_SETTINGS_MODULE as a repository variable which is then made available as an environment variable when Pylint is run.