记录 IPython 输出?

14
有没有办法使IPython的日志功能包括输出和输入?
这是当前日志文件的样子:
#!/usr/bin/env python 
# 2012-08-06.py 
# IPython automatic logging file
# 12:02 
# =================================
print "test"

我想要再显示一行:

#!/usr/bin/env python 
# 2012-08-06.py 
# IPython automatic logging file
# 12:02 
# =================================
print "test"
# test

(我使用#是因为我认为需要防止破坏IPython的logplay功能)

我认为可以使用IPython笔记本来实现,但至少在一台我需要它的机器上,我受限于ipython 0.10.2。

编辑:我想知道如何自动设置这个,即在配置文件中。现在我的配置看起来像:

from time import strftime
import os
logfilename = strftime('ipython_log_%Y-%m-%d')+".py" 
logfilepath = "%s/%s" % (os.getcwd(),logfilename) 

file_handle = open(logfilepath,'a') 
file_handle.write('########################################################\n') 
out_str = '# Started Logging At: '+ strftime('%Y-%m-%d %H:%M:%S\n') 
file_handle.write(out_str) 
file_handle.write('########################################################\n') 
file_handle.close() 

c.TerminalInteractiveShell.logappend = logfilepath
c.TerminalInteractiveShell.logstart = True

但是指定c.TerminalInteractiveShell.log_output = True似乎没有任何影响。

1个回答

11

-o选项可用于%logstart

-o: log also IPython's output.  In this mode, all commands which
  generate an Out[NN] prompt are recorded to the logfile, right after
  their corresponding input line.  The output lines are always
  prepended with a '#[Out]# ' marker, so that the log remains valid
  Python code.

附加说明:如果你正在进行交互式的 IPython 会话,而日志已经启动了,那么你必须首先停止日志记录,然后重新启动:

In [1]: %logstop

In [2]: %logstart -o
Activating auto-logging. Current session state plus future input saved.
Filename       : ./ipython.py
Mode           : backup
Output logging : True
Raw input log  : False
Timestamping   : False
State          : active

请注意,在重启后,“输出日志记录”现在为“True”。


2
解决方案似乎是在启动脚本中进行此操作,如此处所述:http://wiki.ipython.org/Cookbook/DatedLog(这是在http://stackoverflow.com/questions/11836612/where/how/in/what/context/is-ipythons-configuration-file-executed?lq=1提供的解决方案)。 - keflavich
这非常方便,但在我的情况下,它只打印了已执行的代码,而不是实际的回溯信息。 - gies0r
这并没有回答问题,问题是如何让代码输出。 - David V.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接