Search code examples
pythondjangodjango-rest-frameworkpylintdjango-apps

PyLint Django: django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured


My Django app works fine when I am just launching my application through python src/manage.py runserver But I would like to integrate pylint here, and when i launch the command pylint src --load-plugins pylint_django I got the following traceback:

Traceback (most recent call last):
  File "/venv/lib/python3.11/site-packages/pylint_django/checkers/foreign_key_strings.py", line 87, in open
    django.setup()
  File "/venv/lib/python3.11/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/django/conf/__init__.py", line 102, in __getattr__
    self._setup(name)
  File "/venv/lib/python3.11/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_MO
DULE or call settings.configure() before accessing settings.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/venv/lib/python3.11/site-packages/pylint_django/checkers/foreign_key_strings.py", line 114, in open
    django.setup()
  File "/venv/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/venv/lib/python3.11/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/django/apps/config.py", line 178, in create
    mod = import_module(mod_path)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'apps'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/venv/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
             ^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/pylint/__init__.py", line 34, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "/venv/lib/python3.11/site-packages/pylint/lint/run.py", line 211, in __init__
    linter.check(args)
  File "/venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 699, in check
    with self._astroid_module_checker() as check_astroid_module:
  File "/usr/local/lib/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 947, in _astroid_module_checker
    checker.open()
  File "/venv/lib/python3.11/site-packages/pylint_django/checkers/foreign_key_strings.py", line 120, in open
    args=self.config.django_settings_module,
         ^^^^^^^^^^^
AttributeError: 'ForeignKeyStringsChecker' object has no attribute 'config'

I have django project with such structure:

  • root_proj_dir
    • pyproject.toml
    • src
      • __ init__.py
      • manage.py
      • apps
        • __ init__.py
        • auth
          • apps.py
          • __ init__.py
          • models.py
      • config
        • settings.py

My auth app config file:

class IdentityConfig(AppConfig): 
    """ Identity App Config """

    default_auto_field = 'django.db.models.BigAutoField'
    name = 'apps.identity'

settings.py Installed Apps


INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'apps.identity.apps.IdentityConfig',
]

I define django-settings-module = "src.config.settings" in pyproject.toml config, but it doesn't help

Also I noticed that for pylint it can be fixed, if I add src prefix before app in my INSTALLED_APPS and AppConfig like 'src.apps.identity.apps.IdentityConfig' and name = 'src.apps.identity'. With src it will work fine, but usual app load crashes with import error.

Could someone help please with this problem of pylint and paths?


Solution

  • The problem was solved by adding ENV var - PYTHONPATH='/src'

    Seems that using non standard Django project structure with common folder apps for all applications broke the lib.