microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.33k stars 1.19k forks source link

venv isolation broken when debugging #22919

Open sil-lnagel opened 8 months ago

sil-lnagel commented 8 months ago

Type: Bug

Behaviour

Expected vs. Actual

When I activate a virtual environment I expect python to use packages from this virtual environment over globally installed packages.

If run the debugger with the vscode python extension on Ubuntu sys.path contains /usr/lib/python3/dist-packages in front of the venv packages. This causes global packages to be loaded.

If you add export PYTHONPATH="" to .zshrc this is not added to sys.path during debug. But if you modify the PYTHONPATH eg to add a src folder inside your launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Current File with Arguments",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "env": {
                "PYTHONPATH": "src"
            }
        }
    ]
}

it still shows up in sys.path

Steps to reproduce:

  1. Create venv
  2. Select venv in vscode
  3. Create launch.json file as above
  4. debug a python file and print sys.path
  5. Verify that /usr/lib/python3/dist-packages is in path before venv folders

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

``` XXX ```

User Settings

``` languageServer: "Pylance" terminal • activateEnvironment: false ```

Extension version: 2024.0.1 VS Code version: Code 1.86.2 (903b1e9d8990623e3d7da1df3d33db3e42d80eda, 2024-02-13T19:41:37.860Z) OS version: Linux x64 6.2.0-39-generic Modes:

A/B Experiments ``` vsliv368cf:30146710 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscoreces:30445986 vscod805cf:30301675 binariesv615:30325510 vsaa593cf:30376535 py29gd2263:30899288 c4g48928:30535728 azure-dev_surveyone:30548225 vscrpc:30673769 962ge761:30959799 pythongtdpath:30769146 welcomedialog:30910333 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pyreplss1:30897532 pythonmypyd1:30879173 pythoncet0:30885854 pythontbext0:30879054 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 edj9j583:30958191 fegfb526:30961430 7j2b6412:30964151 ccp1r6:30958156 ```
irm-codebase commented 8 months ago

I am having a similar issue with conda environments (https://github.com/microsoft/vscode-python/issues/22918). It seems like something is botched in the Python plugin.

irm-codebase commented 8 months ago

This is particularly annoying as I cannot debug properly unless I install the packages in the base environment.