Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

A shell-like DSL front for subprocess.Popen

Project Description


What is it?

The popen package provides a simple shell-like syntax inside python for running external commands.


  • Chaining (aka. piping) as Sh('ls') | 'sort'
  • Redirect stderr to stdout as Sh('make').include_stderr | 'wc'
  • Redirect output to file as Sh('ls') > '~/listing.txt'
  • Iteration over output lines as for line in Sh('ls'):
  • Streaming input into commands in the chain as Sh.pipe('~/listing.txt').Sh('grep', '-q', 'code').returncode
  • Expands special characters (~*!?)
  • Expands env vars ($HOME) as print Sh('ls', '$USER', '~/*')
  • Properly splits strings ('echo "hole in one"' becomes ['echo', 'hole in one'])
  • Or iterable arguments (Sh('ls', '-l') | ['wc', '-l'] > '/dev/null')



pip install popen


from popen import Sh
for line in Sh('du', '~/') | 'head -n 10' | 'sort -n':
    print('GOT', line)


Simples usage, run a command and iterate the output lines:

from popen import Sh
for line in Sh('ls', '-la', '~'):
    print line

Piping that output to a file:

Sh('ls', '-la', '~') > '~/listing'

Note that special characters like ~ are expanded, as are environment variables (e.g. $HOME).

Chaining commands is trivial, here with append instead of write to file:

Sh('ls') | Sh('sort') >> '~/listing'

But the right hand side of | can be very flexible, as it employs lexical splitter on string input or takes an iterable:

Sh('ls', '-la', '~') | 'sort -c' | ['uniq', '-c'] | 'tail' | Sh('wc') > '~/listing'

To run a command and let output go to stdout, ask for the return code:

cmd = Sh('ls') | 'grep polka'
print cmd.returncode

To pipe a string into a command, wrap in an iterable:

print Sh.pipe(['this is a \nmulti line\nstring.']) | 'wc'

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(7.0 kB) Copy SHA256 Hash SHA256
Source None Jan 29, 2015

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting