Set environment variable in current shell from child process #23014

Closed
opened 2026-01-31 08:29:56 +00:00 by claunia · 5 comments
Owner

Originally created by @peakschris on GitHub (Mar 11, 2025).

Description of the new feature

We launch a python CLI from windows terminal. As a result of certain CLI actions, we want the windows terminal to set certain environment variables as soon as the python CLI command finishes. I understand that this is impossible when launching python from CMD. However, maybe there is some trick to allow it when launching python from Windows Terminal.

We have discarded the option of wrapping the python CLI in a bat or cmd as then we end up with the dreaded 'Terminate (Y/N)' prompt on Ctrl-C.

My hope is that Windows Terminal has some kind of scripting support such that the python subprocess can add an envvar in its parent shell via some kind of API call.

Any other ideas very welcome!

Proposed technical implementation details

No response

Originally created by @peakschris on GitHub (Mar 11, 2025). ### Description of the new feature We launch a python CLI from windows terminal. As a result of certain CLI actions, we want the windows terminal to set certain environment variables as soon as the python CLI command finishes. I understand that this is impossible when launching python from CMD. However, maybe there is some trick to allow it when launching python from Windows Terminal. We have discarded the option of wrapping the python CLI in a bat or cmd as then we end up with the dreaded 'Terminate (Y/N)' prompt on Ctrl-C. My hope is that Windows Terminal has some kind of scripting support such that the python subprocess can add an envvar in its parent shell via some kind of API call. Any other ideas very welcome! ### Proposed technical implementation details _No response_
Author
Owner

@PankajBhojwani commented on GitHub (Mar 12, 2025):

Thank you for the request! Unfortunately Terminal does not have any special control over the environments of running processes.

Just curious - why do you want the environment variables in CMD while running Python /?

@PankajBhojwani commented on GitHub (Mar 12, 2025): Thank you for the request! Unfortunately Terminal does not have any special control over the environments of running processes. Just curious - why do you want the environment variables in CMD while running Python /?
Author
Owner

@peakschris commented on GitHub (Mar 12, 2025):

So to be clear, I don't want terminal to change the environment of a running process. I want the running process to change the terminal's environment so that it will apply to any future commands that are invoked.

@peakschris commented on GitHub (Mar 12, 2025): So to be clear, I don't want terminal to change the environment of a running process. I want the running process to change the terminal's environment so that it will apply to any future commands that are invoked.
Author
Owner

@peakschris commented on GitHub (Mar 12, 2025):

Why? We have a python CLI that has multiple options. One of those is 'install devtools'. This creates a new bin directory in a new location. We want these tools to be available immediately after this CLI invocation is finished, without the developer needing to restart terminal. So we'd like the python CLI to be able to update the PATH envvar that the terminal will use for future commands.

@peakschris commented on GitHub (Mar 12, 2025): Why? We have a python CLI that has multiple options. One of those is 'install devtools'. This creates a new bin directory in a new location. We want these tools to be available immediately after this CLI invocation is finished, without the developer needing to restart terminal. So we'd like the python CLI to be able to update the PATH envvar that the terminal will use for future commands.
Author
Owner

@DHowett commented on GitHub (Mar 12, 2025):

Ah, sorry about that.

Terminal automatically reloads the environment from the registry for every new pane and every new tab! That is, unless the user has disabled that feature by setting compatibility.reloadEnvironmentVariables to false (or toggled this setting in the Advanced settings page:

Image

)

Even so, when I read...

the terminal will use for future commands

... I still can't help but think you want the currently running shell (pwsh.exe, cmd.exe, powershell.exe) to reload.

At the end of the day, it is still the shell which is running your commands. Any process that is already running when you update the environment--including the shell--is immune to those changes.

That's what we mean when we say "running process". 🙂


To put it differently:

I understand that this is impossible when launching python from CMD. However, maybe there is some trick to allow it when launching python from Windows Terminal.

There is no dichotomy here: CMD is still running, it's just inside Terminal.

@DHowett commented on GitHub (Mar 12, 2025): Ah, sorry about that. Terminal automatically reloads the environment from the registry for every new pane and every new tab! That is, unless the user has disabled that feature by setting `compatibility.reloadEnvironmentVariables` to `false` (or toggled this setting in the Advanced settings page: ![Image](https://github.com/user-attachments/assets/83510508-a1a6-4b02-ac51-c1bc8035245d) ) Even so, when I read... > the terminal will use for future commands ... I still can't help but think you want the currently running shell (pwsh.exe, cmd.exe, powershell.exe) to reload. At the end of the day, it is still the shell which is running your commands. Any process that is *already running* when you update the environment--including the shell--is immune to those changes. That's what we mean when we say "running process". 🙂 --- To put it differently: > I understand that this is impossible when launching python from CMD. However, maybe there is some trick to allow it when launching python from Windows Terminal. There is no dichotomy here: CMD is still running, it's just inside Terminal.
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Mar 24, 2025):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Mar 24, 2025): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**. <!-- Policy app identification https://img.shields.io/static/v1?label=PullRequestIssueManagement. -->
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#23014