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

Мое Django-приложение работает нормально, если я запускаю его через python src/manage.py runserver. Однако я хочу интегрировать в проект pylint, и при запуске команды pylint src --load-plugins pylint_django я получаю следующую ошибку:

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'

Вот структура проекта:

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

Мой файл приложения Django:

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',
]

Я определил django-settings-module = "src.config.settings" in pyproject.toml конфиге, но это не помогло

Я также заметил, что все будет отлично работать, если добавить src к пути в приложениях INSTALLED_APPS и AppConfig наподобие 'src.apps.identity.apps.IdentityConfig' и name = 'src.apps.identity'. С src pylint работает хорошо, однако перестает работать обычный запуск django сервера с ошибкой о неправильных путях.

Может ли кто-нибудь пожалуйста помочь с этой ошибкой?

Спустя долгие мучения пофиксил с помощью установки "PYTHONPATH="src/." перед запуском pylint

Вернуться на верх