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

如何在Neovim中配置pylsp以关闭指定格式类错误

关闭Neovim中pylsp的E302、E741格式错误

在Neovim中使用pylsp时,会触发E302(期望2个空行,实际找到1个)、E741(变量名'l'歧义)这类无意义的格式错误,干扰工作区。以下是基于lazy.nvim、Mason、mason-lspconfig和lspconfig的配置方案,直接修改你的lsp-config.lua即可解决:

修改lsp-config.lua中的pylsp配置

找到文件中nvim-lspconfig插件的配置段,将原本空的lspconfig.pylsp.setup({})替换为包含自定义检查规则的配置:

return {
  {
    "williamboman/mason.nvim",
    config = function()
      require("mason").setup()
    end
  },
  {
    "williamboman/mason-lspconfig.nvim",
    config = function()
      require("mason-lspconfig").setup({
        ensure_installed = {
          "clangd",
          "pylsp",
          "lua_ls",
          "texlab",
        }
      })
    end
  },
  {
    "neovim/nvim-lspconfig",
    config = function()
      local lspconfig = require("lspconfig")
      lspconfig.clangd.setup({})
      -- 修改此处的pylsp配置,添加规则关闭指定错误
      lspconfig.pylsp.setup({
        settings = {
          pylsp = {
            plugins = {
              pycodestyle = {
                ignore = {"E302", "E741"},
                maxLineLength = 120 -- 可选:自定义代码最大行长度,默认79,按需调整
              }
            }
          }
        }
      })
      lspconfig.lua_ls.setup({})
      lspconfig.texlab.setup({})
    end
  },
  {
    "nvim-telescope/telescope-ui-select.nvim",
    config = function()
      require("telescope").setup({
        extensions = {
          ["ui-select"] = {
            require("telescope.themes").get_dropdown {
            }
          }
        }
      })
      require("telescope").load_extension("ui-select")
    end
  },
}

配置说明

  • pycodestyle是pylsp默认启用的代码检查插件,通过ignore列表指定需要跳过的错误码,添加E302和E741即可关闭对应的提示。
  • 可选的maxLineLength可自定义代码行的最大长度,避免默认79行的限制报错,按需调整即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 09:50:01