32

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.

Guillermo Brachetta
  • 3,857
  • 3
  • 18
  • 36
  • What code or command did you use when you received the message "Django was not configured"? – Jill Cheng Jan 19 '21 at 07:21
  • 1
    I'm using it as my linter in VSCode, so it displays warnings or errors inline in the code. Every single python file has the message saying "Django not configured". Django and Pylint are both configured, so this must be a plugin problem (pylint-django) that started with v.2.4.0 – Guillermo Brachetta Jan 19 '21 at 07:50

5 Answers5

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.

Alexander Taylor
  • 16,574
  • 14
  • 62
  • 83
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
  • This definitely fixes the issue, thank you very much! I find it annoying, though, that pyling-django needs this (never needed it before!) as I now need to add yet another configuration and I rather prefer to work with user settings. I might stick to "disable=django-not-configured" :) Nonetheless, thank you again! – Guillermo Brachetta Jan 21 '21 at 22:02
  • 4
    In my environment it seems that I didn't need `"--disable=django-not-configured"`, but only `"--django-settings-module=.settings",`. – Vincenzo Pii Feb 06 '21 at 14:03
  • 4
    I just tried to add this option but it caused my linter to stop working altogether. – uwain12345 May 25 '21 at 19:05
  • If you're using Pylint via the "Prospector" or "pylama" linters rather than directly via VS Code, then VS Code's `pylintArgs` setting will have no effect; you'll need to configure this in a pylint config file such as `.pylintrc` in the root of your django project (typically alongside `manage.py`) with contents: `[MAIN]` or `[MASTER]` followed by newline and then `django-settings-module = .settings` (replace `` with your main app directory), or with an environment variable somehow as mentioned in the original answer. – Alexander Taylor May 22 '23 at 14:29
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
  • 557
  • 5
  • 8
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.

elmcrest
  • 195
  • 11
  • Thank you for this. Indeed I tried that before but no matter what I did I couldn't get rid of the "Django not configured" error in EACH python file (even empty ones). For the time being I'm either using v.2.3.0 or disabling the error with '--disable=django-not-configured'. But I'm very open to know what you did to fix it! – Guillermo Brachetta Jan 19 '21 at 14:38
  • If the python file you are linting is empty or doesn‘t matter for the pylint_django plugin. It‘s a warning from the plugin that it needs to know the settings module. So I‘d really recommend telling the plugin which settings you‘re using. Maybe I did understand something wrong though, in case, please let me know. Besides please consider marking my answer as a solution. – elmcrest Jan 19 '21 at 15:25
  • Maybe I wasn't clear: I said that EVEN in empty files I get the error, meaning I get the error in all python files. Only from version 2.4.0. Also, I ran all the setup advised and the error stays. The only way to get rid of it (so far) is either going to v.2.3.0 or adding the line in settings.json to disable it. Again, if you found a solution please paste it in full. Thank you! – Guillermo Brachetta Jan 19 '21 at 15:41
  • Yes, it‘s not a bug, it‘s a feature. If you are linting a non-django project don‘t load the plugin. If you are linting a django project pass pyling_django your settings module. See the [release notes](https://github.com/PyCQA/pylint-django/blob/master/CHANGELOG.rst) for reference. – elmcrest Jan 19 '21 at 16:44
  • Well obviously I'm not clear enough. Of course I'm talking about a Django project, and of course I configured it, as I mentioned in my original question. – Guillermo Brachetta Jan 19 '21 at 17:16
3

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
  • 31
  • 2
2

@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
Jeff Martin
  • 317
  • 4
  • 7