Search code examples
pythonneovimneovim-plugin

Python virtual environment support in NeoVim


I am using NeoVim to write Python code. Importing packages seems to work imperfectly at best. Consider the following:

enter image description here

My LSP seems not to recognize the package. However, the code seems to work fine:

enter image description here

The problem is not limited to numpy: it occurs with built in packages such as math, as well as with other imported packages, such as torch.

My question: why does this happen, and how can I fix it?

I am using treesitter for syntax highlighting, mason.nvim to manage LSPs, pyright and pylint as my LSP and linter, and nvim-conda to set up virtual environments in NeoVim. I’ve made sure that the correct conda environment is selected (one in which the relevant packages have been installed).

My question is similar to this one, but that one has received no answers; moreover it isn’t clear that the asker has the same config as I do.


Solution

  • I would suggest looking into linux-cultist/venv-selector.nvim which can achieve what you want and also allow you to automatically activate or switch virtual environments from within neovim.

    You haven't specified how your LSP is configured. It is most probably possible to configure the LSP to load a specific virtual environment and/or one relative to the working directory.