Skip to content

getEnvironment() switches to a newly created venv without firing onDidChangeEnvironment #2046

Open

Description

@AndreasReichel

Environment

  • VS Code 1.146, Windows 11
  • ms-python.vscode-python-envs 1.44.0
  • ms-python.python 2026.10.0
  • ms-python.pylint 2026.8.0 (as an affected consumer)
  • Reproduced with python.useEnvironmentsExtension unset (default) and with true

Steps to reproduce

  1. Open a folder that contains only main.py (import six), with no Python settings. Python Environments selects the
    global Python for it (source: autoDiscovery) and fires onDidChangeEnvironment.
  2. In a terminal, create a venv in the folder: python -m venv .venv.
  3. Wait.

Expected

When getEnvironment(folderUri) starts returning .venv, onDidChangeEnvironment fires for the folder, so consumers
that cache the interpreter (Pylint, other tool extensions) can switch to it.

Actual

getEnvironment(folderUri) returns .venv about 15 s after the venv was created, but no event fires, and the log has no
"Changed environment" line for it. Consumers that listen to the event keep the old interpreter. For example, Pylint 2026.8.0
keeps running with the global Python until "Pylint: Restart Server", which then picks up .venv.

Timeline from a small probe extension (polls getEnvironment() every second, logs every onDidChangeEnvironment),
merged with the Python Environments and Pylint logs; python.useEnvironmentsExtension unset:

15:48:18.974 getEnvironment(ws) = C:\Python314\python.exe
15:48:18.984 EVENT onDidChangeEnvironment <folder>: undefined -> C:\Python314\python.exe
15:48:18.986 [Python Environments] [interpreterSelection] ws: Python 3.14 (64-bit) (source: autoDiscovery)
15:48:19.005 [Pylint] Server run command: C:\Python314\python.exe ...
15:48:31.227 (python -m venv .venv in <folder>)
15:48:47.136 [Python Environments] Discovered env: <folder>\.venv\Scripts\python.exe
15:48:47.757 getEnvironment(ws) = <folder>\.venv\Scripts\python.exe      <- no event, no "Changed environment" log line
15:49:50.005 (Pylint: Restart Server)
15:49:52.060 [Pylint] Server run command: <folder>\.venv\Scripts\python.exe ...

Same with "python.useEnvironmentsExtension": true:

15:50:39.531 getEnvironment(ws) = C:\Python314\python.exe
15:50:39.540 EVENT onDidChangeEnvironment <folder>: undefined -> C:\Python314\python.exe
15:50:39.567 [Pylint] Server run command: C:\Python314\python.exe ...
15:51:05.370 (python -m venv .venv in <folder>)
15:51:18.920 getEnvironment(ws) = <folder>\.venv\Scripts\python.exe      <- no event
15:52:23.140 [Pylint] Server run command: <folder>\.venv\Scripts\python.exe ...   (after Pylint: Restart Server)

Probe used:

const api = await vscode.extensions.getExtension('ms-python.vscode-python-envs').activate();
api.onDidChangeEnvironment((e) => log(`EVENT ${e.uri?.fsPath ?? 'global'}: ${exe(e.old)} -> ${exe(e.new)}`));
setInterval(async () => {
  for (const f of vscode.workspace.workspaceFolders) log(`getEnvironment(${f.name}) = ${exe(await api.getEnvironment(f.uri))}`); // logged on change only
}, 1000);

Impact

Any extension that follows the documented pattern (read getEnvironment() once, then react to
onDidChangeEnvironment) ends up with a different interpreter than Python Environments reports. With Pylint this means
false import-errors for packages installed in the new venv, until the user restarts the Pylint server or reloads the
window. Nothing in the UI hints at that.

Our extension creates a venv in the folder and then restarts Pylint. As a workaround, it calls
refreshEnvironments(folderUri) first, so that Pylint reads the new interpreter when it restarts. That only helps when
the venv is created by us, not when the user creates one.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions