Reputation: 1575
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
Upvotes: 0
Views: 78
Reputation: 195438
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)
Upvotes: 1
Reputation: 8813
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.
Upvotes: 1