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

前端 未结 4 1829
陌清茗
陌清茗 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 00:41

    I struggled with this myself until I found this python bug report.

    "If you add a directory into PATH on Windows so that the directory is in quotes, subprocess does not find executables in it." Since the quotes aren't required by windows removing them fixes my problem (in 2.7).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-02 01:06

    Ok here is how I got it to work.

    env = os.environ
    proc = subprocess.Popen(args, env=env)
    
    0 讨论(0)
  • 2021-01-02 01:06

    A colleague of mine has reproduced this issue with Python 3.6.5 on Windows 10 64-bits.

    The installed version of Python was however 32-bits.

    Re-installing 64-bits version of Python fixed this issue.

    0 讨论(0)
提交回复
热议问题