0

I need to check if a directory is already in the system environment variables PATH and add if it is not. I run cmd commands in python to add a directory to the PATH (may not be the best practice but im desperate). Here is the code:

import os
new_list = os.environ['PATH'].split(";")
try:
    search = new_list.index('C:\\Octave\\Octave-5.2.0.0\\mingw64\\bin2')
except ValueError:
    print('directory not found')
    command_cmd = 'setx PATH "%path%;C:\\Octave\\Octave-5.2.0.0\\mingw64\\bin"'
    os.system('cmd /c ' + command_cmd)

Running setx PATH "%path%;C:\\Octave\\Octave-5.2.0.0\\mingw64\\bindirectly to the cmd works but when implement it in python, PATH corrupts. Did i miss something? Any help will be greatly appreciated.

Klien Menard
  • 103
  • 1
  • 1
  • 10
  • 2
    `os.system` already executes via `cmd /c`, and `setx` is not an internal CMD command anyway. It's setx.exe, and how you're using it to modify `PATH` is wrong and corrupts the per-user "Path" value in the registry with the expanded and concatenated system + user `PATH` value. You need to use the winreg module to modify the user's unexpanded "Path" value in "HKCU\Environment". – Eryk Sun Apr 29 '20 at 07:21
  • @ErykSun Im confused as to why running ```setx PATH "%path%;C:\\Octave\\Octave-5.2.0.0\\mingw64\\bin``` directly in command prompt does the trick but doing it inside python truncates my path to 1024 characters – Klien Menard Apr 29 '20 at 07:37
  • **UPDATE: Tried using winreg to edit the PATH and it works. Thanks @ErykSun and it would be great if you post your comment as an answer – Klien Menard Apr 29 '20 at 07:48
  • SetX works differently for `path` to other variables. Just add the path you want. If its not in the path it will be added to the path else if it is in the path nothing will happen. –  Apr 29 '20 at 08:09
  • 2
    @Mark, setx.exe should never be used like this to modify `PATH`. It makes a mess to store the concatenated and expanded system+user value of `PATH` into either the system or user value. The way to use setx.exe to modify `PATH` is in combination with reg.exe, and in that case setx.exe is only used instead of just using reg.exe because it broadcasts the `WM_SETTINGCHANGE` message that causes Explorer to reload its environment. But in Python there's no need for that when we can more idomatically use winreg and broadcast the window message via ctypes or PyWin32. – Eryk Sun Apr 29 '20 at 08:39

1 Answers1

1

It is possible not only to read from os.environ['PATH'] but also assign to it. Please try following code:

import os
new_list = os.environ['PATH'].split(";")
new_path = 'C:\\Octave\\Octave-5.2.0.0\\mingw64\\bin2'
if new_path not in new_list:
    os.environ['PATH'] = os.environ['PATH'] + ';' + new_path
Daweo
  • 31,313
  • 3
  • 12
  • 25
  • I would like to make the change in PATH to be permanent hence made me to invoke cmd command solution and not this. – Klien Menard Apr 29 '20 at 07:12
  • @KlienMenard: in such case read this topic: https://stackoverflow.com/questions/488366/how-do-i-make-environment-variable-changes-stick-in-python – Daweo Apr 29 '20 at 07:24
  • What if `PATH` contains a (quoted) path that contains `;` on its own? – aschipfl Apr 29 '20 at 10:51