set environmental variable from python
davea at davea.name
Fri Oct 31 13:34:08 CET 2014
On 10/30/2014 10:40 PM, Artur Bercik wrote:
> On Fri, Oct 31, 2014 at 11:30 AM, Zachary Ware <
> zachary.ware+pylist at gmail.com> wrote:
>> On Thursday, October 30, 2014, Artur Bercik <vbubbly21 at gmail.com> wrote:
>>> Dear Dave Angel
>>> Thanks for your answer.
>>> I am using Python 2.7
>>> I want to set it permanently.
>>> I have to set several variables so it would be easier if I could set them
>>> from Python.
>> Depending on how "permanently" you mean, about your only solutions would
>> be "os.system('setx <...>')" or manually manipulating the registry with the
>> _winreg module.
> could you please elaborate 'setx <...>'?
SETX is a Windows utility. I don't run Windows, but I found a Win7
machine and played again. I used to be quite knowledgeable about
Windows, but I could get parts of the following wrong.
To see what SETX does, type (at a cmd line):
Or google it. (I used duckduckgo, and here are the first two useful
links: Obviously the second one is an official Microsoft page.
There are several variants of "permanent" that are relevant here.
1) Nothing you change in one process will change another process that's
already running. That includes any shells you might have already
started, such as DOS boxes. (cmd windows) And the term shell here also
includes any other already-running process that is going to launch
another program. The shell controls its child's environment, and in
general do not check the registry for changes you might have made.
2) SETX will affect new processes launched directly from Windows (eg.
from Explorer), because it changes the registry itself.
3) SETX will affect *new* shells launched, and thus indirectly affect
4) SETX effects will survive a reboot. So when you restart your system,
you'll still see the new values. Incidentally, that's the first time
when you'll be sure that all processes will see the new values.
5) SETX can be used on remote (networked) systems, and the rules are
somewhat different there.
6) If you have (or ever will have) multiple users, you have to consider
whether you want to affect the system environment variables or just one
user. And if just one user, whether it's yourself or some other
account, such as Administrator. Every time a process is launched by the
system, it builds an environment from TWO sets of registry items. So if
you want to affect all users, you need to change the system variables.
If you are just going to do this once, and you want to affect all the
processes that ever run in the future, you'll be much safer using the
Control Panel | System | Advanced | Environment Variables
More information about the Python-list