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

Neovim中Pyright语言服务无法加载新文件的问题排查

问题:不重启Neovim时,新建文件无法被项目其他文件作为模块导入

在不关闭Neovim实例的情况下创建新文件后,无法在项目其他文件中将该新文件作为模块导入其中的变量,必须重启Neovim才能解决。我的Neovim LSP基础配置如下:

lsp_zero.on_attach(function(client, bufnr)
  -- see :help lsp-zero-keybindings
  -- to learn the available actions
  lsp_zero.default_keymaps({buffer = bufnr})
  local opts = {buffer = bufnr, remap = false}

  vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
  vim.keymap.set("n", "K", function() vim.lsp.buf.hover() end, opts)
  vim.keymap.set("n", "<leader>vws", function() vim.lsp.buf.workspace_symbol() end, opts)
  vim.keymap.set("n", "<leader>vd", function() vim.diagnostic.open_float() end, opts)
  vim.keymap.set("n", "[d", function() vim.diagnostic.goto_next() end, opts)
  vim.keymap.set("n", "]d", function() vim.diagnostic.goto_prev() end, opts)
  vim.keymap.set("n", "<leader>vca", function() vim.lsp.buf.code_action() end, opts)
  vim.keymap.set("n", "<leader>vrr", function() vim.lsp.buf.references() end, opts)
  vim.keymap.set("n", "<leader>vrn", function() vim.lsp.buf.rename() end, opts)
  vim.keymap.set("i", "<C-h>", function() vim.lsp.buf.signature_help() end, opts)
end)

require('lspconfig').tsserver.setup({})
require('lspconfig').rust_analyzer.setup({})
require'lspconfig'.pyright.setup{}
require'lspconfig'.texlab.setup{}
require'lspconfig'.gopls.setup{}

更新

新文件的缓冲区与项目中其他文件存在差异,示例如下:

新文件的:LspInfo信息:

Press q or <Esc> to close this window. Press <Tab> to view server doc.
 
 Language client log: /Users/psr/.local/state/nvim/lsp.log
 Detected filetype:   python
 
 2 client(s) attached to this buffer: 
 
 Client: copilot (id: 1, bufnr: [4, 6, 8])
    filetypes:       
    autostart:       false
    root directory:  Running in single file mode.
    cmd:             node /Users/psr/.local/share/nvim/lazy/copilot.vim/dist/agent.js --stdio
 
 Client: pyright (id: 2, bufnr: [8, 6])
    filetypes:       python
    autostart:       true
    root directory:  /Users/psr/GitHub/[project-name]
    cmd:             /Users/psr/.nvm/versions/node/v21.6.1/bin/pyright-langserver --stdio
 
 Configured servers list: gopls, texlab, rust_analyzer, tsserver, pyright

项目中其他文件的:LspInfo信息:

Press q or <Esc> to close this window. Press <Tab> to view server doc.
 
 Language client log: /Users/psr/.local/state/nvim/lsp.log
 Detected filetype:   python
 
 2 client(s) attached to this buffer: 
 
 Client: copilot (id: 1, bufnr: [4, 6, 8, 13])
    filetypes:       
    autostart:       false
    root directory:  Running in single file mode.
    cmd:             node /Users/psr/.local/share/nvim/lazy/copilot.vim/dist/agent.js --stdio
 
 Client: pyright (id: 2, bufnr: [8, 13, 6])
    filetypes:       python
    autostart:       true
    root directory:  /Users/psr/GitHub/[project-name]
    cmd:             /Users/psr/.nvm/versions/node/v21.6.1/bin/pyright-langserver --stdio
 
 Configured servers list: gopls, texlab, rust_analyzer, tsserver, pyright

内容的提问来源于stack exchange,提问作者Prabhjot Singh Rai

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 03:00:16