Subprocess.call or Subprocess.Popen cannot use executables that are in PATH (Linux/Windows)

前端 未结 4 1844
陌清茗
陌清茗 2021-01-02 00:14

I\'m writing a programme that needs to run on both Linux and Windows and use executables (with parameters) that exist in the path. (Assumed)

Currently I\'m having tr

4条回答
  •  清歌不尽
    2021-01-02 01:04

    You can control the environment variables available in the spawned subprocess by passing a mapping with the env keyword argument. E.g.

    proc = subprocess.Popen(args, env={'PATH': '/some/path'})
    

    Or to inherit PATH from the system environment variable, without necessarily chucking in everything else from the system environment:

    proc = subprocess.Popen(args, env={'PATH': os.getenv('PATH')})
    

    It might be easier/simpler just to use an absolute path, though.

提交回复
热议问题