microsoft / vscode-python

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

virtualenv environment in remote scenario is not activated in terminal #23962

Closed kguryanov closed 2 weeks ago

kguryanov commented 1 month ago

Type: Bug

Behaviour

When using a non-multi-root workspace, virualenv environment is not being activated in the terminal

Environment:

  1. Python 12.4 installed via pyenv
  2. WLS2 standard Ubuntu (Default): 2.1. Ubuntu version:
    [11:41:28] /tmp/tmp.kQw8PyhiV3$ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 22.04.3 LTS
    Release:        22.04
    Codename:       jammy

Steps to reproduce:

  1. Create new directory and initialize virtualenv environment in it:
    [11:40:18] ~$ mktemp -d
    /tmp/tmp.kQw8PyhiV3
    [11:40:25] ~$ cd /tmp/tmp.kQw8PyhiV3
    [11:40:34] /tmp/tmp.kQw8PyhiV3$ virtualenv venv
    created virtual environment CPython3.12.4.final.0-64 in 86ms
    creator CPython3Posix(dest=/tmp/tmp.kQw8PyhiV3/venv, clear=False, no_vcs_ignore=False, global=False)
    seeder FromAppData(download=False, pip=bundle, via=copy, app_data_dir=/home/thenin/.local/share/virtualenv)
    added seed packages: pip==24.1.2
    activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
    [11:40:58] /tmp/tmp.kQw8PyhiV3$ . venv/bin/activate
    (venv) [11:41:12] /tmp/tmp.kQw8PyhiV3$ which python
    /tmp/tmp.kQw8PyhiV3/venv/bin/python
    (venv) [11:54:43] /tmp/tmp.kQw8PyhiV3$ touch test.py
  2. Start VSCode
  3. Connect to wsl
  4. Open the directory crated in step 1 (/tmp/tmp.kQw8PyhiV3 )
  5. Open Python file test.py (the virtual environment is correctly identified)
  6. Open the terminal

Actual result:

1, pyenv environment is actually activated in terminal though the hower over the terminal name states that the local virtualenv one is activated: image 2, Reloading workspace does not change the behavior

  1. Current config: image

Expected result:

  1. virualenv environment to be activated: image

