0

For some reason I have to write Python for Windows (extremely tedious), and can't find a way to append the script's current directory to the PATH environment variable.

I know about

import os
os.environ["PATH"] += os.pathsep + <current_directory>

but this doesn't do it for me since the change is not permanent.

All the answers point to changing the PATH by using the GUI (System > Advanced System Settings > Environment Variables). I want to automate my tool as much as possible so the user only has to run the config.py file and start working, without worrying too much about path variables.

On Linux I just wrote to .bashrc and sourced the file. How can I do this in Windows?

Gabe N.
  • 1
  • 1
  • @ecatmur The answer for the question you linked gives vbscript solution. This question asks explicitly for a Python solution and therefore is not a duplicate. – ElmoVanKielmo Sep 19 '16 at 11:53
  • Hey! I think that works for me, I was just looking for some pointers. I'll try modifying the registry using Python, see if I can work something out. – Gabe N. Sep 19 '16 at 11:58
  • I can't post the answer since the question is on hold due to being marked as a duplicate (incorrectly). Still you can edit Windows registry with https://docs.python.org/2/library/_winreg.html module. Update `"HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path"`. Remember to append your path to the existing value. Don't replace the whole thing. – ElmoVanKielmo Sep 19 '16 at 11:58
  • @ElmoVanKielmo Thank you! I got it now. – Gabe N. Sep 19 '16 at 12:06
  • @ElmoVanKielmo the question is language-agnostic, so it's irrelevant that the accepted answer is in VBScript. – ecatmur Sep 19 '16 at 12:35
  • Don't follow the recommendation to use setx.exe. That would make a mess. The user `Path` needs to be edited separate from the system path and retain the original unexpanded environment variables. You should preferably modify the user's value in `HKCU\Environment` rather than the system value, unless you really need to modify it for all users. Also, keep the original data type (`REG_SZ` vs `REG_EXPAND_SZ`), except if the value has more than one "%" in it you should use `REG_EXPAND_SZ`. And remember to broadcast a `WM_SETTINGCHANGE` "Environment" message to not require logging off and on. – Eryk Sun Sep 19 '16 at 12:45
  • @ecatmur How is `Permanently adding to %PATH% from Python` language agnostic? – ElmoVanKielmo Sep 19 '16 at 12:53
  • @ElmoVanKielmo the linked question is language-agnostic, and an answer in VBScript can easily be translated into Python, so there's no need to have separate questions per language. – ecatmur Sep 19 '16 at 13:15

0 Answers0