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.

5
  • 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". Commented Apr 29, 2020 at 7: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 Commented Apr 29, 2020 at 7: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 Commented Apr 29, 2020 at 7: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. Commented Apr 29, 2020 at 8: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. Commented Apr 29, 2020 at 8:39

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

I would like to make the change in PATH to be permanent hence made me to invoke cmd command solution and not this.
@KlienMenard: in such case read this topic: stackoverflow.com/questions/488366/…
What if PATH contains a (quoted) path that contains ; on its own?

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.