【问题标题】:How Can I add any directory to PATH using Python?如何使用 Python 将任何目录添加到 PATH?
【发布时间】:2021-02-13 12:00:29
【问题描述】:

我正在开发一个将某个目录添加到 Windows 路径环境变量的项目。但我不知道如何使用 Python 将目录添加到 Path

【问题讨论】:

标签: python


【解决方案1】:

您需要确保以具有管理员权限的帐户运行它

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)

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 1970-01-01
    • 2020-01-21
    • 2012-02-13
    • 2021-10-13
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多