Differences between subprocess module, envoy, sarge and pexpect?

As the maintainer of sarge, I can tell you that its goals are broadly similar to envoy (in terms of ease of use over subprocess) and there is (IMO) more functionality in sarge with respect to:

  • Cross-platform support for bash-like syntax (e.g.use of &&, ||, & in command lines)
  • Better support for capturing subprocess output streams and working with them asynchronously
  • More documentation, especially about the internals and peripheral issues like threading+forking in the context of using subprocess
  • Support for prevention of shell injection attacks

Of course YMMV, but you can check out the docs, they're reasonably comprehensive.


pexpect

In 2015, pexpect does not work on windows. Rumored to add "experimental" support in the next version, but this has been a rumor for a long time (I'm not holding my breath).

Having written many applications using pexpect (and loving it), I am now sorry because one of the things I love about python (that it is cross platform) is not true for my applications.

If you plan to ever add windows support, for the moment, avoid pexpect.

envoy

Not much activity in the last year. And few commits (12 total) since 2012. Not very promising for its future.

Internally it uses shlex in a way that is not compatible with windows paths (the commands must use '/' not '\' for directory separators). A workaround (when using pathlib) is to call as_posix() on path objects before passing them as commands. See this answer.

Getting access to the internal streams (i.e. I want to parse the output to have some updating scrollbars), seems possible but is not documented.

sarge

Works on windows out-of-the-box and has an expect() method that should provide functionality similar to pexpect (allowing me to update a scrollbar). Recent activity, but it is hosted on gitlab and bitbucket (very confusing).

Personal Conclusion

I'm moving from pexpect to sarge for future development. Seems to provide similar feature set to pexpect and supports windows.


subprocess - is a standard library module, so it'll be available with python installation. But it has a reputation of hard to use since it's api is non-intuitive.

envoy - is a third party module that wraps around subprocess. It was written to be an easy to use alternative to subprocess. The author of envoy Kenneth Reitz is famous for his Python for Humans philosophy.

I'm not familiar with the other two.