【发布时间】:2021-02-13 12:00:29
【问题描述】:
我正在开发一个将某个目录添加到 Windows 路径环境变量的项目。但我不知道如何使用 Python 将目录添加到 Path
【问题讨论】:
-
如果我使用stackoverflow.com/questions/12257747/… 或stackoverflow.com/questions/5971312/…,我无法在Shell 中使用它们
标签: python
我正在开发一个将某个目录添加到 Windows 路径环境变量的项目。但我不知道如何使用 Python 将目录添加到 Path
【问题讨论】:
标签: python
您需要确保以具有管理员权限的帐户运行它
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)
【讨论】: