I want to access the traceback of a python programm running in a subprocess.
The documentation says:
Exceptions raised in the child process, before the new program has started to execute, will be re-raised in the parent. Additionally, the exception object will have one extra attribute called child_traceback, which is a string containing traceback information from the child’s point of view.
Contents of my_sub_program.py
:
raise Exception("I am raised!")
Contents of my_main_program.py
:
import sys
import subprocess
try:
subprocess.check_output([sys.executable, "my_sub_program.py"])
except Exception as e:
print e.child_traceback
If I run my_main_program.py
, I get the following error:
Traceback (most recent call last):
File "my_main_program.py", line 6, in <module>
print e.child_traceback
AttributeError: 'CalledProcessError' object has no attribute 'child_traceback'
How can I access the traceback of the subprocess without modifying the subprocess program code? This means, I want to avoid adding a large try/except
clause around my whole sub-program code, but rather handle error logging from my main program.
Edit: sys.executable
should be replaceable with an interpreter differing from the one running the main program.
child_traceback
. Once the new program is running you need to catch theCalledProcessError
exception and do as the following: #24850498 usingCalledProcessError.output
– SalsifyCalledProcessError.output
did only capture the standard output, but not the traceback of the Exception. – Persephonestderr
. Look at answers from the question I sent you the link above for more details – Salsify