I'm trying to set a Windows environment variable using Python.
It seems that, contrary to the docs, os.environ can get environment variables but cannot set them. Try running these in a Windows Command Prompt:
This works:
python -c "import os; print(os.environ['PATH'])"
This does not:
python -c "import os; os.environ['FOO'] = 'BAR'"
Try typing set in the Command Prompt. The environment variable FOO does not exist.
How can I set a permanent Windows environment variable from Python?
os.environis creating environment variables, checking from your shell is the wrong tool. Use Process Explorer to inspect the state of your Python process, and you'll see that these truly are created, even though they don't outlive the process that creates them. - Charles Duffyos.environdict can get out of sync with the actual process environment, e.g. a DLL function could call_putenv. - Eryk Sun