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

Neovim搭配Pyright时Python代码补全受限问题求助

Neovim + Pyright补全问题求助

我是Neovim和Pyright新手,按教程配置完后,只有for循环的代码片段补全能正常工作,另外两种场景完全没补全提示:

  • 写for value in...时,变量"list"没有补全;
  • 代码开头已经import os,但输入os.w...时,os.walk的补全根本不出来。

想搞清楚这是配置有问题还是Pyright本身的限制,求各位大佬给点建议。

系统信息

  • Linux Mint 6
  • Python 3.11
  • Neovim v0.10.2

配置文件

lsp-config.lua

return {
    {
        "williamboman/mason.nvim", -- installation
        lazy = false,
        config = function()
            require("mason").setup() -- setup
        end,
    },
    {
        "williamboman/mason-lspconfig.nvim", -- installation
        lazy = false,
        opts = {
            auto_install = true, -- instead of something like ensure_installed = { "lua_ls", "pyright" },
        },
    },
    {
        "neovim/nvim-lspconfig",
        lazy = false,
        config = function()
            local capabilities = require("cmp_nvim_lsp").default_capabilities()
            local lspconfig = require("lspconfig")
            lspconfig.html.setup({
                capabilities = capabilities,
            })
            lspconfig.lua_ls.setup({
                capabilities = capabilities,
            })
            lspconfig.pyright.setup({
                capabilities = capabilities,
            })

            vim.keymap.set("n", "K", vim.lsp.buf.hover, {})
            vim.keymap.set("n", "<leader>gd", vim.lsp.buf.definition, {})
            vim.keymap.set("n", "<leader>gr", vim.lsp.buf.references, {})
            vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, {})
        end,
    },
}

none-ls.lua

return {
    "nvimtools/none-ls.nvim", -- calls none-ls through short github url
    config = function()
        local null_ls = require("null-ls")
        null_ls.setup({
            sources = {
                null_ls.builtins.formatting.stylua, -- formatter for lua installed with Mason
                null_ls.builtins.formatting.black, -- formatter for python installed with Mason
                null_ls.builtins.formatting.isort, -- formatter for python installed with Mason
            },
        })
        vim.keymap.set("n", "<leader>gf", vim.lsp.buf.format, {}) -- for instance, adds double quotes instead of single quotes when the relevant language server advices it
    end,
}

completions.lua

return {
    { "hrsh7th/cmp-nvim-lsp" },
    {
        "L3MON4D3/LuaSnip",
        dependencies = {
            "saadparwaiz1/cmp_luasnip",
            "rafamadriz/friendly-snippets",
        },
    },
    {
        "hrsh7th/nvim-cmp",
        config = function()
            local cmp = require("cmp")
            require("luasnip.loaders.from_vscode").lazy_load()

            cmp.setup({
                snippet = {
                    expand = function(args)
                        require("luasnip").lsp_expand(args.body) -- For `luasnip` users
                    end,
                },
                window = {
                    completion = cmp.config.window.bordered(),
                    documentation = cmp.config.window.bordered(),
                },
                mapping = cmp.mapping.preset.insert({
                    ["<C-b>"] = cmp.mapping.scroll_docs(-4),
                    ["<C-f>"] = cmp.mapping.scroll_docs(4),
                    ["<C-Space>"] = cmp.mapping.complete(),
                    ["<C-e>"] = cmp.mapping.abort(),
                    ["<CR>"] = cmp.mapping.confirm({ select = true }),
                }),
                sources = cmp.config.sources({
                    -- { name = 'nvim_lsp' },
                    { name = "luasnip" }, -- For luasnip users.
                }, {
                    { name = "buffer" },
                }),
            })
        end,
    },
}

问题分析与解决方案

核心问题:LSP补全源未启用

看你的completions.lua配置,nvim-cmp的sources里把nvim_lsp给注释掉了!这就导致Pyright提供的LSP补全完全没被加载,你现在能看到的只有LuaSnip的代码片段和buffer里的文本补全,自然没有变量、模块成员的智能补全。

修复步骤

  1. 打开LSP补全源
    修改completions.lua里的sources部分,取消nvim_lsp的注释:
sources = cmp.config.sources({
    { name = 'nvim_lsp' },
    { name = "luasnip" }, -- For luasnip users.
}, {
    { name = "buffer" },
}),
  1. 验证Pyright状态
    打开Python文件后,输入:LspInfo,确认Pyright显示为"active",如果没激活,检查Mason是否正确安装了Pyright,或者手动通过:MasonInstall pyright安装。

  2. 确保Pyright识别Python环境
    Pyright需要知道当前使用的Python解释器,你可以在lsp-config.lua的Pyright配置里添加settings:

lspconfig.pyright.setup({
    capabilities = capabilities,
    settings = {
        python = {
            pythonPath = "/usr/bin/python3.11", -- 换成你的Python实际路径
            analysis = {
                autoSearchPaths = true,
                useLibraryCodeForTypes = true,
            }
        }
    }
})

如果用虚拟环境,Pyright通常会自动检测,也可以手动指定venv路径。

  1. 重启Neovim
    修改配置后重启Neovim,让配置生效,再测试补全功能。

额外建议

  • 若补全仍有问题,可查看Pyright日志(:LspLog),里面会有详细错误信息;
  • 确保你的Python代码无语法错误,Pyright在代码有语法问题时可能会停止提供补全;
  • 可以安装nvim-lsp-installer辅助管理LSP服务器,或确认Mason已正确维护Pyright。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 15:47:33