microsoft / vscode-python-debugger

Python debugger (debugpy) extension for VS Code.
https://marketplace.visualstudio.com/items?itemName=ms-python.debugpy
MIT License
41 stars 17 forks source link

Python debugger does not activate conda environment #291

Open jordanca42 opened 3 months ago

jordanca42 commented 3 months ago

Type: Bug

I am using VS Code with the Python interpreter set to an interpreter in an anaconda environment. VS Code detects that this is an anaconda environment and correctly activates the environment when opening a terminal window, running a python file, etc.

When debugging a python file, a terminal window is opened to run the commands to start debugging. In this debugging terminal window, the anaconda environment is NOT activated. This causes failures when debugging. If I run conda activate ... in the debugging terminal to activate my environment, I can then debug successfully. However, the environment should be activated automatically.

Here is an example of what I see when opening a new non-debugging terminal:

(my_env) PS C:\repos\my_project>

Here is an example of what I see when trying to debug:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

Loading personal and system profiles took 2159ms.
(base) PS C:\repos\my_project>  & 'c:\Users\xxx\Anaconda3\envs\my_env\python.exe' 'c:\Users\xxx\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '28255' '--' 'C:\repos\my_project\test_programs\test.py'
c:\Users\xxx\Anaconda3\envs\my_env\lib\site-packages\numpy\__init__.py:127: 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
Traceback (most recent call last):
  File "c:\Users\xxx\Anaconda3\envs\my_env\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\Users\xxx\Anaconda3\envs\my_env\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\xxx\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\xxx\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\xxx\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\xxx\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\xxx\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\xxx\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\repos\my_project\test_programs\test.py", line 1, in <module>
        import pandas as pd
  File "c:\Users\xxx\Anaconda3\envs\my_env\lib\site-packages\pandas\__init__.py", line 16, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.

If I run conda activate my_env and debug again, it works.

(base) PS C:\repos\my_project> conda activate my_env
(my_env) PS C:\repos\my_project>  c:; cd 'c:\repos\my_project'; & 'c:\Users\xxx\Anaconda3\envs\my_env\python.exe' 'c:\Users\xxx\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '28289' '--' 'C:\repos\my_project\test_programs\test.py' 
<successful program output>

Extension version: 2024.2.0 VS Code version: Code 1.87.2 (863d2581ecda6849923a2118d93a088b0745d9d6, 2024-03-08T15:20:17.278Z) OS version: Windows_NT x64 10.0.19045 Modes:

System Info |Item|Value| |---|---| |CPUs|AMD EPYC 7542 32-Core Processor (4 x 2895)| |GPU Status|2d_canvas: unavailable_software
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: disabled_software
multiple_raster_threads: enabled_on
opengl: disabled_off
rasterization: disabled_software
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: disabled_software
video_encode: disabled_software
vulkan: disabled_off
webgl: unavailable_software
webgl2: unavailable_software
webgpu: unavailable_software| |Load (avg)|undefined| |Memory (System)|16.00GB (2.42GB free)| |Process Argv|--crash-reporter-id 5d80ae5c-1369-41ef-ad5f-42c215c9dfe5| |Screen Reader|no| |VM|100%|
A/B Experiments ``` vsliv368cf:30146710 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscorecescf:30445987 vscod805cf:30301675 binariesv615:30325510 vsaa593:30376534 py29gd2263:30899288 c4g48928:30535728 azure-dev_surveyone:30548225 962ge761:30959799 pythongtdpath:30769146 welcomedialog:30910333 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pyreplss1:30897532 pythonmypyd1:30879173 pythoncet0:30885854 2e7ec940:30992800 pythontbext0:30879054 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 bf62j303:30959262 pydisww2:30991134 cppperfcontrol:30979541 d34g3935:30971562 fegfb526:30981948 bg6jg535:30979843 dsvsc020:30976470 pythonaic:30992952 ```
karthiknadig commented 3 months ago

@jordanca42 We are working on a more generic solution to activation. For now, start VS Code from an activated environment.

Here is a workaround for this:

  1. Turn off auto activation. To do this, disable these two settings, ideally only for the project you are working on: image
  2. Exit VS Code.
  3. Open Anaconda Prompt, navigate to your project.
  4. Activate the environment that you want.
  5. Run code . or code project.code-workspace to launch VS Code.

This will give you an activated environment, and the extension will not have to try and activate every time a terminal is created.

cgr71ii commented 1 month ago

Can the workaround be extended to remote explorers?

Rafael-Silva-Oliveira commented 3 weeks ago

Can the workaround be extended to remote explorers?

I have the same question. This doesn't work for remote explorers