palantir / python-language-server

An implementation of the Language Server Protocol for Python
MIT License
2.6k stars 281 forks source link

mypy cannot locate local packages #970

Open MKrbm opened 5 months ago

MKrbm commented 5 months ago

I am experiencing issues with integrating the Python Language Server (pylsp) in Neovim while using a Conda virtual environment. Despite configuring pylsp and mypy within Neovim, the language server is unable to locate my local Python package, though running mypy directly in the terminal detects the package without any issues.

The error messages displaying is that : Cannot find implementation or library stub for module named "my_lib"

Environment

Configuration

Here is the relevant part of my Neovim configuration for pylsp:

local python_folder = os.getenv("CONDA_PREFIX")
local python_executable = python_folder .. "/bin/python"
local pylsp_executable = python_folder .. "/bin/pylsp"

require("lspconfig").pylsp.setup({
    cmd = { pylsp_executable, "--log-file", "/tmp/pylsp.log" },
    filetypes = { "python" },
    settings = {
        pylsp = {
            configurationSources = { "pycodestyle" },
            formatCommand = { "autopep8" },
            plugins = {
                -- Lint
                pycodestyle = {
                    enabled = true,
                    maxLineLength = 100, -- default: 79
                    aggressive = 2,
                },
                mypy = {
                    enabled = true,
                    live_mode = true,
                    strict = true,
                    overrides = { "--python-executable", python_executable },
                },
                -- -- Code refactor
                rope = { enabled = true }, -- This is a python refactoring library (refactor means renaming, extracting functions, ...)

                pydocstyle = { -- n: make sure to install pydocstyle before using it
                    enabled = true,
                    convention = "google",
                },

                -- Formatting
                black = { enabled = false },
                pyls_isort = { enabled = false },
                yapf = { enabled = false },
                autopep8 = { enabled = true, maxLineLength = 100 }, -- autopep8 is a python formatting library (it fixes the pycodestyle errors)
            },
            log_file = "/tmp/pylsp.log",
        },
    },
})

Expected Behavior

I expect pylsp to correctly locate and interact with my local Python package within the Conda environment, similar to how mypy operates when run directly in the terminal.

Actual Behavior

The language server does not locate my local Python package, causing issues with linting, type checking, and other features provided by pylsp and mypy.

Steps to Reproduce

  1. create virtual env with conda
  2. Install python-lsp and mypy via pip install "python-lsp-server[all]" pylsp-mypy
  3. activate the virtual env and run nvim

Additional Note