Skip to main content

Python Shell Pipes

A simple way to access your shell commands in Python

The shpipes package uses subprocess.Popen to run commands in a shell easily from native Python.

    >>> from shpipes import Pipe
    # python --version
    >>> Pipe('python')('--version').getvalue()
    'Python 3.8.6\n'

Chaining pipes together

You can chain together your commands, passing output from one to the input of another, similar to shell pipes. You can use the bitwise inclusive or operator to chain Pipe instances together.

    # echo 1+1 | bc
    >>> (Pipe('echo')('1+1') | Pipe('bc')).getvalue()
    '2\n'
    # This also works
    >>> pipe = Pipe('echo')('1+1')
    >>> pipe |= Pipe('bc'))
    >>> pipe.getvalue()
    '2\n'

Loading commands from your PATH

Shell Pipes can also collect all executables from your PATH variable and gather them into a Commands instance so you can use the lib just like your native shell.

    >>> from shpipes import Commands
    >>> shell = Commands()
    # find . -type f | grep .py$ | wc -l
    >>> (shell.find('.', '-type', 'f') | shell.grep('.py$') | shell.wc('-l')).getvalue()
    '9\n'
    # this also works
    >>> pipe = shell.find('.', '-type', 'f')
    >>> pipe |= shell.grep('.py$')
    >>> pipe |= shell.wc('-l')
    >>> pipe.getvalue()
    '9\n'

Handling pipe arguments

When a Pipe is called, its arguments are directly passed to Popen so beware that strings must be quoted properly. You can use getvalue() to get the output from one pipe and then pass it as a command argument instead of input text. Pipes are only evaluated when getvalue() is called

    >>> license = shell.find('.', '-name', '"LICENSE"').getvalue()
    >>> shell.wc(license).getvalue()
    '  21  169 1069 ./LICENSE\n'

Shell by default

Since pipes run in a shell by default, environment variables evaluate automatically

    >>> pipe = shell.ps('-u $USER')
    >>> pipe |= shell.grep('python')
    >>> pipe |= shell.head('-1')

Configuration options

If you need to run a different shell or disable shell entirely, then you can pass options via environment variables or kwargs to Pipe

Overriding options in Pipe

You can override all options to Popen (except stdin/stdout) by passing arguments to Pipe By default Popen is run in shell mode with your default shell.

    >>> Pipe('ls', executable='/bin/zsh', cwd='/var', shell=False)

Set env SHPIPES_NO_SHELL=true

Sets shell=False in the call to Popen

Set env SHPIPES_SHELL=/bin/zsh

Changes the executable shell run by Popen

Metadata

Release files for shell-pipes 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shell-pipes 0.1.0
File Size Uploaded Provenance
shell-pipes-0.1.0.tar.gz 3.5 kB Without provenance Details

Release files / shell-pipes-0.1.0.tar.gz

Download URL shell-pipes-0.1.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
58af2c90074fc8ec76a95f026c0d59f570a6226c2a551e7c4fe1fceaf2ad1198
BLAKE2b-256 checksum
How to use checksums
2f3327e00c1fce243ee5ff623a4a15521b57e4d83952af668e8b434afc52979f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

Security for shell-pipes 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page