1

I'm using nvim-lsp and pyright. That latter provides a command PyrightSetPythonPath to reset the path to the python interpreter. Since I can see no other way to perform the same task, I'd like to call this command whenever it becomes available.

NB LspAttach does not work since PyrightSetPythonPath seems to only become available a bit later after LSP attaching.

local callback = function ()
    if vim.fn.exists ':PyrightSetPythonPath' == 2 then
        local wherepython = (function ()
            local f = io.popen('where python')
            local where = f:read()
            f:close()
            return where
        end)()

        vim.cmd.PyrightSetPythonPath(wherepython)
    end
end

local pyright_group = vim.api.nvim_create_augroup('pyright_set_path', {})
vim.api.nvim_create_autocmd('LspAttach', { callback = callback
                                         , pattern = '*.py'
                                         , group = pyright_group
                                         })
-- does not work

1 Answer 1

0

I don’t use nvim-lsp, but if you look at the source, you’ll see that you should be able to set config.settings.python.pythonPath the same as you do with the rest of your configuration.

Besides that, there is the CmdUndefined and FuncUndefined autocommands that lazy load specific commands until they are actually used. I don’t know if it would help in this situation.

Lastly, maybe try using vim.schedule or vim.schedule_wrap in your callback to see if that will get it to work if neither of the things above do. You could also try removing the lazy-loading of your plugin if that is what you’re doing as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.