off the stack

import sh

2014-06-02 | python

A small python library which seems to work rather well when it comes to shelling out is sh. It provides a handy and lightweight abstraction above the means already available within python.

The features I found most useful:

Take the small script on this page for example. At the top of the script is being imported with import sh. Then further down, the following lines test for command availability and make the commands available as python objects.

    git = sh.git
    ls =
    docker = sh.docker
    test = sh.test

In case the command in question does not exist, the library will throw an exception. This enables us to check the environment and bail out with a minimal amount of code.

Further down I can simply use this line

            digest = git('ls-remote', repo, 'refs/heads/master').split()[0]

to read out remote git repository information and work on the command's output right away with a small amount of code. Of course I could use the subprocess module too to achieve similar functionality but using sh tends to result in cleaner implementations and adds more flexibility.

It surely is worth a shot.