I would like to get a full, descriptive error message from failed Python script executed with subprocess module.
I have a following script sub_script.py
which fails and produces IndexError: pop from empty list
error when executed on it's own:
# sub_script.py
empty_list = []
empty_list.pop()
I am calling sub_script.py
from sub_test.py
as follows:
# sub_test.py
import subprocess
import sys
print(str(subprocess.run([sys.executable, 'sub_script.py'],
check=True,
capture_output=True)))
However I am only getting subprocess.CalledProcessError
error.
Traceback (most recent call last):
File "/Users/my_user/Desktop/my_dir/sub_test.py", line 4, in <module>
print(str(subprocess.run([sys.executable, 'sub_script.py'],
File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 528, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/usr/local/opt/python@3.9/bin/python3.9', 'sub_script.py']' returned non-zero exit status 1.
I would like to see a full description of the error returned from sub_script.py
(IndexError: pop from empty list
) when it's executed with a subprocess in sub_test.py
.
Is it possible to get full error when script executes and fails within subprocess.run
?