How do I activate my conda environment for VS Code python debugger and testing?

Viewed 4701

When I try to run the debugger:

C:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara> cmd /C "C:/Users/Philip/miniconda3/envs/capra/python.exe c:\Users\Philip\.vscode\extensions\ms-python.python-2020.8.101144\pythonFiles\lib\python\debugpy\launcher 53607 -- c:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara\updater.py "
C:\Users\Philip\miniconda3\envs\capra\lib\site-packages\numpy\__init__.py:138: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init

I've worked out that if I install numpy into my base environment then I can get around the error. The question is how to activate the capra environment for the debugger?

I've tried following the guidance from VS Code:

1. pythonPath property of the selected debug configuration in launch.json

My launch.json file:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "pythonPath": "C:/Users/Philip/miniconda3/envs/capra/python.exe"
        }
    ]
}

Next:

2. python.pythonPath setting in the workspace settings.json

I restart VS Code and get:

We removed the "python.pythonPath" setting from your settings.json file as the setting is no longer used by the Python extension. You can get the path of your selected interpreter in the Python output channel. [Learn more](https://aka.ms/AA7jfor).

Lastly:

3. python.pythonPath setting in the user settings.json

My user settings file:

{
    "terminal.integrated.shell.windows": "C:/WINDOWS/System32/cmd.exe",
    "kite.showWelcomeNotificationOnStartup": false,
    "python.venvPath": "C:/Users/Philip/miniconda3/envs/capra",
    "python.pythonPath": "C:/Users/Philip/miniconda3/envs/capra/python.exe",
    "terminal.integrated.automationShell.windows": "C:/WINDOWS/System32/cmd.exe",
    "python.autoComplete.extraPaths": [
    
    ]
}

Still get the same numpy error.

Worth mentioning that I get exactly the same error when I look at the Python Test Log output after trying to discover tests.

3 Answers

You click on the Python 3.6 ..... bottom right side as shown in the picture below Click on red square part Python 3.6. ....

Then you just have to select the environments you have already created as shown in the picture below Select the environment in red circle

Then you run the code and code will run in the selected environment.

According to your description, you could try the following steps to use conda environment: (Example: Use python3.8 to create a conda environment named 'capra'.)

  1. Create a conda environment.

conda create -n capra python=3.8

After the creation is complete, reload VSCode, refresh and load several times.

  1. Select conda environment.

    click the lower left corner of VSCode to select the environment just created. enter image description here

  2. Activate the conda environment. You could use shortcut keys directly: Ctrl+Shift+`

    enter image description here

When debugging the code, the terminal shows that it is in the created environment:

enter image description here

  1. Install the required modules in the current conda environment.

In addition, for the'removed "python.pythonPath" setting' you mentioned, the reason is that you set the'workspace settings.json'. Now the python extension does not use the'pythonPath' here. When we set both in the two setting files, it will be removed. This will not affect the use of conda environment.

In my case, Visual Studio Code version:1.70.2, the Python environment selection was on the bottom right. I clicked there and changed the environment to the conda location. To find your conda environment and respective python version, issue the commands: pip --version and python --version. Where to click in VisualStudioCode

Related