Pylint-django 引发有关未配置 Django 的错误,但情况并非如此 (VSCode)

Pylint-django raising error about Django not being configured when that's not the case (VSCode)

Pylint-django 在版本 2 之前工作得很好。3.x 但是从 2.4.0 开始它在每个 python-django 文件上报告错误:

Django was not configured. For more information runpylint --load-plugins=pylint_django --help-msg=django-not-configuredpylint(django-not-configured)

这发生在 VSCode,我相信我已经正确配置了它:

    {
      "python.linting.pylintArgs": [
          "--load-plugins",
          "pylint_django",
          "--load-plugins",
          "pylint_django.checkers.migrations",
          "--disable=C0114, C0115, W0222",
          "--disable=imported-auth-user",
          "--disable=invalid-name",
          "--disable=line-too-long"
      ]
    }

正如我所说,在 v.2.3 之前,它工作得很好。

我在他们的 repository 上提出了一个问题,但遗憾的是,它被驳回了,几乎没有任何明显的努力来解决它。

目前我使用的是 v.2.3.0,上面的配置没有问题,但我想知道这是否是错误。

是否有人解决了这个问题,或者还有什么我遗漏的吗?

注:

可以通过在 VSCode 的 settings.json:

中添加此值来隐藏错误消息
  {
    "python.linting.pylintArgs": [
        [...]
        "--disable=django-not-configured",
    ]
  }

但我知道这是在扫地毯下的灰尘。

这也打击了我,我想我有一个解决方案。如果您在终端中 运行,按照错误消息的建议,使用参数 --help-msg=django-not-configured,您将得到以下消息:

Finding foreign-key relationships from strings in pylint-django requires
configuring Django. This can be done via the DJANGO_SETTINGS_MODULE
environment variable or the pylint option django-settings-module, eg: `pylint
--load-plugins=pylint_django --django-settings-module=myproject.settings` .
This can also be set as an option in a .pylintrc configuration file. Some
basic default settings were used, however this will lead to less accurate
linting. Consider passing in an explicit Django configuration file to match
your project to improve accuracy. This message belongs to the django foreign
keys referenced by strings checker.

所以要解决这个问题,您可以通过命令行参数在 .pylintrc 文件中传递所需的设置模块,或者设置(导出)您的环境变量。

希望对您有所帮助。

在 VS Code 设置中添加 --django-settings-module 参数:

  {
    "python.linting.pylintArgs": [
        [...]
        "--disable=django-not-configured",
        "--django-settings-module=<mainapp>.settings",
    ]
  }

<mainapp> 更改为您的主应用程序目录。例如,如果您的 settings.pysweetstuff/settings.py 中,则参数值将为 sweetstuff.settings。这与从 Python 模块或 Django shell.

中导入设置模块的格式相同

我在某些 Bitbucket 管道中遇到了这个问题。我通过创建 DJANGO_SETTINGS_MODULE 作为存储库变量在那里解决了它,然后在 Pylint 为 运行.

时作为环境变量可用

如果您想使用 .pylintrc 文件,请将以下内容添加到您的 .pylintrc 文件

注意:myproject替换为您的项目名称(您的Django项目的根目录。

[MASTER]
load-plugins=pylint_django
django-settings-module=myproject.settings

如果您没有,您可以创建一个,这里是一个示例

[MASTER]
load-plugins=pylint_django
django-settings-module=myproject.settings

[FORMAT]
max-line-length=120

[MESSAGES CONTROL]
disable=missing-docstring,invalid-name

[DESIGN]
max-parents=13

[TYPECHECK]
generated-members=REQUEST,acl_users,aq_parent,"[a-zA-Z]+_set{1,2}",save,delete

如果您在本地和生产环境中使用单独的设置文件,那么 使用相关的设置文件 ex:

django-settings-module=myproject.settings.local

@motash 的回答非常有效。一个小补充:如果您使用 Powershell 创建 .pylintrc 文件,则需要传入 UTF-8 编码: pylint --generate-rcfile | Out-File -Encoding utf8 .pylintrc

从那里,正如@motash 所说,将其添加到您的 .pylintrc 文件中:

load-plugins=pylint_django
django-settings-module=myproject.settings