<div dir="ltr"><div><div><div><div><div>Hi, <br></div><div>I am looking at a new strategy to capture stderr and exceptions in python code blocks. Right now exceptions are not captured in the output, and neither is stderr.<br> <br>I made a little sandbox module that captures stdout, stderr, and exceptions and then prints them all to stdout with some minor formatting. Here is an example.<br><br></div><div>Say test.py has this content<br><br>#+BEGIN_SRC python <br> print 'hello'<br><br>print 4 + 6<br><br>import sys<br><br>print >>sys.stderr, 'message to stderr'<br><br><br>raise Exception('baboom')<br>#+END_SRC<br><br></div><div>When I use the sandbox, I get all the output on stdout like this.<br> </div><div><br></div><div>#+BEGIN_SRC sh<br>python -m sandbox < test.py<br># or cat test.py | python -m sandbox <br>#+END_SRC<br><br>#+RESULTS:<br>#+begin_example<br><br>--------------------------------------------------------------<br> hello<br>10<br><br><br>--------------------------------------------------------------<br>stderr:<br>message to stderr<br><br><br>--------------------------------------------------------------<br>Traceback (most recent call last):<br> File "/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py", line 16, in <module><br> exec(content, ns_globals, ns_locals)<br> File "<string>", line 10, in <module><br>Exception: baboom<br> <br><br>#+end_example<br></div><br><br></div>So, I was wondering how to get this to happen in org-mode on a regular python block. I think it could work if I could define a custom interpreter for a particular block, e.g. python-sandbox that takes the codeblock on stdin.<br> </div></div><br></div>Is there some other way that I could do this? Thanks!<br><div><div><br clear="all"><div><div><div><div><div>John<br><br>-----------------------------------<br>John Kitchin<br>Associate Professor<br>Doherty Hall A207F<br> Department of Chemical Engineering<br>Carnegie Mellon University<br>Pittsburgh, PA 15213<br>412-268-7803<br><a href="http://kitchingroup.cheme.cmu.edu" target="_blank">http://kitchingroup.cheme.cmu.edu</a><br><br></div> </div></div></div></div></div></div></div>