-4

I know how to manually add a variable with the control panel. There doesn't seem to be any shortage of information about that. But what I am interested in is how to add a system variable to the list (not just appending the 'PATH' variable) by using a python program. So far what I've gathered is using os.environ is my best bet.... somehow. It seems that it's good for seeing what's already there and not much else.

So, in short what I want to do is add 'SYS_VAR' with the path C:\suspiious_folder to the system variable list.

Edit : I do want to permanently add to the list of system variables.

Korbaine
  • 49
  • 4

1 Answers1

4

You can set an environment variable using os.environ as you mentioned simply with:

os.environ['SYS_VAR'] = <the path you want as a string>

or, in your case

os.environ['SYS_VAR'] = 'C:/suspicious_folder'

To have your environment variable, SYS_VAR, persist across sessions and be added permanently, you may want to check out: Is it possible to set an environment variable from Python permanently? and How do I make environment variable changes stick in Python?.

Edit: As @Torxed mentioned, here's another SO question that may help explain how modifying Windows environment variables from Python works: Interface for modifying Windows environment variables from Python

Community
  • 1
  • 1
Michael Recachinas
  • 2,739
  • 2
  • 20
  • 29
  • 4
    Beware, I think the OP is asking how to add it permanently. But then again, what do I know.. The OP was extremely vague in his way of questioning and also forgot to include any piece of code he or she has already tried. – Torxed Jan 15 '16 at 17:14
  • 1
    I want to add to your answer that this SO question might be of use as well for understanding how it works: http://stackoverflow.com/questions/1085852/interface-for-modifying-windows-environment-variables-from-python – Torxed Jan 15 '16 at 17:17