Closed paulfrische closed 1 year ago
Hello! Have you tried setting the pyls.plugins.jedi.environment
setting? I believe setting it to your project (or global) Python installation should fix things. See for example:
Hey thx, this solved my issue. wanna close this thread Have a Great Day!
@paulfrische Could you please elaborate more on how exactly did you fix it, i am having a hard time with this problem
lspconfig.pylsp.setup({
on_attach = M.attach,
settings = {
pylsp = {
plugins = {
jedi = {
environment = python,
}
}
}
}
})
EDIT: python is the path to the python Interpreter
local lspconfig = require('lspconfig')
lspconfig.pylsp.setup({
settings = {
pylsp = {
plugins = {
jedi = {
environment = "/usr/bin/python"
}
}
}
}
})
@paulfrische This is my current config and for some reason it is still using the python interpreter from the Mason venv, do you have any idea why?
Maybe you call lspconfig.pylsp.setup twice accidentally
Can someone tell me where I'm going wrong? Same issue as OP
-- Mason
local servers = {}
servers['pylsp'] = {
pylsp = {
plugins = {
jedi = {
environment = "/usr/bin/python3",
}
}
}
}
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
-- Ensure the servers above are installed
local mason_lspconfig = require 'mason-lspconfig'
mason_lspconfig.setup {
ensure_installed = vim.tbl_keys(servers),
}
...
mason_lspconfig.setup_handlers {
function(server_name)
require('lspconfig')[server_name].setup {
capabilities = capabilities,
on_attach = attachLSP,
settings = servers[server_name],
}
end,
}
The problem with setting a fixed path like /usr/bin/python
is that completion won't work for projects using custom virtualenvs. The solution is to get the interpreter path dynamically by running env which python
.
Sharing my solution here:
local lspconfig = require "lspconfig"
local on_attach = require("plugins.configs.lspconfig").on_attach
local function which_python()
local f = io.popen('env which python', 'r') or error("Fail to execute 'env which python'")
local s = f:read('*a') or error("Fail to read from io.popen result")
f:close()
return string.gsub(s, '%s+$', '')
end
lspconfig['pylsp'].setup {
on_attach = on_attach,
settings = {
pylsp = {
plugins = {
jedi = { environment = which_python() },
}
}
}
}
This still requires to run . $venv/bin/activate
before opening nvim. A better integration will be to detect pdm
, poetry
, etc... and call the appropriate subcommand to get the project virtualenv.
I've searched open issues for similar requests
I've manually reviewed logs to find potential errors
I've recently downloaded the latest plugin version of mason.nvim
Problem description
When installing pylsp via mason it doesn't recognize external dependencies/libraries you use because mason installs it in a venv.
Expected behavior
Code intelligence for external libs too.
Steps to reproduce
Affected packages
python-lsp-server
Neovim version (>= 0.7)
NVIM v0.10.0-dev-24+g3c724fe1f Build type: RelWithDebInfo LuaJIT 2.1.0-beta3
Operating system/version
Linux archlinux 6.2.12-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 20 Apr 2023 16:11:55 +0000 x86_64 GNU/Linux
Healthcheck
Screenshots or recordings
No response