Closed wigging closed 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?
It returns the path to the conda environment. See the screenshot below.
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.
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.
Why do you need to find pyvenv.cfg?
Because I need these information. Ideally, without invoking a process.
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.
You can get the name of the conda environment from the path without having to use pyvenv.cfg
.
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")
You can use this to get the Python version of the activated conda environment:
from platform import python_version
python_version()
Thanks. But the issue is not I don't know how to get those. Refactoring is required.
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.
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?
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
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.
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:
I have a simple example of using NumPy:
Everything works fine and LSP-pyright finds the venv environment as shown in the status bar:
Not working with conda
I created and activated a Python conda environment with NumPy installed and opened Sublime Text using:
I have a simple example of using NumPy:
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.
I am also not seeing anything regarding the conda environment in the LSP-pyright status bar: