0

Is os.environ is same as changing environmental variables from settings in Windows?

Import os
os.environ[‘variable_name’] = [‘value’]

I tried above to change environment variable, I ran above code from pythonshell and checked my environment variable from Windows settings. I didn’t see that value.

Is this approach right?

martineau
  • 119,623
  • 25
  • 170
  • 301
bunty
  • 11
  • 3
  • Does this answer your question? [Is it possible to set an environment variable from Python permanently?](https://stackoverflow.com/questions/17657686/is-it-possible-to-set-an-environment-variable-from-python-permanently) – Random Davis Mar 10 '21 at 23:16
  • 3
    `os.environ` is, of course, very useful for getting values FROM the environment. Changing `os.environ` changes a variable during the execution of your process, and in any new processes that you create. Your environment is deleted when your program ends. – Tim Roberts Mar 10 '21 at 23:21
  • You can modify system environment variables on Windows by changing the registry through the `winreg` module (if your script can be run with administrative privileges). See [How to add to and remove from system's environment variable “PATH”?](https://stackoverflow.com/questions/21138014/how-to-add-to-and-remove-from-systems-environment-variable-path) for an example. – martineau Mar 11 '21 at 00:11
  • @RandomDavis: This question is not a duplicate of that linked one IMO. – martineau Mar 11 '21 at 00:18

0 Answers0