HightronicDesign
HightronicDesign

Reputation: 709

Python change Windows Path (refresh Shell)

i have a Python script here and it is called from the Windows CMD. It is executing some commands and also changing the Windows environment variables. Now after i changed them with the command "setx". I have to restart another Shell so the new variables are loaded into it.

Is it possible that the main shell from which i called my script can update the variables itself ?

Or is it possible to start another shell with the new variables and the script will continue in the new opened shell ?

Thanks

Upvotes: 1

Views: 3278

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149125

Each process has its own environment. When a process starts another process, the new process gets a (eventually modified) copy of its parent environment.

The rule is :

  • a process can modify its own environment - this modifications will be inherited by child processes started later
  • a process can modify (at start time) the environment of its child processes
  • a process can never modify its parent's environment (*)

So when you start a Python script from a cmd.exe :

  • the script can change its own environment and those changes will be visible by all subsequent commands of the script and all its children
  • the script cannot change the environment for its parent cmd.exe nor for subsequent commands of that cmd.exe

If you need to execute other batch commands after changing the environment, you will have to start a new cmd.exe for the python script and have this new shell execute the other commands, or directly execute a .bat file (both via subprocessmodule).


setx is a completely different thing : it updates the default environmnent that is given to processes started from windows explorer (including cmd.exe). That environment is stored permanently in Windows registry, and every change to it is broadcasted to all active processes ... that monitors it. Any windows GUI application can process it (and explorer does - that's how every explorer window know immediatly what is the current default environment), but console applications normaly do not.


(*) well it used to be possible for .com executable in old MS/DOS system and was even documented. It should be possible on Windows recent system through WriteProcessMemory API call but is absolutely non documented (thanks to eryksun for noticing)

Upvotes: 2

loopbackbee
loopbackbee

Reputation: 23332

You can't change the value of a environment variable.

Allow me to clarify: environment variables represent the variables set on the environment of a process when that process starts.

From the point-of-view of the new process, its environment is unchanging. Changing a variable on the environment (the process' parent) will not change the value of the environment variable seen by the process. Changing a variable on the process will not make it's environment see the change.

So, what can you change?

  • Variables set on your process. This is achieved in python by changing os.environ, or using set on the shell. Any changes will be seen by your process and any children you make (os.system, subprocess, most commands on the shell).

  • Variables set by the system (what SetX does). These changes will be seen by any new process launched directly by the system (Explorer, in Windows) after you change them.

Upvotes: 1

Related Questions