I am using Neovim and installed mypy
with mason
, and I also configured it using null-nvim
:
local sources = { null_ls.builtins.diagnostics.mypy }
Here, mypy
, installed by mason
, in fact, is installed in a separate virtual environment under ~/.local/share/nvim/mason/packages/mypy
. It seems that mypy
will always raise warnings "cannot find implementation or library stub" if the third-party library is installed in another environment.
For example, I activated myenv
where openai
is installed, and then I opened a Python file using Neovim:
On the other hand, if I installed mypy
inside myenv
, then everything works well. But this method requires me to install every copy of mypy
for every virtual environment. How to avoid the warning message when I installed it via mason
?
For example, if you are running your code in a virtualenv, make sure to install and use
mypy
within the virtualenv. Alternatively, if you want to use a globally installedmypy
, set the--python-executable
command line flag to point the Python interpreter containing your installed third party packages.
It seems that mypy
is not able to detect the current virutalenv I am using.
Here is a workaround way in both Linux and macOS:
nls.builtins.diagnostics.mypy.with({
extra_args = function()
local virtual = os.getenv("VIRTUAL_ENV") or os.getenv("CONDA_PREFIX") or "/usr"
return { "--python-executable", virtual .. "/bin/python3" }
end,
}),
The code above detects virutalenv first, and otherwise, it will use default /usr/bin/python3
as the --python-executable
.