PyLint Django:django.core.exceptions.ImproperlyConfigured:请求设置 LOGGING_CONFIG,但未配置设置

问题描述 投票:0回答:1

当我刚刚通过 python src/manage.py runserver 启动我的应用程序时,我的 Django 应用程序工作正常但我想在此处集成 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'

我有一个具有这种结构的django项目

  • root_proj_dir
    • pyproject.toml
    • 源代码
      • __ init__.py
      • 管理.py
      • 应用程序
        • __ init__.py
        • 验证
          • apps.py
          • __ init__.py
          • 模型.py
      • 配置
        • 设置.py

我的身份验证应用程序配置文件:

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

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

settings.py 安装的应用程序


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

我在 pyproject.toml 配置中定义了

django-settings-module = "src.config.settings"
,但没有帮助

我还注意到,对于 pylint,如果我在我的

src
INSTALLED_APPS
中的应用程序之前添加
AppConfig
前缀,例如
'src.apps.identity.apps.IdentityConfig'
name = 'src.apps.identity'
,则可以修复它。使用 src 可以正常工作,但通常的应用程序加载会因导入错误而崩溃。

有人可以帮忙解决 pylint 和路径的问题吗?

python django django-rest-framework pylint django-apps
1个回答
0
投票

尝试设置环境变量,如错误消息所述:

DJANGO_SETTINGS_MODULE=src.config.settings pylint ...

© www.soinside.com 2019 - 2024. All rights reserved.