Python Forum

Full Version: os.environ not setting current shell variables in Windows?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
Having some trouble with a project. Using Python 2.7 and 3.5 (trying to keep things compatible with both for simplicity). Using 3.5 for most of my first-pass testing/dev.

I'm trying to set an environment variable in the user's profile on Windows. Currently running Win 10, but have seen the same behavior on 8.1.

I can set the environment variable via registry key (winreg.SetValueEx) and send the system broadcast needed to have future processes pick up the change. But what I'm trying to do now is set the environment for the current process so I can spawn a child that will inherit the new setting.
os.environ["FOO"] = value
doesn't seem to be setting the current process' environment properly, running "set" after the script completes doesn't show the variable set in the current process. I can't seem to find much beyond "it should work". Is there another, more effective, way to set the environment variables for the current process? My reading of putenv() is that it wouldn't work to set the value here, is that correct?

Any help is appreciated, and thank you.
Brian
Thanks, I'll look through those responses again to see if I missed anything, I suspect I'm going to have to resort to spawning multiple cmd processes to try and force the inheritance, as the straight use of os.environ is not setting the current environment.