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

Question:

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.

Answers:

this hit me too and I think I have a solution. if you run in terminal, as suggested by the error message, with the parameter --help-msg=django-not-configured you’ll end up with the following message:

Finding foreign-key relationships from strings in pylint-django requires
configuring Django. This can be done via the DJANGO_SETTINGS_MODULE
environment variable or the pylint option django-settings-module, eg: `pylint
--load-plugins=pylint_django --django-settings-module=myproject.settings` .
This can also be set as an option in a .pylintrc configuration file. Some
basic default settings were used, however this will lead to less accurate
linting. Consider passing in an explicit Django configuration file to match
your project to improve accuracy. This message belongs to the django foreign
keys referenced by strings checker.

so to solve this you can either pass the needed settings module via command line argument, in a .pylintrc file or set (export) your environment variable.

hope it helped.

Answered By: elmcrest

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

  {
    "python.linting.pylintArgs": [
        [...]
        "--disable=django-not-configured",
        "--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.

Answered By: Neil C. Obremski

If you want to use a .pylintrc file add the following to to your .pylintrc file

Note: replace myproject with the name of your project (the root directory of your Django project.

[MASTER]
load-plugins=pylint_django
django-settings-module=myproject.settings

if you don’t have one you can create one here’s an example

[MASTER]
load-plugins=pylint_django
django-settings-module=myproject.settings

[FORMAT]
max-line-length=120

[MESSAGES CONTROL]
disable=missing-docstring,invalid-name

[DESIGN]
max-parents=13

[TYPECHECK]
generated-members=REQUEST,acl_users,aq_parent,"[a-zA-Z]+_set{1,2}",save,delete

If you’re using a seperate settings file for local vs productin then
use the relevant setting file ex:

django-settings-module=myproject.settings.local

Answered By: motash

@motash’s answer works perfectly. One small addition: if you are creating a .pylintrc file using Powershell, you need to pass in UTF-8 encoding:
pylint --generate-rcfile | Out-File -Encoding utf8 .pylintrc

From there, as @motash says, add this to your .pylintrc file:

load-plugins=pylint_django
django-settings-module=myproject.settings
Answered By: Jeff Martin

If you also encounter an issue with KeyError: 'Command line or configuration file' after you already added a .pylintrc file. You may add an init-hook line to the file.

eg.

[MAIN]
init-hook="import sys; import os; from pylint.config import find_pylintrc; sys.path.append(os.path.dirname(find_pylintrc()))"
load-plugins=pylint_django
django-settings-module=<project>.settings
Answered By: Vokey