'VSCode does not use conda environment when starting from the terminal

I'm using Visual Studio Code (VSCode) to do some Python programming on a Mac. I use conda for my Python environments. When I open a folder from within VSCode (using File -> Open... from the menu), it opens the selected folder of Python files. I select a Python file to edit it and VSCode selects my default conda environment which is named base. When I open the integrated VSCode terminal, it also uses the base conda environment. A screen capture of this process is shown below.

screen capture working example

I can also start VSCode from the macOS terminal using the code command. From within the Mac terminal, I cd to the folder containing the Python files. From within the directory, I open VSCode using the code . terminal command. I select a Python file to edit it and VSCode selects my default conda environment which is named base. When I open the integrated VSCode terminal, it says I'm using the base conda environment. However, the integrated VSCode terminal is not using conda environment Python and packages. A screen capture of this is shown below.

screen capture of nonworking example

My VSCode settings are:

{
    "telemetry.telemetryLevel": "off",
    "python.condaPath": "/opt/miniconda3/bin/conda",
    "python.defaultInterpreterPath": "/opt/miniconda3/bin/python",
    "python.pythonPath": "/opt/miniconda3/bin/python",
    "python.terminal.activateEnvInCurrentTerminal": true,
    "python.terminal.activateEnvironment": true,
    "python.linting.flake8Args": [
        "--max-line-length=120"
    ],
    "breadcrumbs.enabled": false,
    "terminal.integrated.defaultProfile.osx": "zsh",
    "terminal.explorerKind": "external",
    "terminal.integrated.inheritEnv": false,
    "python.linting.flake8Enabled": true,
    "editor.fontSize": 13,
    "terminal.integrated.fontSize": 13,
    "workbench.colorTheme": "Dracula Soft",
}

How can I make the integrated VSCode terminal use the default conda environment when the app is started from the macOS terminal?



Solution 1:[1]

In the command palette of VS Code, use > Python: Select Interpreter and select the version of python you want to use.

Once you select, it should be set as default, so that should be what you need.

To activate the environment in the integrated terminal, ensure that the setting python.terminal.activateEnvironment in VS Code is set to true.

Source: Visual Studio Code Documentation

Solution 2:[2]

Not a mac user, but I recently had to sort through a similar issue with vscode on Windows. It would not handle conda environments correctly until I added

"python.condaPath": "C:\\Users\\xxxx\\mambaforge\\Scripts\\conda.exe"

(Note for other Windows users: I also switched the default terminal to Command Prompt since conda and Powershell don't play perfectly well together)

I see that you have this variable in your settings file, but you're only leading the camel to water (/opt/miniconda3/bin/conda) not shoving its face into the bucket (/opt/miniconda3/bin/conda/Scripts/conda.exe, I presume), so give that a try. I know that the approach you are currently using did not resolve my issue until I pointed right at conda.exe in the Scripts folder (where the activation bat lives).

Another note to consider: I didn't set any of these lines in my settings.json (with one exception, noted):

{
    "python.defaultInterpreterPath": "/opt/miniconda3/bin/python",
    "python.pythonPath": "/opt/miniconda3/bin/python",
    "python.terminal.activateEnvInCurrentTerminal": true,
    "python.terminal.activateEnvironment": true,
    "python.linting.flake8Args": [
        "--max-line-length=120"
    ],
    "breadcrumbs.enabled": false,
    "terminal.explorerKind": "external",
    "terminal.integrated.inheritEnv": false, // <-- I actually have this set to 'true'
    "python.linting.flake8Enabled": true,
}

Not that any of these are necessarily coming into play here at all, just wanted to throw it out there in case it helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AzuxirenLeadGuy
Solution 2