windowsvisual-studio-codesshvscode-remotevscode-remote-ssh

How to make VS Code recognize the environment variables of the SSH remote Windows host?


I have two machines:

I'd like to ssh the guest by VS Code's SSH Remote extension and debug the pythonWebApp project remotely.

Current behavior is that the SSH connection succeeds, but environment variables differ - for example, PATH in the guest machine has the Python paths.

The following is the Windows Terminal outputs in the guest VM:

PS C:\Users\kusha> $env:PATH
C:\Users\kusha\AppData\Local\Programs\Python\Python313\Scripts\;C:\Users\kusha\AppData\Local\Programs\Python\Python313\;C:\Users\kusha\AppData\Local\Programs\Python\Launcher\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\kusha\AppData\Local\Programs\Python\Launcher\;C:\Users\kusha\AppData\Local\Microsoft\WindowsApps;

PS: I added these Python paths to system variable PATH manually.

PATH in ssh session lacks these Python paths.

The following is the Terminal Window outputs in VS Code:

PS C:\Users\kusha\pl-python\pythonWebApp> $env:PATH
c:\Users\kusha\.vscode-server\cli\servers\Stable-cb0c47c0cfaad0757385834bd89d410c78a856c0\server\bin\remote-cli;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\kusha\AppData\Local\Microsoft\WindowsApps;;c:\Users\kusha\.vscode-server\extensions\ms-python.debugpy-2025.10.0-win32-x64\bundled\scripts\noConfigScripts;c:\Users\kusha\.vscode-server\data\User\globalStorage\github.copilot-chat\debugCommand

Apparently, due to the lack of Python environment variables in the ssh session, VS Code cannot perform any Python related operation.

PS: using a pure ssh session (i.e. not through VS Code) has a similar problem.

Question: how can I make the ssh session use the same environment variables as the guest? Alternatively, is there a way to automatically set environment variables when ssh connect?

Thanks very much.


Solution

  • A partial answer to my own question:

    This Hyper-V guest VM had two accounts - a Microsoft account (kusha) and a local account (Chansey). A strange observation was that even though the local account was used by default to log into the desktop, only the Microsoft account's username appeared under C:\Users (i.e., C:\Users\kusha).

    I created a brand-new VM with Windows 11 IoT LTSC and ensured it didn’t use a Microsoft account (meaning the system only had a local account). This resolved the issue.

    So, it’s reasonable to suspect it was a Windows account-related problem. However, tonight I rebooted the old guest VM and found the issue no longer existed in the old guest VM.

    Thus, the root cause still remains unclear (perhaps a reboot was required after installing OpenSSH Server).