I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input into the shell (db connections, variable assignments, little for loops and bits of logic) -- some history of the interactive session? If I use something like script I get too much stdout noise. I don't really need to pickle all the objects -- though if there is a solution that does that, it would be OK. Ideally I would just be left with a script that ran as the one I created interactively, and I could just delete the bits I didn't need. Is there a package that does this, or a DIY approach?


当前回答

除了IPython之外,类似的实用程序bpython还有一个“将您输入的代码保存到文件中”的特性

其他回答

%history命令非常棒,但不幸的是,它不能让您将%paste 'd保存到sesh中。要做到这一点,我认为你必须在开头执行%logstart(尽管我还没有确认这是有效的)。

我喜欢做的是

%history -o -n -p -f filename.txt

它将在每个输入(o, n和p选项)之前保存输出,行号和'>>>'。请在这里查看文档中的%history。

来自安德鲁·琼斯的网站(存档):

import readline
readline.write_history_file('/home/ahj/history')

除了IPython之外,类似的实用程序bpython还有一个“将您输入的代码保存到文件中”的特性

保存XUbuntu的输入输出:

在XWindows中,从Xfce终端应用程序运行iPython 单击顶部菜单栏中的“终端”,在下拉菜单中查找保存内容

我发现这节省了输入和输出,一直追溯到我打开终端的时候。这不是特定于ipython的,它与ssh会话或从终端窗口运行的其他任务一起工作。

在安装Ipython并打开Ipython会话后,执行以下命令:

ipython

在你的命令行中,只需运行下面的Ipython 'magic'命令来自动记录你的整个Ipython会话:

%logstart

这将创建一个唯一命名的.py文件,并存储您的会话,以供以后作为交互式Ipython会话使用或在您选择的脚本中使用。