记录IPython输出? [英] Log IPython output?

查看:414
本文介绍了记录IPython输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让IPython的日志记录功能包括输出和输入?

Is there any way to make IPython's logging capability include output as well as input?

这是当前日志文件的样子:

This is what a log file looks like currently:

#!/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 功能)

(the # is because I assume that is needed to prevent breaking IPython's logplay feature)

我想这可能是使用IPython笔记本电脑,但至少在一台我需要它的机器上,我只限于ipython 0.10.2。

I suppose this is possible using IPython notebooks, but on at least one machine I need this for, I'm limited to ipython 0.10.2.

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

I'd like to know how to set this up automatically, i.e. within the configuration file. Right now my config looks like

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 似乎没有影响

推荐答案

%logstart -o 选项:

-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.

ADDENDUM:如果您处于已启动日志记录的交互式ipython会话中,则必须先停止记录然后重新启动:

ADDENDUM: If you are in an interactive ipython session for which logging has already been started, you must first stop logging and then restart:

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

注意,重启后,输出记录现在为真。

Observe that, after the restart, "Output Logging" is now "True".

这篇关于记录IPython输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