I\'d like to have real-time access to both interpreter input and error and standard output. Preferably this information would be written to a file, so that I can poll the fi
You can try and use my logging tool. It's not perfect yet but it solved my problem, which seems similar to your.
https://github.com/hholst80/loginteractive
It works by using LD_PRELOAD to pipe stdin.txt (or $STDIN) to stdout. It works for python and octave, although I have not tested it that much yet.
You can simply use the unix script command, try:
script -a filename.txt
python
>> print("hi")
hi
>> exit()
exit
filename.txt will record everything you did on that session, it will look something like this:
Script started on Sat Dec 14 11:18:41 2013
python
>> print('hi')
hi
>> exit()
exit
Script done on Sat Dec 14 11:18:59 2013
See this Virtualenv article by Doug Hellmann, showing how to log an iPython session:
If you are comfortable working at the interactive prompt in this way, but want to record what you do for future reference after you close your session, you can use IPython’s logging feature to write the session to a file. To activate the log, use the control command
%logstart
, as illustrated in Listing 5. The output file is a Python source file, so it is easy to clean it up and turn it into a “real” module when you are done experimenting.
In [6]: %logstart
Activating auto-logging. Current session state plus future input saved.
Filename : ipython_log.py
Mode : rotate
Output logging : False
Raw input log : False
Timestamping : False
State : active
In [7]: a = 5
In [8]: b = 6
In [9]: c = a * b
In [10]: c
Out[10]: 30
In [11]: d = [ a, b, c]
In [12]: d
Out[12]: [5, 6, 30]
In [13]: %logstop
take a look at IPython (haven't used it myself). Here's a section in the docs that might be of particular interest: http://ipython.org/ipython-doc/dev/interactive/reference.html#session-logging-and-restoring
I've only tested this in python2.7. I don't have 3 handy.
import code
import sys
class Tee(object):
def __init__(self, log_fname, mode='a'):
self.log = open(log_fname, mode)
def __del__(self):
# Restore sin, so, se
sys.stdout = sys.__stdout__
sys.stdir = sys.__stdin__
sys.stderr = sys.__stderr__
self.log.close()
def write(self, data):
self.log.write(data)
sys.__stdout__.write(data)
def readline(self):
s = sys.__stdin__.readline()
self.log.write(s)
return s
# Tie the ins and outs to Tee.
sys.stdout = sys.stderr = sys.stdin = Tee('consolelog.dat', 'w')
console = code.InteractiveConsole()
console.interact()