Sublime Forum

Use Sublime Text with Python conda environments

#1

I’m a macOS user and have Python installed via Miniconda which is basically a lightweight version of Anaconda. All of my Python development is done within conda environments. These environments have different Python packages installed based on the project that the environment is for.

For writing my Python code, I use Sublime Text 4 with the LSP-pylsp package. I launch Sublime Text from the terminal where the Python environment is already active. Sometimes things like autocompletion, displaying docstrings, and other LSP functionality work fine. But other times I don’t get any autocompletion or docstring support in Python files. So it seems like Sublime Text does not recognize certain activated conda environments. I tried the settings shown below in my .sublime-project file but it doesn’t fix my issue.

{
    "python_binary": "/opt/miniconda3/envs/myproj/bin/python",
    "folders":
    [
        {
            "path": "."
        }
    ],
    "settings": {
        "LSP": {
            "LSP-pylsp": {
                "settings": {
                    "pylsp.plugins.jedi.environment": "/opt/miniconda3/envs/myproj"
                }
            }
        }
    }
}

And here is my LSP-pylsp.sublime-settings file.

{
    "settings": {
        "pylsp.configurationSources": [
            "flake8"
        ],
        "pylsp.plugins.pycodestyle.enabled": false,
        "pylsp.plugins.flake8.enabled": true,
        "pylsp.plugins.flake8.maxLineLength": 120,
        "pylsp.plugins.pylsp_black.enabled": true,
    }
}

Is there something else I need to configure to make sure Sublime Text is using the activated conda environment? And how can I tell which conda environment it is using?

0 Likes

#2

Were you able to get this working? I’ve been running into this issue as well, trying to work with condo environments and the LSP based completion, but have yet to find out how to get this to work

0 Likes

#3

That depends on the LSP server you use.

0 Likes

#4

Can you elaborate? Which LSP server. have you been able to configure to work with conda environments?

0 Likes

#5

That’s my question but now you ask me the same. OP uses LSP-pylsp. I use LSP-pyright, sort of.

I don’t use conda but I don’t think it’s required to help. The key is always to let the server know the path of the python executable you used.

0 Likes

#6

I’m no longer using LSP-pylsp. I switched to LSP-pyright because it seems better maintained and has more features.

Regarding the original question that I posted here… In my terminal I activate the conda environment. In that same terminal session, I then launch Sublime Text from the command line. Sublime automatically recognizes the conda environment so there’s no need to define the path to the conda environment in the settings.

If you run the following in the Sublime Text console, you can confirm that it is using the conda environment:

import os; print(os.getenv("CONDA_PREFIX"))
0 Likes