extending PATH on Windows?

Thorsten Kampe thorsten at thorstenkampe.de
Tue Feb 16 07:19:32 EST 2016


* Ulli Horlacher (Tue, 16 Feb 2016 08:30:59 +0000 (UTC))
> I need to extend the PATH environment variable on Windows.
> 
> So far, I use:
> 
>    system('setx PATH "%PATH%;'+bindir+'"')
> 
> The problem: In a new process (cmd.exe) PATH contains a lot of double
> elements. As far as I have understood, Windows builds the PATH
> environment variable from a system component and a user component. With
> the setx command from above I have copied the system PATH into the user
> PATH component.
> 
> Is there a better way to extend the PATH environment variable for the user?
> It must be persistent, not only for the current process.

`os.system` should be `subprocess.call` on modern systems (actually 
`subprocess.run` if you have Python 3.5). Since `setx` doesn't seem 
to have unique and add options, you basically have two options:

1. Add the path component yourself into HKEY_CURRENT_USER and make 
sure it's not there already (pure Python).

2. a) use a shell that offers that capability with `set`: 
https://jpsoft.com/help/set.htm (TCC/LE is free)

   b) use a dedicated environment variable editor: 
http://www.rapidee.com/en/command-line

Thorsten



More information about the Python-list mailing list