Skip to content

A keyboardInterrupt happent when venv going start #25720

Closed

Description

@HECARIIA

Type: Bug

Description
When running Python code that requires blocking input (e.g., input()) for the first time in a new shell, VS Code triggers an unexpected KeyboardInterrupt.
This issue only occurs inside a Python virtual environment (venv).
Running the same code a second time (and any subsequent times) works normally.

This behavior does not occur when using the system Python interpreter.
I am using the default "python" debug configuration provided by VS Code.

Steps to Reproduce

  1. Open VS Code.

  2. Create or activate a Python venv.

  3. Run a simple blocking script, for example:

input("Enter something: ")

  1. Observe that VS Code immediately raises a KeyboardInterrupt on the first run.

  2. Run the same script again in the same shell.

  3. The script works normally without interruption.

Expected Behavior
The script should wait for user input normally on the first run, just like it does on subsequent runs.

Actual Behavior

Image

On the first execution in a new shell:

The program is interrupted automatically.

A KeyboardInterrupt is raised without any user action.

On second and later executions:

The script behaves normally and waits for input.

Additional Notes
The issue only appears when using a virtual environment.

The issue does not occur with the system Python interpreter.

I am using the default "python" launch configuration generated by VS Code.

ScreenShot_2026-01-09_04-46-44.mp4

Environment:

VS Code version: 1.108.0

Python extension version: 2025.21.2026010601

Operating System: Windows 11 25H2 26200.7462

Python version: python 3.14.2

Virtual environment type: venv

Shell used: PowerShell

Extension version: 2025.21.2026010601
VS Code version: Code 1.108.0 (94e8ae2b28cb5cc932b86e1070569c4463565c37, 2026-01-08T13:53:10.781Z)
OS version: Windows_NT x64 10.0.26200
Modes:

  • Python version (& distribution if applicable, e.g. Anaconda): 3.14.2
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): System
  • Value of the python.languageServer setting: Default
User Settings


languageServer: "Pylance"

Installed Extensions
Extension Name Extension Id Version
autodocstring njp 0.6.1
copilot-chat Git 0.35.3
debugpy ms- 2025.19.2025121701
even-better-toml tam 0.21.2
js-debug ms- 1.105.0
js-debug-companion ms- 1.1.3
jupyter ms- 2025.10.2026010601
jupyter-keymap ms- 1.1.2
jupyter-renderers ms- 1.3.2025062701
python ms- 2025.21.2026010601
remote-containers ms- 0.438.0
remote-explorer ms- 0.5.0
remote-server ms- 1.5.3
remote-ssh ms- 0.122.0
remote-ssh-edit ms- 0.87.0
remote-wsl ms- 0.104.3
ruff cha 2026.34.0
vscode-docker ms- 2.0.0
vscode-js-profile-table ms- 1.0.10
vscode-jupyter-cell-tags ms- 0.1.9
vscode-jupyter-slideshow ms- 0.1.6
vscode-language-pack-zh-hans MS- 1.108.2026010709
vscode-pylance ms- 2025.10.100
vscode-python-envs ms- 1.15.13521015
vscode-remote-extensionpack ms- 0.26.0
System Info
Item Value
CPUs 13th Gen Intel(R) Core(TM) i7-13650HX (20 x 2803)
GPU Status 2d_canvas: enabled
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
trees_in_viz: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) undefined
Memory (System) 31.63GB (18.07GB free)
Process Argv --crash-reporter-id 57e8a3cf-b24b-44a7-a2da-975ff851ba3d
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
binariesv615:30325510
nativeloc1:31344060
dwcopilot:31170013
dwoutputs:31242946
copilot_t_ci:31333650
e5gg6876:31282496
pythonrdcb7:31342333
6518g693:31436602
aj953862:31281341
6abeh943:31336334
envsactivate1:31353494
cloudbuttont:31379625
3efgi100_wstrepl:31403338
use-responses-api:31390855
je187915:31407605
ec5jj548:31422691
terminalsuggestenabled:31431119
cmp-ext-treat:31426748
cp_cls_c_966_ss:31442353
c0683394:31419495
478ah919:31426797
ge8j1254_inline_auto_hint_haiku:31427726
a5gib710:31434435
38bie571_auto:31426784
no_request_with_suggest:31435826
rename_enabled:31436409
7a04d226_do_not_restore_last_panel_session:31438103
anthropic_thinking_t:31432745
h0hdh950:31428394
cp_cls_c_1081:31443829
copilot-nes-callisto-trt:31434412
nowdiget:31442216
nes-slash-models-on:31441832
nes-set-to-on:31441941
a43f0576c:31442824

