代码之家  ›  专栏  ›  技术社区  ›  Andrew

在Windows中通过python添加到路径

  •  0
  • Andrew  · 技术社区  · 8 年前

    我要将geckoDriver exe添加到 PATH 环境变量通过python脚本,我在stackoverflow上找到了一个答案,但这对我不起作用。我的代码说路径已添加,但当我签入时 cmd 或者通过系统设置,它不存在:

    cwd = os.getcwd()+"\\driver\\"
    
    if 'geckodriver' not in os.environ:
        print("Added")
        os.environ["Path"] = cwd
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Andrej Kesely    8 年前

    setx

    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)
    
        2
  •  1
  •   Alex Taylor    8 年前