0

I would like to set environment parameters using python in windows 11. Using commands like:

import os
os.environ['USER'] = 'username'
print(os.environ['USER'])

will print username. Nether the less, once I close the window. The set environment parameters dissipated. It seems I need something like SETX command in windows. I found the article how do I make environment variable changes stick in python. Nether the less, they use there the windows command.

Are there python commands as well to do the same? instead of sending subprocess or os.system commands.

Eagle
  • 3,362
  • 5
  • 34
  • 46

1 Answers1

0

You basically answered the question yourself with the link you provided.

Quoting from the documentation

This mapping is captured the first time the os module is imported

This is a bit of an interpretation from me, but that means os.environ is not a reference to the environment variable, but a copy of them in your Python interpreter,which seems to be validated a bit further in the doc':

Changes to the environment made after this time are not reflected in os.environ

While I do not really understand while you would want a program to modify your environment variables (a bit risky, no ?), you should probably go with the solution you provided in your question.

Itération 122442
  • 2,644
  • 2
  • 27
  • 73