Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conda environment is not recognized #348

Closed
wigging opened this issue Aug 10, 2024 · 13 comments · Fixed by #349
Closed

Conda environment is not recognized #348

wigging opened this issue Aug 10, 2024 · 13 comments · Fixed by #349
Labels
bug Something isn't working

Comments

@wigging
Copy link

wigging commented Aug 10, 2024

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:

  • macOS Sonoma 14.6.1
  • Sublime Text Build 4180
  • conda 24.7.1
  • LSP-pyright 1.4.12
  • Python 3.12.4

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
Copy link
Collaborator

jfcherng commented Aug 10, 2024

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
Copy link
Author

wigging commented Aug 10, 2024

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

condaprefix

@jfcherng jfcherng added the bug Something isn't working label Aug 10, 2024
@jfcherng
Copy link
Collaborator

jfcherng commented Aug 10, 2024

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
Copy link
Author

wigging commented Aug 11, 2024

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
Copy link
Collaborator

jfcherng commented Aug 11, 2024

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
Copy link
Author

wigging commented Aug 11, 2024

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

@wigging
Copy link
Author

wigging commented Aug 11, 2024

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
Copy link
Author

wigging commented Aug 11, 2024

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

from platform import python_version
python_version()

@jfcherng
Copy link
Collaborator

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

@jfcherng
Copy link
Collaborator

jfcherng commented Aug 11, 2024

Could you try this PR: #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
Copy link
Author

wigging commented Aug 11, 2024

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
Copy link
Author

wigging commented Aug 12, 2024

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
Copy link
Collaborator

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.

jfcherng added a commit to sublimelsp/LSP-basedpyright that referenced this issue Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants