microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.25k stars 1.15k forks source link

VS Code can't find Homebrew-installed Conda #23162

Open kxmh42 opened 3 months ago

kxmh42 commented 3 months ago

Applies To

What happened?

  1. On macOS 14.3.1, install Conda with brew install miniconda
  2. Open VS Code and open any ipynb file.
  3. Press Shift-Cmd-P and click on "Notebook: Select Notebook Kernel"
  4. Click "Select Another Kernel..."
  5. Click "Python Environments..."
  6. Click "+ Create Python Environment"
  7. Click "Conda"

Expected result: A menu with a choice of the version of Python to install in the environment. Actual result: An error box in the bottom right corner, saying Install `conda` to create conda environments.

Probable root cause: the hardcoded glob patterns for searching Conda in src/test/interpreters/condaService.node.ts, which don't match /opt/homebrew/bin/conda.

Quick workaround: cd /opt && sudo ln -s homebrew conda (since /opt/conda/bin/conda is matched by the patterns).

A quick solution would be to add a pattern for Homebrew, and a better one would be to look for Conda in a way that is independent of the way it is installed.

VS Code Version

Version: 1.87.2 Commit: 863d2581ecda6849923a2118d93a088b0745d9d6 Date: 2024-03-08T15:20:57.520Z (3 wks ago) Electron: 27.3.2 ElectronBuildId: 26836302 Chromium: 118.0.5993.159 Node.js: 18.17.1 V8: 11.8.172.18-electron.0 OS: Darwin arm64 23.3.0

Jupyter Extension Version

v2024.2.0

Jupyter logs

No response

Coding Language and Runtime Version

No response

Language Extension Version (if applicable)

No response

Anaconda Version (if applicable)

No response

Running Jupyter locally or remotely?

Local

kxmh42 commented 2 months ago

I tested it with v2024.5.11021008 (pre-release), and it still doesn't work.

karthiknadig commented 2 months ago

@kxmh42 You can see here that we specifically look at /opt/homebrew/bin https://github.com/microsoft/vscode-python/blob/77e44cf2dacc7b6de24fe45318ecac2fd0f439f7/src/client/pythonEnvironments/common/environmentManagers/conda.ts#L345

The latest version of the extension is: 2024.5.11172159 and you will need latest VS Code insiders to install the latest python extension insiders.

kxmh42 commented 2 months ago

I updated to v2024.7.11241010 and can see the above prefixes.push call is present in the compiled code in .vscode/extensions/ms-python.python-2024.7.11241010/out/client/extension.js. However, the bug can still be reproduced with the exactly same steps as above – it cannot find Conda.

karthiknadig commented 2 months ago

Can you provide logs (Output > Python)again with the latest extension? Make sure you have VS Code version 1.89, and the python extension version 2024.6.0 or greater.

kxmh42 commented 2 months ago

VS Code v. 1.89.0, Python extension v2024.7.11241010

2024-05-06 15:44:14.928 [info] Telemetry level is off
2024-05-06 15:44:14.928 [info] Experiments are disabled, only manually opted experiments are active.
2024-05-06 15:44:15.010 [info] Default formatter is set to null for workspace /Users/kaxia/proj/new
2024-05-06 15:44:15.027 [info] > conda info --json
2024-05-06 15:44:15.029 [info] > hatch env show --json
2024-05-06 15:44:15.030 [info] cwd: .
2024-05-06 15:44:15.039 [info] Python interpreter path: /usr/bin/python3
2024-05-06 15:44:15.270 [info] > pyenv which python
2024-05-06 15:44:15.271 [info] cwd: .
2024-05-06 15:44:15.281 [info] > /opt/homebrew/bin/conda/bin/conda info --json
2024-05-06 15:44:15.281 [info] > /opt/homebrew/bin/conda2solv/bin/conda info --json
2024-05-06 15:44:15.290 [info] > ~/.conda/bin/conda info --json
2024-05-06 15:44:15.291 [info] Found: /usr/bin/python3 --> /usr/bin/python3
2024-05-06 15:44:15.319 [info] > /usr/bin/python3 -I ~/.vscode/extensions/ms-python.python-2024.7.11241010/python_files/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2024.7.11241010/python_files/interpreterInfo.py
2024-05-06 15:44:16.202 [info] Starting Pylance language server.
kxmh42 commented 2 months ago

See the comment in getCandidatesFromKnownPaths:

// Check common locations. We want to look up "<prefix>/*conda*/<suffix>", where prefix and suffix
// depend on the platform, to account for both Anaconda and Miniconda, and all possible variations.

And the definition of suffix:

const suffix = getOSType() === OSType.Windows ? 'Scripts\\conda.exe' : 'bin/conda';

Adding /opt/homebrew/bin to prefixes is incorrect, because now it is looking for the Conda binary in /opt/homebrew/bin/*conda*/bin/conda, and the correct binary path is /opt/homebrew/bin/conda.

In addition to /opt/homebrew/bin/conda, please also consider looking for Conda binary in ~/.local/bin/conda (that is, path.join(home, '.local', 'bin', 'conda')), as this is where the executable might be if it is installed locally on the user's account. I see the same error as above when I run VS Code remotely on a Linux server with Conda installed just on my account.

karthiknadig commented 1 month ago

@kxmh42 Thanks for looking into this. I got this wrong when I tried it, I will do a fix soon.