0

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

Yashraj Narke
  • 26
  • 2
  • 4
  • https://stackoverflow.com/questions/12257747/permanently-adding-a-file-path-to-sys-path-in-python This is a duplicate – ScottBot10 Feb 13 '21 at 12:02
  • [How to set environment variables in Python?](https://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python) – AcK Feb 13 '21 at 12:04
  • I cant use them in Shell if I use https://stackoverflow.com/questions/12257747/permanently-adding-a-file-path-to-sys-path-in-python or https://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python – Yashraj Narke Feb 13 '21 at 12:33

1 Answers1

1

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)
jadki
  • 482
  • 1
  • 8
  • 15