0

I want to add GeckoDriver exe to PATH environment variable via a python script, I found an answer on StackOverflow but this didn't work for me. My code says the path was added, but when I check in cmd or through the system settings, it isn't there:

cwd = os.getcwd()+"\\driver\\"

if 'geckodriver' not in os.environ:
    print("Added")
    os.environ["Path"] = cwd
Alex Taylor
  • 7,080
  • 4
  • 23
  • 37
Andrew
  • 945
  • 10
  • 24

2 Answers2

1

The presented code will update the environment variable for the current shell and its children. It won't propagate up to setting the system-level settings.

The worse news, is that this can't be done easily. You either have to edit the registry, or you may be able to use setx command line tool. Neither are particularly friendly from Python, and both have downsides.

Alex Taylor
  • 7,080
  • 4
  • 23
  • 37
1

You need to run setx command to set permanent environmental variable under Windows. To run setx from Python, try this code:

from subprocess import check_output

cwd = os.getcwd()+"\\driver\\"

if 'geckodriver' not in os.environ:
    print("Added")
    check_output(f'setx Path "{cwd}"', shell=True)
Andrej Kesely
  • 81,807
  • 10
  • 31
  • 56