Home > OS >  How to set windows environment variable from python
How to set windows environment variable from python

Time:02-25

I am writing a python program that needs to store a value in a persistent user environment variable. I believe these values are stored in the registry. I have tried something like this using the winreg python module.

key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Environment', access=winreg.KEY_WRITE) winreg.SetValueEx(key, envVarName, 0, winreg.REG_SZ, envVarValue) winreg.Close(key)

I can see from using Registry Editor that this value is successfully set. But if I try and read the value from another python script or from a new powershell instance, I don't see the new value until the machine is rebooted.

What else do I need to do to make this value available to other processes without rebooting?

CodePudding user response:

Looking at this answer gives a possible answer: enter image description here

Now I use the getRegistry and open a PS in admin mode to run it:

print(getRegistry(regdir, keyname))

enter image description here

  • Related