You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pylint_django的Django项目无法触发print语句警告如何解决?

解决Pylint不检测Python3 print()函数的问题

核心问题

你当前启用的print-statement检查项仅针对Python 2中的print语句(无括号形式),而Python 3中print是函数,对应的检查项是print-function,这是Pylint未触发警告的根本原因。

修复步骤

  1. 修正.pylintrc配置
    在[MESSAGES CONTROL]区块中,将print-statement替换为print-function:

    [MESSAGES CONTROL]
    confidence=
    
    disable=missing-docstring,
            invalid-name,
            astroid-error,
            protected-access,
            broad-except
    
    enable=c-extension-no-member, print-function
    

    同时建议在全局配置中加载pylint_django插件,避免VSCode参数与配置文件不一致:

    load-plugins=pylint_django
    
  2. 简化VSCode配置(可选)
    既然已经在.pylintrc中配置了插件加载和检查项,可以移除VSCode settings.json中的重复参数,保持配置统一:

    {
      "python.linting.pylintEnabled": true,
      "python.linting.enabled": true,
      "python.linting.flake8Enabled": false,
      "python.linting.prospectorEnabled": false,
      "python.linting.pylintArgs": [
        "--rcfile=.pylintrc"
      ]
    }
    
  3. 验证生效

    • 重启VSCode,让配置重新加载
    • 在代码中写入print("test"),保存后查看Pylint是否触发警告

进阶:强制禁用print

如果需要将print的使用标记为错误而非警告,可以在.pylintrc中添加:

[MESSAGES CONTROL]
# 将print-function的级别设为error
message-control=print-function:error

内容的提问来源于stack exchange,提问作者Alijonov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 14:25:18