foo 2>&1 means redirect all of the output, including handle 2 (that is, STDERR), from the foo command to handle 1 (that is, STDOUT)
so here out = !foo 2>&1 maybe good enough. below is the demo:
egg.py:
#!/usr/bin/env python
# -*- coding: utf8 -*-
def main():
print 'hello'
print 3/0
if __name__ == "__main__":
main()
IPython 0.10
In [5]: out = !egg.py
Traceback (most recent call last):
File "D:\python\note\egg.py", line 7, in <module>
main()
File "D:\python\note\egg.py", line 5, in main
print 3/0
ZeroDivisionError: integer division or modulo by zero
In [6]: out
Out[6]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: hello
In [7]: out = !egg.py 2>&1
In [8]: out
Out[8]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: hello
1: Traceback (most recent call last):
2: File "D:\python\note\egg.py", line 7, in <module>
3: main()
4: File "D:\python\note\egg.py", line 5, in main
5: print 3/0
6: ZeroDivisionError: integer division or modulo by zero
Hope this helps