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?
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.
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.