Lsp: python virtualenv

Created on 14 Mar 2019  路  9Comments  路  Source: sublimelsp/LSP

I can't succeed making LSP work with python virtualenv.

I read some config hint here : https://github.com/tomv564/LSP/pull/395 but no result.

Please could you tell me the current way to work with virtualenv.

thanks

questioheldebug

Most helpful comment

I just set the PYTHONPATH environment variable on a per-project basis, in my Sublime Text project configuration. See the LSP part in my example Sublime Text project file below (including the whole thing for context):

{
    "folders": [
        {
            "path": "/path/to/project/dir"
        }
    ],
    "settings": {
        "python_interpreter": "/Users/mike/.virtualenvs/my-virtual-env/bin/python",
        "LSP": {
            "pyls": {
                "env": {
                    "PYTHONPATH": "/Users/mike/.virtualenvs/my-virtual-env/lib/python3.7/site-packages"
                }
            }
        }
    }
}

Change the /Users/mike/.virtualenvs/my-virtual-env/lib/python3.7/site-packages path to wherever your virtual environment's site-packages directory is located.

One advantage of this approach is that you get to set a per-project virtual environment for LSP, rather than trying to launch Sublime with the proper environment activated each time

All 9 comments

One reliable way to do it: launch sublime text so it inherits the environment from an activated virtualenv.
My guess is pyls is not in your sublime environment's PATH - the issue #414 might have some relevant tips?

It worked for me with the following user configuration:

{
    "clients":
    {
        "pyls":
        {
            "enabled": true,
            "command": ["venv/bin/pyls"]
        }
    }
}

One reliable way to do it: launch sublime text so it inherits the environment from an activated virtualenv.

That works, but (correct me if I'm wrong) then it seems that I can only work on one project at a time, as the environment gets set to the first project that I opened in a virtualenv, and when I open a second project it keeps the environment that the Sublime Text 3 was opened with. I have to close the application and reopen it with a new environment to change the pyls.

It seems that the current best practice is to put the LSP inside the virtualenv so that it can have its environment set correctly. Is there a way to configure LSP from the .sublime-project file? That way we can use the global environment by default (configured in the LSP.sublime-settings user file), but when inside a project use the project's environment (configured in the .sublime-project file).

Hi,
running subl from activated add the virtualenv to the path, but does not find lib installed in the virtualenv.

my configuration is :

    "default_clients":
    {
        "pyls":
        {
            "command":
            [
                "pyls"
            ],
            "languageId": "python",
            "scopes":
            [
                "source.python"
            ],
            "settings":
            {
                "pyls":
                {
                    "follow_imports": true,
                    "python_interpreter": "/home/jimmy/.cache/pypoetry/virtualenvs/kns7_gallery_api-FKZrd98_-py3.6/bin/pyhon",
                    "python_virtualenv": "/home/jimmy/.cache/pypoetry/virtualenvs/kns7_gallery_api-FKZrd98_-py3.6"

                }
            },
            "syntaxes":
            [
                "Packages/Python/Python.sublime-syntax",
                "Packages/MagicPython/grammars/MagicPython.tmLanguage",
                "Packages/Djaneiro/Syntaxes/Python Django.tmLanguage"
            ]
        },
}

I don't know what else I can do ?

I just set the PYTHONPATH environment variable on a per-project basis, in my Sublime Text project configuration. See the LSP part in my example Sublime Text project file below (including the whole thing for context):

{
    "folders": [
        {
            "path": "/path/to/project/dir"
        }
    ],
    "settings": {
        "python_interpreter": "/Users/mike/.virtualenvs/my-virtual-env/bin/python",
        "LSP": {
            "pyls": {
                "env": {
                    "PYTHONPATH": "/Users/mike/.virtualenvs/my-virtual-env/lib/python3.7/site-packages"
                }
            }
        }
    }
}

Change the /Users/mike/.virtualenvs/my-virtual-env/lib/python3.7/site-packages path to wherever your virtual environment's site-packages directory is located.

One advantage of this approach is that you get to set a per-project virtual environment for LSP, rather than trying to launch Sublime with the proper environment activated each time

that's great.
python_interpreter is a setting used in many plugins.
Maybe LSP could guess pythonpath from python_interpreter variable ?

@jgirardet LSP would never read python_interpreter as it doesn't want code specific for one language server only.

Are things working for you now?

Here's my configs which i use for cases when i don't have a project but one python file. To activate it i use the command "LSP: Enable Language Server In Project" and select pyls-p2venv. This way you can configure different virtualenvs and enable whichever you want.

"clients": {
        "pyls-p2venv":
            {
              "command": ["e:/WorkFiles/Dev/pythonEnvs/p2venv/Scripts/pyls.exe"],
              "scopes": ["source.python"],
              "syntaxes": ["Packages/Python/Python.sublime-syntax", "Packages/MagicPython/grammars/MagicPython.tmLanguage"],
              "languageId": "python",
            }
    }

It looks like setting VIRTUAL_ENV ENV variable instead of PYTHONPATH also works, and sounds a lot less intrusive. Here's an example project configuration:

{
    "folders": [
        {
            "path": "/path/to/project/dir"
        }
    ],
    "settings": {
        "LSP": {
            "pyls": {
                "enabled": true,
                "env": {
                    "VIRTUAL_ENV": "/Users/mike/.virtualenvs/my-virtual-env"
                }
            }
        }
    }
}

I haven't tested this thoroughly yet, but Jedi has supported this since 2012: https://github.com/davidhalter/jedi/commit/580a4f4ab019a09fed6975f0a26065d16f767b98

It looks like setting VIRTUAL_ENV ENV variable instead of PYTHONPATH also works, and sounds a lot less intrusive. Here's an example project configuration:

{
    "folders": [
        {
            "path": "/path/to/project/dir"
        }
    ],
    "settings": {
        "LSP": {
            "pyls": {
                "enabled": true,
                "env": {
                    "VIRTUAL_ENV": "/Users/mike/.virtualenvs/my-virtual-env"
                }
            }
        }
    }
}

I haven't tested this thoroughly yet, but Jedi has supported this since 2012: davidhalter/jedi@580a4f4

Great tip! Even simpler, and has worked so far in my test cases.

Was this page helpful?
0 / 5 - 0 ratings