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

53👍

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.

14👍

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

👤motash

5👍

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
👤Vokey

4👍

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.

3👍

@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

Leave a comment