Notes:

  1. it seem to work fine with multi-root workspaces:
    [12:20:23] /tmp/tmp.kQw8PyhiV3$ mkdir test
    [12:20:26] /tmp/tmp.kQw8PyhiV3$ code --add test
  2. after workspace is reopened, kill the terminal terminal and open it with CTRL+`
  3. Detected virtual environment is activated in terminal: image

Notes 2:

1, The command from logs seem to activate manually image

Diagnostic data

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

``` 2024-08-17 12:02:53.369 [info] Experiment 'pythonCreateEnvOnPipInstall' is active 2024-08-17 12:02:53.369 [info] Experiment 'pythonRecommendTensorboardExt' is active 2024-08-17 12:02:53.369 [info] Experiment 'pythonSurveyNotification' is active 2024-08-17 12:02:53.369 [info] Experiment 'pythonTerminalEnvVarActivation' is active 2024-08-17 12:02:53.369 [info] Experiment 'pythonTestAdapter' is active 2024-08-17 12:02:53.369 [error] Reading directory to watch failed [Error: ENOENT: no such file or directory, scandir '/tmp/tmp.kQw8PyhiV3/.pixi/envs' at Object.readdirSync (node:fs:1509:26) at /home/thenin/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/out/client/extension.js:2:586747 at /home/thenin/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/out/client/extension.js:2:586938 at Array.map () at d.initWatchers (/home/thenin/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/out/client/extension.js:2:586656) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async d.ensureWatchersReady (/home/thenin/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/out/client/extension.js:2:543067)] { errno: -2, code: 'ENOENT', syscall: 'scandir', path: '/tmp/tmp.kQw8PyhiV3/.pixi/envs' } 2024-08-17 12:02:53.369 [error] Dir "/tmp/tmp.kQw8PyhiV3/.pixi/envs" is not watchable (directory does not exist) 2024-08-17 12:02:53.369 [info] Default formatter is set to ms-python.autopep8 for workspace /tmp/tmp.kQw8PyhiV3 2024-08-17 12:02:53.369 [info] Python interpreter path: ./venv/bin/python 2024-08-17 12:02:53.376 [info] > ~/.pyenv/bin/pyenv which python 2024-08-17 12:02:53.376 [info] cwd: . 2024-08-17 12:02:53.422 [info] > conda info --json 2024-08-17 12:02:53.452 [info] > pixi --version 2024-08-17 12:02:53.466 [error] [Error: spawn pixi ENOENT at ChildProcess._handle.onexit (node:internal/child_process:286:19) at onErrorNT (node:internal/child_process:484:16) at process.processTicksAndRejections (node:internal/process/task_queues:82:21)] { errno: -2, code: 'ENOENT', syscall: 'spawn pixi', path: 'pixi', spawnargs: [ '--version' ] } 2024-08-17 12:02:53.466 [warning] could not find a pixi interpreter for the interpreter at /tmp/tmp.kQw8PyhiV3/venv/bin/python 2024-08-17 12:02:53.533 [info] > . ./venv/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/python_files/printEnvVariables.py 2024-08-17 12:02:53.533 [info] shell: bash 2024-08-17 12:02:53.559 [info] > ~/.pyenv/versions/3.12.4/bin/python ~/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/python_files/printEnvVariables.py 2024-08-17 12:02:53.559 [info] shell: bash 2024-08-17 12:02:53.582 [info] Setting environment variable VIRTUAL_ENV in collection to /tmp/tmp.kQw8PyhiV3/venv {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-08-17 12:02:53.582 [info] Setting environment variable VIRTUAL_ENV_PROMPT in collection to venv {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-08-17 12:02:53.582 [info] Prepending environment variable PS1 in collection with venv {"applyAtShellIntegration":true,"applyAtProcessCreation":false} 2024-08-17 12:02:53.582 [info] Prepending environment variable PATH in collection with /home/thenin/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/python_files/deactivate/bash:/tmp/tmp.kQw8PyhiV3/venv/bin: {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-08-17 12:02:53.583 [info] Send text to terminal: /home/thenin/.pyenv/versions/3.12.4/bin/python /home/thenin/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/python_files/printEnvVariablesToFile.py /home/thenin/.vscode-server/extensions/ms-python.python-2024.12.3-linux-x64/python_files/deactivate/bash/envVars.txt 2024-08-17 12:02:54.416 [info] Starting Pylance language server. ```

Extension version: 2024.12.3 VS Code version: Code 1.92.2 (fee1edb8d6d72a0ddff41e5f71a671c23ed924b9, 2024-08-14T17:29:30.058Z) OS version: Windows_NT x64 10.0.22631 Modes: Remote OS version: Linux x64 5.15.153.1-microsoft-standard-WSL2 Remote OS version: Linux x64 5.15.153.1-microsoft-standard-WSL2

User Settings

``` languageServer: "Pylance" testing • pytestArgs: "" • pytestEnabled: true terminal • executeInFileDir: "" ```

Installed Extensions |Extension Name|Extension Id|Version| |---|---|---| |autoDocstring - Python Docstring Generator|njpwerner.autodocstring|0.6.1| |autopep8|ms-python.autopep8|2024.0.0| |Black Formatter|ms-python.black-formatter|2024.2.0| |C/C++|ms-vscode.cpptools|1.21.6| |djLint|monosans.djlint|2024.7.0| |Docker|ms-azuretools.vscode-docker|1.29.2| |Even Better TOML|tamasfe.even-better-toml|0.19.2| |Flake8|ms-python.flake8|2023.10.0| |GitHub Actions|github.vscode-github-actions|0.26.3| |Ini for VSCode|DavidWang.ini-for-vscode|0.0.4| |JavaScript Debugger|ms-vscode.js-debug|1.92.0| |Jupyter|ms-toolsai.jupyter|2024.7.0| |Jupyter Cell Tags|ms-toolsai.vscode-jupyter-cell-tags|0.1.9| |Jupyter Keymap|ms-toolsai.jupyter-keymap|1.1.2| |Jupyter Notebook Renderers|ms-toolsai.jupyter-renderers|1.0.19| |Jupyter Slide Show|ms-toolsai.vscode-jupyter-slideshow|0.1.6| |Live Server|ritwickdey.LiveServer|5.7.9| |Pylance|ms-python.vscode-pylance|2024.8.1| |Python|ms-python.python|2024.12.3| |Python Debugger|ms-python.debugpy|2024.10.0| |python-tox|the-compiler.python-tox|1.0.0| |Rainbow CSV|mechatroner.rainbow-csv|3.12.0| |SQLTools|mtxr.sqltools|0.28.3| |SQLTools SQLite|mtxr.sqltools-driver-sqlite|0.5.1| |Table Visualizer for JavaScript Profiles|ms-vscode.vscode-js-profile-table|1.0.9|
System Info |Item|Value| |---|---| |CPUs|AMD Ryzen 9 5900HX with Radeon Graphics (16 x 3294)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off| |Load (avg)|undefined| |Memory (System)|31.42GB (13.61GB free)| |Process Argv|--crash-reporter-id fe598ff3-5a6b-4d3a-808c-6401870925c9| |Screen Reader|no| |VM|0%| |Item|Value| |---|---| |Remote|WSL: Ubuntu| |OS|Linux x64 5.15.153.1-microsoft-standard-WSL2| |CPUs|AMD Ryzen 9 5900HX with Radeon Graphics (16 x 0)| |Memory (System)|15.33GB (6.59GB free)| |VM|0%| |Item|Value| |---|---| |Remote|WSL: Ubuntu| |OS|Linux x64 5.15.153.1-microsoft-standard-WSL2| |CPUs|AMD Ryzen 9 5900HX with Radeon Graphics (16 x 0)| |Memory (System)|15.33GB (6.58GB free)| |VM|0%|
A/B Experiments ``` vsliv368:30146709 vspor879:30202332 vspor708:30202333 vspor363:30204092 vswsl492:30256859 vscod805cf:30301675 binariesv615:30325510 vsaa593cf:30376535 py29gd2263:31024239 vscaac:30438847 c4g48928:30535728 azure-dev_surveyone:30548225 a9j8j154:30646983 962ge761:30959799 pythongtdpath:30769146 welcomedialog:30910333 pythonnoceb:30805159 asynctok:30898717 pythonregdiag2:30936856 pythonmypyd1:30879173 2e7ec940:31000449 pythontbext0:30879054 accentitlementst:30995554 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 cppperfnew:31000557 dsvsc020:30976470 pythonait:31006305 dsvsc021:30996838 01bff139:31013167 pythoncenvpt:31062603 a69g1124:31058053 dvdeprecation:31068756 dwnewjupytercf:31046870 2f103344:31071589 impr_priority:31102340 nativerepl1:31104043 refactort:31108082 pythonrstrctxt:31112756 flightc:31119335 wkspc-onlycs-t:31111718 wkspc-ranged-t:31118572 ```
anthonykim1 commented 4 weeks ago

Hello @kguryanov Thanks for filing the issue. Are you on a multi-root workspace?

kguryanov commented 3 weeks ago

Hello @kguryanov Thanks for filing the issue. Are you on a multi-root workspace?

Hi @anthonykim1

Original issue is reported for regular workspace (aka open folder)

Making it a multi-root is a workaround for the original issue. Only after adding another folder to workspace, virtual environment activation starts working in the terminal

anthonykim1 commented 3 weeks ago

@kguryanov Got it, that is pretty interesting..

Are you able to add:

 "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],

in your settings.json and see if you can repro the same problem? It seems that you are only seeing the "activation command" when additional workspace folder is added.

kguryanov commented 3 weeks ago

Hi @anthonykim1

Are you able to add:

 "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],

That actually worked.

  1. Add "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"], to settings.json, save changes
  2. Close current terminal
  3. Reload window (Command Palette -> Developer: Reload Window)
  4. Open the terminal (CTRL+`)

After VSCode window reload, the venv started activating in terminal image

There is, however, a little side effect:

  1. Add "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"], to settings.json, save changes
  2. Reload window (Command Palette -> Developer: Reload Window) without closing the current termnial
    • VSCode window is reloaded and terminal is automatically launched (restored) with venv activated
    • VSCode prompts to relaunch terminal because Python extension requires it
  3. On prompt from VSCode, click the "Relaunch Terinal" link: image

After terminal relaunch, venv fails to activate once again, same as in original report.

However, closing the terminal and opening it again, activates venv in terminal successfully once again.

Is there a reproducible way to trigger terminal env update and cause VSCode to prompt to relaunch the terminal? I suspect this little glitch can happen anytime VSCode detects that Relaunch Terminal is required and user clicks on the link in the prompt.

I believe some extensions may trigger that prompt upon installation, but I am not sure.

anthonykim1 commented 2 weeks ago

@kguryanov Glad that worked out - You might want to try out the "Python Clear Cache and Reload" command from the command palette instead of just "Reload Window" Or exit out of all the terminal instances and create new one after you have added "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],"

Make sure you add that setting line to both your user and workspace settings so that you don't have to remember to add the setting everytime. Once the Python extension detects that you have added that setting, we wont try to add environment variables in the terminal for you. - Thanks!