Skip to content

Conda PATH entries added to end of $PATH instead of start on macOS #17959

@mfripp

Description

@mfripp

VS Code version

1.62.0

Extension version

v2021.11.1422169775

OS type

macOS

OS version

10.14.6

Python distribution

Anaconda

Python version

3.9

Language server

Default

Expected behaviour

When I open a new terminal pane and run which python, I expect it to show the interpreter that I have selected for the current project.

When I open a terminal pane and type echo $PATH I expect it to show /Users/<me>/opt/miniconda3/bin (the interpreter selected for this project) near the start, somewhere before /usr/bin, where the system Python is installed.

If I open a terminal pane and type python, I expect it to load the version I selected for the current project.

Actual behaviour

which python shows the built-in interpreter at /usr/bin/python

echo $PATH shows /Users/<me>/opt/miniconda3/bin near the end, after /usr/bin

running python loads the system interpreter from /usr/bin, not the one I selected in /Users/<me>/opt/miniconda3/bin

Steps to reproduce

This is with a brand-new installation of VS Code and miniconda3 on macOS. This appears to occur because VS Code adds /Users/<me>/opt/miniconda3/bin to the end of the $PATH variable before launching the bash terminal when I choose Terminal > New Terminal.

If I add echo $PATH to the top and bottom of my ~/.bash_profile file, I can see that

(1) the .bash_profile script is run when the terminal starts, as it should be, but
(2) /Users/<me>/opt/miniconda3/bin is already on the end of $PATH before .bash_profile runs, so
(3) when .bash_profile, calls conda.sh code, conda.sh decides not to add its path to the start of the $PATH variable, so
(4) /usr/bin remains earlier in the path than /Users/<me>/opt/miniconda3/bin, which it should not be.

There are a few successful workarounds for this on the internet:

  • This VS code issue recommends calling conda deactivate from the terminal, then conda activate. (If you are using an environment other than the base, you have to use conda deactivate twice.) This removes /Users/<me>/opt/miniconda3/bin from the end of $PATH but leaves /Users/<me>/opt/miniconda3/condabin, so then when you run conda activate or conda activate <env>, it adds /Users/<me>/opt/miniconda3/bin to the start of the $PATH.
  • This comment on the same issue recommends using terminal.integrated.shellArgs.osx: [] in settings.json, which I guess prevents VS Code from adding a path-extending argument, so then .bash_profile does its job correctly
  • This stackoverflow answer recommends adding "terminal.integrated.env.osx": {"PATH": ""} in settings.json, which presumably also prevents VS Code from extending the path when running bash
  • This may be the easiest workaround: uncheck the option for "Terminal › Integrated: Inherit Env" in the preferences. When this is done, bash inherits a pretty clean $PATH, identical to what is shown before running .bash_profile in Terminal.app, then .bash_profile adds the conda path correctly and conda activate ... updates the path as needed. I haven't seen this on the internet.
  • This vscode-python issue identifies the problem but was closed without resolution.

However, it should not be necessary to use these workarounds. The simple change would be for VS Code to add the project's interpreter to the start of the $PATH instead of the end, or not to add it at all, and let .bash_profile take care of it.

This is different from the issue of running bash as a non-login shell. I think VS Code now launches bash as a login shell by default, which is great, because that's the best way to get the conda settings. But since VS Code is sticking the conda path at the end of the $PATH before running bash, it ends up thwarting the proper path-setting by .bash_profile.

Logs

Experiment 'pythonaacf' is active
Experiment 'pythonTensorboardExperiment' is active
Experiment 'pythonSurveyNotificationcf' is active
Experiment 'PythonPyTorchProfiler' is active
Experiment 'pythonDeprecatePythonPath' is active
Experiment 'pythonRunFailedTestsButtonDisplayed' is active
Experiment 'pythonRefreshTestsButtonDisplayed' is active
Experiment 'pythonRememberDebugConfig' is active

conda info --json
~/opt/miniconda3/bin/python ~/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/interpreterInfo.py
/usr/bin/python ~/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/interpreterInfo.py
/usr/bin/python2.7 ~/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/interpreterInfo.py
Python interpreter path: ~/opt/miniconda3/bin/python
/usr/lib/python2.7 ~/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/interpreterInfo.py
/usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/interpreterInfo.py
/usr/local/bin/python3.6 ~/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/interpreterInfo.py
/usr/local/bin/python3.8 ~/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/interpreterInfo.py
~/opt/miniconda3/envs/switch/bin/python ~/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/interpreterInfo.py
Starting Pylance language server.
Python interpreter path: ~/opt/miniconda3/envs/switch/bin/python
conda info --json
conda info --json
conda info --json
conda info --json
conda info --json
conda info --json
conda info --json
conda info --json
conda info --json

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions