Skip to main content

Extern

Extern is an opinionated version of Python's subprocess, making it just that little bit more convenient to run shell commands from within Python code. Extern is Python-3 only, and is no longer compatible with Python-2, as of version 0.4.0.

It is reasonably straightforward:

>>> import extern
>>> extern.run("echo it works") #=> returns 'it works\n'
>>> extern.run("echo 1 2 5 |cat") #=> returns '1 2 5\n'

When a command that fails is run e.g.

>>> extern.run("cat /not_a_file")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-x86_64/egg/extern/__init__.py", line 29, in run
extern.ExternCalledProcessError: Command cat /not_a_file returned non-zero exit status 1.
STDERR was: cat: /not_a_file: No such file or directory
STDOUT was: 

an exception is raised just like subprocess. However, the error message generated includes STDERR and STDOUT, which is more convenient for debugging. For reference, the subprocess equivalent:

>>> subprocess.check_output(['bash','-c','cat /not_a_file'])
cat: /not_a_file: No such file or directory
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['bash', '-c', 'cat /not_a_file']' returned non-zero exit status 1

The useful thing is that Extern collects STDERR and only reports it when there is a non-zero exit status, discarding it otherwise.

IMPORTANT: use of this library with untrusted strings presents a security risk in the same way as little bobby tables, and shell=True in subprocess.

###Multiple commands run simultaneously

>>> extern.run_many(['echo once','echo twice','echo thrice'])
#=> ['once\n', 'twice\n', 'thrice\n']

Progress can also be observed:

>>> extern.run_many(['echo once','echo twice','echo thrice'], progress_stream=sys.stderr)
Finished processing 3 of 3 (100.00%) items.
#=> ['once\n', 'twice\n', 'thrice\n']

STDIN can be provided to run():

extern.run('cat',stdin='dog') #=> 'dog'

###Which There is also a which function, useful for determing where (and if) a program exists on the command line:

>>> import extern
>>> extern.which('cat') #=> '/bin/cat'
>>> extern.which('dog') #=> None

Installation

You can also install it directly from the Python Package Index with this command:

sudo pip install extern

Licence

See file LICENCE.txt in this folder

Contribute

Extern is an open-source software. Everyone is welcome to contribute !

Metadata

Release files for extern 0.4.1

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

Source distribution (sdist)

Source distribution for extern 0.4.1
File Size Uploaded
extern-0.4.1.tar.gz 6.3 kB Details

Release files / extern-0.4.1.tar.gz

Download URL extern-0.4.1.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0ff01adc2ad423f3d1e31641024b3974569fb0127b4d925bc6bed1cb86b6b1e4
BLAKE2b-256 checksum
How to use checksums
82115fb2720d54499141da8d50c9dd59ffffd89a9a735f9ff2fe1defbea0132b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.4.1 This release

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.4

1 release file

0.0.3

1 release file

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