I'm working on a project which adds a certain directory to the Windows Path Environment variable. But I can't figure out how to add a directory to Path using Python
-
stackoverflow.com/questions/12257747/… This is a duplicateScottBot10– ScottBot102021-02-13 12:02:53 +00:00Commented Feb 13, 2021 at 12:02
-
How to set environment variables in Python?AcK– AcK2021-02-13 12:04:32 +00:00Commented Feb 13, 2021 at 12:04
-
I cant use them in Shell if I use stackoverflow.com/questions/12257747/… or stackoverflow.com/questions/5971312/…user14031682– user140316822021-02-13 12:33:34 +00:00Commented Feb 13, 2021 at 12:33
Add a comment
|
1 Answer
You will need to make sure to run this as an account with Admin privileges
import win32com.shell.shell as shell
def addPathToEnv(pathToAdd):
"""
Add the supplied path the Windows Path Environment variable
:param pathToAdd: <str> Full path to be added as Path Environment.
"""
commands = f'setx /M PATH "%PATH%;{pathToAdd}"'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)