Activity

  1. mlcfr commented on Jan 11, 2026

    @mlcfr

    It's not a bug. You are trying to run Python code from an environment that you haven't activated first.

  2. HECARIIA commented on Jan 12, 2026

    @HECARIIA
    Author

    It's not a bug. You are trying to run Python code from an environment that you haven't activated first.

    I'm find this when I using matplotlib. If the environment haven't activate, it should report error immediately, but it doesn't.
    So I think environment isn't the reason. Besides, not run in the right environment should not report "KeyboardInterrupt", which should be reported when I press "CTRL + C", but not when run the code.

  3. HECARIIA commented on Jan 12, 2026

    @HECARIIA
    Author

    It's not a bug. You are trying to run Python code from an environment that you haven't activated first.

    Thank for you help, I have understand what happend when I use "Run by Python".
    When I click this button, it open a new terminal and run the code, but terminal run "& .\Activate.ps1" when the python code is running, and this will creat a KeyboardInterrupt to stop python.
    I still think this should be a bug.

  4. changed the title [-]Unexpected Execution of KeyboardInterrupt when First Run A Code that Can Block Task[/-] [+]A keyboardInterrupt happent when venv going start[/+] on Jan 12, 2026
  5. sagar-aps-MaMaTa commented on Jan 14, 2026

    @sagar-aps-MaMaTa

    This happened after the last update. Running any debug command in any 'new' terminal first goes to KeyboardInterrupt.

    Before the update, this worked quite smoothly. The debug commands do 'try' to activate the environment but then an Interrupt is immediately sent.

    Here is an example of the command that stops at first run but works at second.

    The issue is worse when youtry to run TWO debugs at the same time. The Keyboard Interrupt kills the FIRST debug terminal.

    & 'C:\Users\apshankar\Documents\Python_Virtual_envs\Cognite_DataScience_py312\Scripts\python.exe' 'c:\Users\apshankar\.vscode\extensions\ms-python.debugpy-2025.18.0-win32-x64\bundled\libs\debugpy\launcher' '64571' '--' '-m' 'motor_current_rise.cli' 'run' '--ptt' 'MotorCurrentDrift' '--current-key' 'PT6.I.''input_timeseries''' '--label-key' 'PT9.I.''target_time_series''' '--granularity' '1h' '--aggregation' 'average' '--label-granularity' '1h' '--label-aggregation' 'max' '--shift-hours' '6' '--tuner' 'optuna' '--tune-trials' '130' '--log-level' 'INFO' '--log-file' 'C:\Users\apshankar\Documents\Sagar/Lab_AlgoTestBed/motor_current_rise/motor_current_rise/data/outputs/run_all.log' '--output-xlsx' 'C:\Users\apshankar\Documents\Sagar/Lab_AlgoTestBed/motor_current_rise/motor_current_rise/data/outputs/all_assets_optuna.xlsx'

  6. HECARIIA commented on Jan 16, 2026

    @HECARIIA
    Author

    Look this, VSCode try to starting venv when code is running:
    Image
    And it need 4 seconds to start:

    Image
  7. HECARIIA commented on Jan 19, 2026

    @HECARIIA
    Author

    It looks like this bug is fixed now. It still happens when you use Code Runner, but that's a different issue.

  8. rfernand2 commented on Feb 22, 2026

    @rfernand2

    As of Feb-22-2026, I am still seeing this bug, which I think has been around since Dec-2025. It only happens every 1 in about 10 runs. Here a stack trace:
    INFO: Uvicorn running on http://localhost:5000 (Press CTRL+C to quit)
    INFO: Started reloader process [16316] using StatReload
    INFO: DEBUG=True, TESTER_LOGIN=True
    INFO: Started server process [28324]
    INFO: Waiting for application startup.
    INFO: Application startup complete.
    ERROR: Traceback (most recent call last):
    File "D:\Users\rfern\AppData\Local\anaconda3\envs\sessions\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "D:\Users\rfern\AppData\Local\anaconda3\envs\sessions\Lib\asyncio\base_events.py", line 654, in run_until_complete
    return future.result()
    ^^^^^^^^^^^^^^^
    asyncio.exceptions.CancelledError

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "D:\Users\rfern\AppData\Local\anaconda3\envs\sessions\Lib\site-packages\uvicorn_compat.py", line 30, in asyncio_run
    return runner.run(main)
    ^^^^^^^^^^^^^^^^
    File "D:\Users\rfern\AppData\Local\anaconda3\envs\sessions\Lib\asyncio\runners.py", line 123, in run
    raise KeyboardInterrupt()
    KeyboardInterrupt

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "D:\Users\rfern\AppData\Local\anaconda3\envs\sessions\Lib\site-packages\starlette\routing.py", line 701, in lifespan
    await receive()
    File "D:\Users\rfern\AppData\Local\anaconda3\envs\sessions\Lib\site-packages\uvicorn\lifespan\on.py", line 137, in receive
    return await self.receive_queue.get()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "D:\Users\rfern\AppData\Local\anaconda3\envs\sessions\Lib\asyncio\queues.py", line 158, in get
    await getter
    asyncio.exceptions.CancelledError

    INFO: Stopping reloader process [16316]
    PS D:\github\sessions4>

  9. loleczkowo commented on Feb 26, 2026

    @loleczkowo

    I also experinced this issue and i found that the problem for me was setting both Python environment and interpreter to a venv.
    Setting the Python enviroment to the actuall python fixed it for me.

  10. yhlam commented on Mar 4, 2026

    @yhlam

    Got the same problem. Setting python.useEnvironmentsExtension to true solves my problem. I think somehow both the python environment and interpreter settings attempted to activate the venv. The second activation interrupted the running script.

  11. Erlonealpha commented on Mar 6, 2026

    @Erlonealpha

    Got the same problem. Setting python.useEnvironmentsExtension 到 true solves my problem. I think somehow both the python environment and interpreter settings attempted to activate the venv. The second activation interrupted the running script.

    Thanks, It works for me.

  12. optinsoft commented on Apr 3, 2026

    @optinsoft

    I have the same problem. With conda.

  13. anthonykim1 commented on Apr 3, 2026

    @anthonykim1

    Try install https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-python-envs , and download the latest-pre-release.

    Please set python-envs.terminal.autoActivationType to shellStartup in your settings.
    Also add "python.useEnvironmentsExtension": true, to your settings.json.

    Please file issue on https://github.com/microsoft/vscode-python-environments/issues if you see problem again, thanks.

  14. optinsoft commented on Apr 4, 2026

    @optinsoft

    Please set python-envs.terminal.autoActivationType to shellStartup in your settings. Also add "python.useEnvironmentsExtension": true, to your settings.json.

    That works! Thank you!

  15. kyrlon commented on Apr 21, 2026

    @kyrlon

    Try install https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-python-envs , and download the latest-pre-release.

    Please set python-envs.terminal.autoActivationType to shellStartup in your settings. Also add "python.useEnvironmentsExtension": true, to your settings.json.

    Please file issue on https://github.com/microsoft/vscode-python-environments/issues if you see problem again, thanks.

    Also note with the second setting, you need to have it not in your workspace settings:

    This setting can only be applied in user settings in local window or in remote settings in remote window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

info-neededIssue requires more information from postertriage-neededNeeds assignment to the proper sub-team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions