Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't VSCode activate conda before starting the debugger?

When I start the debugger in VSCode, the conda environment only gets activated after the debugging process has stopped with a "Missing required dependencies" import error. Immediately restarting the debugger works fine then.

This problem occurs with the Anaconda "base" and other environments.

Test code: import_pandas.py

    import pandas
    print("Pandas import succeeded!")

VSCode Python Debug Console:

Microsoft Windows [Version 10.0.16299.1146]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\peter\demo>cd c:\Users\peter\demo && 
    cmd /C "set "PYTHONIOENCODING=UTF-8" && 
    set "PYTHONUNBUFFERED=1" && 
    C:\Users\peter\AppData\Local\Anaconda3\python.exe 
        c:/Users/peter/.vscode/extensions/ms-python.python-2019.5.18678/pythonFiles/ptvsd_launcher.py 
            --default --client --host localhost --port 49530 
c:\Users\peter\demo\import_pandas.py "

...

ImportError: Missing required dependencies ['numpy']

C:\Users\peter\demo>C:/Users/peter/AppData/Local/Anaconda3/Scripts/activate

(base) C:\Users\peter\demo>conda activate base

(base) C:\Users\peter\demo>

Note that conda gets automatically activated after the debugger stopped.

Once after the environment got activated, the debug process doesn't fail anymore:

(base) C:\Users\peter\demo>cd c:\Users\peter\demo && 
    cmd /C "set "PYTHONIOENCODING=UTF-8" && 
    set "PYTHONUNBUFFERED=1" && 
    C:\Users\peter\AppData\Local\Anaconda3\python.exe 
        c:/Users/peter/.vscode/extensions/ms-python.python-2019.5.18678/pythonFiles/ptvsd_launcher.py 
        --default --client --host localhost --port 49544 
c:\Users\peter\demo\import_pandas.py "

Pandas import succeeded!

Is there any way to have conda activated prior to calling the debugger for the first time?

like image 725
Peter Avatar asked Jun 06 '19 09:06

Peter


People also ask

How do you activate the base conda?

To activate your Conda environment, type source activate <yourenvironmentname> . Note that conda activate will not work on Discovery with this version. To install a specific package, type conda install -n <yourenvironmentname> [package] . To deactivate the current, active Conda environment, type conda deactivate .

How do you get the Anaconda prompt in VS code?

You can find the Anaconda Prompt by searching in the Windows menu. This will open a command line terminal like the Windows command prompt, except this terminal is configured to use all the Anaconda tools. Once the Anaconda Prompt is open, type “conda” and press enter.

What is the difference between conda activate and source activate?

Generally, you won't find too much of a difference between conda activate and the old source activate , except that it's meant to be faster, and work the same across different operating systems (the latter difference makes conda activate a huge improvement IMO).


4 Answers

It isn't that the debugger activates conda environments after the debugger stops on purpose, it's that conda activation is so slow it doesn't finish until after the debugger itself finishes (this isn't an issue with virtual environments as they don't need activation to behave appropriately). Basically we ask VS Code to launch a terminal with the activation commands and then launch the debugger, but the debugger is able to get going faster than conda activation, and so it doesn't execute until after the debugger.

Working with conda is just hard. You can follow our meta issue that is tracking all the problems we are trying to solve with the conda team.

like image 107
Brett Cannon Avatar answered Nov 15 '22 13:11

Brett Cannon


I've installed AutoLaunch extension and added the following launch configuration:

{ // Activate environment on startup with AutoLaunch extension
    "name": "activate",
    "type": "python",
    "request": "launch",
    "code": "",
    "auto": true,
},

So it runs empty Python script and then activated terminal stays open.


UPD. Now I just use PowerShell in VS Code and don't run into that problem:

  • Start Anaconda Powershell Prompt (conda)
  • conda init powershell it writes a script to %USERPROFILE%\Documents\WindowsPowerShell\profile.ps1 but double-check if you have non-latin symbols in your user name!
  • Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
  • conda config --set auto_activate_base false
like image 31
Winand Avatar answered Nov 15 '22 13:11

Winand


This seems to fit my problem with Python debugging. My python script works normally in the base conda environment. But as soon as I start the debugger using the Python extension, I immediately start seeing import DLL errors in the debug console, but the debugger does not stop on this error. It simply closes the debugger after 3 seconds or so without executing the script. I can even set breakpoints later in the script and it does not stop after this line. Restarting the debugger does the same, unlike that of the original description. I tried uninstalling then reinstalling the MS Python extension to no avail. Any clues you can offer would be great, I'm not a pro at vs code. Thanks.

Update: I got the debugger working properly. I created a Windows 10 Python virtual environment placed in my project workspace. Apparently this works around the conda env activation delay described below, the delay apparently is not the case with the Windows virtual environment.

like image 41
Bill Avatar answered Nov 15 '22 14:11

Bill


Had the same problem.

Sourcing from: https://github.com/formulahendry/vscode-code-runner/issues/395https://github.com/formulahendry/vscode-code-runner/issues/395

Put this into settings.json:

"code-runner.executorMap": {
    "python": "$pythonPath -u $fullFileName",
}, 
"code-runner.runInTerminal": true,

It does not put "conda activate ..." before the first execution, but it replaces code-runner command from:

"python -u <file path>"

to:

"<virtual environment path> -u <file path>"

This fixed my problem

like image 26
Blue Print Avatar answered Nov 15 '22 14:11

Blue Print