【问题标题】:Log IPython output?记录 IPython 输出?
【发布时间】:2012-08-06 18:14: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 似乎没有影响

【问题讨论】:

    标签: ipython


    【解决方案1】:

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

    附录:如果你在一个交互式 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
    

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多