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

wfsdck30  于 2023-11-20  发布在  Go
关注(0)|答案(2)|浏览(137)

当我通过python src/manage.py runserver启动我的应用程序时,我的Django应用程序运行良好,但我想在这里集成pylint,当我启动命令pylint src --load-plugins pylint_django时,我得到了以下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'

字符串
我有一个这样结构的Django项目:

  • 根项目目录
  • pyproject.toml
  • src
  • __ init__.py
  • manage.py
  • 应用程序
  • __ init__.py
  • auth
  • apps.py
  • __ init__.py
  • models.py
  • config
  • settings.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,它可以被修复,如果我在我的INSTALLED_APPSAppConfig中的应用程序之前添加src前缀,如'src.apps.identity.apps.IdentityConfig'name = 'src.apps.identity'。使用src它会工作得很好,但通常的应用程序加载会因导入错误而崩溃。
有没有人能帮我解决这个关于皮林特和路径的问题?

vsdwdz23

vsdwdz231#

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

wlzqhblo

wlzqhblo2#

通过添加ENV var -PYTHONPATH='/src'解决了该问题
似乎使用非标准的Django项目结构和所有应用程序的公共文件夹apps破坏了库。

相关问题