从VSCode转NvChad后,如何配置python-lsp-server(pylsp)?
解决NvChad中null-ls归档问题及pylsp配置失效问题
一、替换归档的null-ls为none-ls
null-ls已停止维护,NvChad社区推荐使用none-ls.nvim作为替代,配置步骤如下:
- 添加none-ls插件
在nvim/lua/custom/plugins.lua中添加插件配置:
return { { "nvimtools/none-ls.nvim", event = "VeryLazy", dependencies = { "nvim-lua/plenary.nvim" }, config = function() require "custom.config.none-ls" end, }, }
- 创建none-ls配置文件
新建nvim/lua/custom/config/none-ls.lua,按需配置诊断、格式化工具(以Python为例):
local none_ls = require "none-ls" local formatting = none_ls.builtins.formatting local diagnostics = none_ls.builtins.diagnostics none_ls.setup({ debug = false, sources = { -- Python格式化与诊断工具 formatting.black, formatting.isort, diagnostics.flake8, -- 可添加其他语言工具,比如prettier、eslint等 }, on_attach = function(client, bufnr) -- 绑定手动格式化快捷键(可选) vim.keymap.set("n", "<leader>fm", vim.lsp.buf.format, { buffer = bufnr, desc = "Format current file" }) end, })
二、正确配置pylsp
你之前在lspconfig.lua中配置未生效,大概率是配置结构或调用方式错误,以下是标准配置步骤:
- 修改lspconfig配置文件
编辑nvim/lua/custom/config/lspconfig.lua,添加pylsp的完整配置:
local lspconfig = require "lspconfig" -- 配置pylsp lspconfig.pylsp.setup({ on_attach = function(client, bufnr) -- 加载NvChad默认的LSP快捷键映射 require("core.utils").load_mappings("lspconfig", { buffer = bufnr }) end, settings = { pylsp = { plugins = { -- 禁用冗余工具,启用需要的功能 pyflakes = { enabled = false }, flake8 = { enabled = true }, black = { enabled = true }, isort = { enabled = true }, pylint = { enabled = true }, -- 按需添加其他插件配置,比如mypy等 }, }, }, })
- 验证配置生效
重启Neovim后,打开Python文件,输入:LspInfo查看pylsp是否正常启动。如果未启动,检查以下几点:
- 确保通过Mason安装的pylsp路径正确(Mason默认安装路径为
~/.local/share/nvim/mason/packages/pylsp) - 确认Python环境中安装了pylsp依赖的工具(比如flake8、black需要全局或虚拟环境安装)
- 检查是否有其他LSP插件与pylsp冲突
内容的提问来源于stack exchange,提问作者Akashdip Baral
相关产品推荐
相关产品推荐

