[Fixed]-Pylint (pylint_django) does not work when –django-settings-module flag is specified

1πŸ‘

After hours trying many possibilities, this is how i could get this thing work

My folder structure

.
β”œβ”€β”€ backend
β”‚   β”œβ”€β”€ backend <- the main app
β”‚   β”‚   └── settings.py
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ manage.py
β”‚   β”œβ”€β”€ app_1
β”‚   β”œβ”€β”€ app_2
β”‚   β”œβ”€β”€ app_3
β”‚   └── app_4
β”œβ”€β”€ database
β”‚   └── data-base-stuff
β”œβ”€β”€ frontend
β”‚   └── front-end-stuff
└── README.md

vscode/settings.json

{   
    
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "${workspaceFolder}/backend/.venv/bin/pylint",
    "python.linting.pylintArgs": [
        "--init-hook", "import os, sys; os.environ['DJANGO_SETTINGS_MODULE'] = 'backend.backend.settings'; sys.path.append('${workspaceFolder}/backend')",        
        "--load-plugins", "pylint_django",
    ],
    "python.linting.cwd": "${workspaceFolder}/backend/",
}

For some reason, neither using
"--django-settings-module=backend.settings"
nor
"--django-settings-module=backend.backend.settings"
works. If I run the command that VSCode runs on my terminal, they gave me this error

Traceback (most recent call last):
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint_django/checkers/foreign_key_strings.py", line 92, in open
    django.setup()
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/django/conf/__init__.py", line 102, in __getattr__
    self._setup(name)
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/django/conf/__init__.py", line 82, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint_django/checkers/foreign_key_strings.py", line 120, in open
    settings.configure(Settings(self.config.django_settings_module))
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/django/conf/__init__.py", line 217, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/python/3.10.12/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'backend.settings'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspaces/checkout/backend/.venv/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint/__init__.py", line 36, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint/lint/run.py", line 215, in __init__
    linter.check(args)
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 713, in check
    with self._astroid_module_checker() as check_astroid_module:
  File "/usr/local/python/3.10.12/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1015, in _astroid_module_checker
    checker.open()
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint_django/checkers/foreign_key_strings.py", line 125, in open
    self.add_message(
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint/checkers/base_checker.py", line 164, in add_message
    self.linter.add_message(
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1341, in add_message
    self._add_one_message(
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1274, in _add_one_message
    self.stats.increase_single_module_message_count(
  File "/workspaces/checkout/backend/.venv/lib/python3.10/site-packages/pylint/utils/linterstats.py", line 315, in increase_single_module_message_count
    self.by_module[modname][type_name] += increase
KeyError: 'Command line or configuration file'

Reading docs I found that another manner of configuring django is via DJANGO_SETTINGS_MODULE env var. So I tried this way and it worked

DJANGO_SETTINGS_MODULE=backend.backend.settings pylint --init-hook "import sys; sys.path.append('./backend')" --load-plugins pylint_django ./backend/payments/models.py

And to make pylint aways set DJANGO_SETTINGS_MODULE env, I added in --init-hook arg

--init-hook "import os, sys; os.environ['DJANGO_SETTINGS_MODULE'] = 'backend.backend.settings'; sys.path.append('${workspaceFolder}/backend')"
πŸ‘€GaNiziolek

Leave a comment