Skip to content

Calling External Binaries

It is often desirable to invoke external executables as part of a processing pipeline. This can be done using subprocess.run, then capture stdout/stderr for logging and troubleshooting. Note that input data for the external executable must be written to a file separately and the result must be parsed afterwards. When packaging into a Docker image, the binaries (and its dependencies) must be copied into the image. The LCModel-MRD-App example implements this functionality to integrate LCModel.

cmd = "some_external_program"
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
    logging.error(f"External program call failed with message:\n{result.stderr}")
    return None

Notes: - capture_output=True and text=True make it easy to persist logs as strings. - Handle failures explicitly by checking result.returncode and surfacing result.stderr.

Reference