sublimelsp / LSP-pyright

Python support for Sublime's LSP plugin provided through microsoft/pyright.
MIT License
131 stars 13 forks source link

Conda environment is not recognized #348

Closed wigging closed 3 months ago

wigging commented 3 months ago

The LSP-pyright package isn't finding my conda environment. But it seems to be working with a Python virtual environment. I'm using the following configuration:

Working with Python venv

I created and activated a Python virtual environment, installed NumPy, then opened Sublime Text as shown below:

python -m venv .venv
source .venv/bin/activate
pip install numpy
subl .

I have a simple example of using NumPy:

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a)

Everything works fine and LSP-pyright finds the venv environment as shown in the status bar:

pyright-status-venv

Not working with conda

I created and activated a Python conda environment with NumPy installed and opened Sublime Text using:

conda create --name envnumpy python numpy
conda activate envnumpy
subl .

I have a simple example of using NumPy:

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a)

I can run the example without any problems but LSP-pyright is not able to resolve the NumPy source as shown below. This does not happen when I use a Python virtual environment.

pyright-conda-resolve

I am also not seeing anything regarding the conda environment in the LSP-pyright status bar:

pyright-status-conda
jfcherng commented 3 months ago

When

conda create --name envnumpy python numpy
conda activate envnumpy
subl .

, what's the output of import os; os.environ.get('CONDA_PREFIX') in ST console?

wigging commented 3 months ago

It returns the path to the conda environment. See the screenshot below.

condaprefix
jfcherng commented 3 months ago

The issue is that I check for VENV_DIR/pyvenv.cfg (pep405) to validate whether a directory (in this case, CONDA_PREFIX) is a venv or not. But somehow CONDA_PREFIX/pyvenv.cfg doesn't exist.

Update: It looks like Conda doesn't follow pep405. I will implement another way for finding Conda venv soon.

wigging commented 3 months ago

Why do you need to find pyvenv.cfg? If you activate a conda environment, then do echo $CONDA_PREFIX, it will return the path to the activated conda environment. If you are not in an active conda environment and do echo $CONDA_PREFIX, then it will not return anything. Seems like that should be enough to determine if a conda environment is active and where the environment is located.

jfcherng commented 3 months ago

Why do you need to find pyvenv.cfg?

Because I need these information. Ideally, without invoking a process. image

And I thought Conda follows pep405 too, so all venv finders can have a simple abstraction. But I don't use it and find it's not now.

wigging commented 3 months ago

You can get the name of the conda environment from the path without having to use pyvenv.cfg.

wigging commented 3 months ago

Using just Python, this gets the name of the activated conda environment:

import os
os.getenv("CONDA_DEFAULT_ENV")

And this gets the path to the activated conda environment:

import os
os.getenv("CONDA_PREFIX")
wigging commented 3 months ago

You can use this to get the Python version of the activated conda environment:

from platform import python_version
python_version()
jfcherng commented 3 months ago

Thanks. But the issue is not I don't know how to get those. Refactoring is required.

jfcherng commented 3 months ago

Could you try this PR: https://github.com/sublimelsp/LSP-pyright/pull/349

It's based on my Ubuntu virtual machine so I am not sure whether it will be different on MacOS, but hopefully it won't.

wigging commented 3 months ago

The Browse Packages... in Sublime Text just shows the sublime-settings files for packages that I have installed. Where are the files located for the LSP-pyright package?

wigging commented 3 months ago

I copied your changes in the fix/venv-conda and put them in the ~/Library/Application Support/Sublime Text/Packages/LSP-pyright directory. The directory tree is shown below. These changes seem to fix the problems I had with conda environments on macOS. Thank you.

LSP-pyright
└── plugin
    ├── client.py
    ├── utils.py
    └── virtual_env
        ├── __init__.py
        ├── helpers.py
        ├── venv_finder.py
        └── venv_info.py
jfcherng commented 3 months ago

I have made a new release 1.4.13, which should be available on Package Control within hours. You can remove those override when it comes.