-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.

1
  • 1
    If you can see it, why can't you modify it? What exactly have you tried? Commented Jan 15, 2016 at 17:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
I want to add to your answer that this SO question might be of use as well for understanding how it works: stackoverflow.com/questions/1085852/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.