如何保存Python交互式会话?

问题:如何保存Python交互式会话?

我发现自己经常使用Python的解释器来处理数据库,文件等-基本上是半结构化数据的大量手动格式化。我没有按我的意愿正确地保存和清理有用的位。有没有一种方法可以将我的输入保存到外壳中(数据库连接,变量分配,很少用于循环和逻辑位)-交互式会话的一些历史记录?如果我使用类似的东西,script则会收到过多的标准输出噪音。我真的不需要腌制所有对象-尽管如果有解决方案可以做到这一点,那就可以了。理想情况下,我只剩下一个脚本,该脚本可以像我交互式创建的那样运行,并且我可以删除不需要的部分。是否有这样做的包装或DIY方法?

更新:我对这些软件包的质量和实用性感到非常惊讶。对于那些类似的痒:

  • IPython-早就应该使用它了
  • 重新互动 -非常令人印象深刻,我想了解有关可视化的更多信息,这似乎会在这里闪耀。排序图的gtk / gnome桌面应用程序。想象一下混合壳+图形计算器+迷你月食。此处的源代码分发:http : //www.reinteract.org/trac/wiki/GettingIt。可以在Ubuntu上很好地构建,也可以集成到gnome桌面,Windows和Mac安装程序中。
  • bpython-非常酷,有很多不错的功能,自动完成(!),倒带,一键保存到文件,缩进,做得很好。Python源代码发行版从sourceforge中提取了两个依赖项。

我被转换了,这些真的满足了解释器和编辑器之间的需求。

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?

UPDATE: I am really amazed at the quality and usefulness of these packages. For those with a similar itch:

  • IPython — should have been using this for ages, kind of what I had in mind
  • reinteract — very impressive, I want to learn more about visualization and this seems like it will shine there. Sort of a gtk/gnome desktop app that renders graphs inline. Imagine a hybrid shell + graphing calculator + mini eclipse. Source distribution here: http://www.reinteract.org/trac/wiki/GettingIt . Built fine on Ubuntu, integrates into gnome desktop, Windows and Mac installers too.
  • bpython — extremely cool, lots of nice features, autocomplete(!), rewind, one keystroke save to file, indentation, well done. Python source distribution, pulled a couple of dependencies from sourceforge.

I am converted, these really fill a need between interpreter and editor.


回答 0

如果您喜欢使用交互式会话,则IPython非常有用。例如,对于您的用例,有一个%savemagic命令,您只需输入%save my_useful_session 10-20 23以保存输入行10至20和23至my_useful_session.py(为此,每行都以其编号作为前缀)。

此外,文档指出:

此函数对输入范围使用与%history相同的语法,然后将这些行保存到您指定的文件名中。

例如,这允许引用较旧的会话,例如

%save current_session ~0/
%save previous_session ~1/

观看演示页面的视频,以快速了解这些功能。

IPython is extremely useful if you like using interactive sessions. For example for your use-case there is the %save magic command, you just input %save my_useful_session 10-20 23 to save input lines 10 to 20 and 23 to my_useful_session.py (to help with this, every line is prefixed by its number).

Furthermore, the documentation states:

This function uses the same syntax as %history for input ranges, then saves the lines to the filename you specify.

This allows for example, to reference older sessions, such as

%save current_session ~0/
%save previous_session ~1/

Look at the videos on the presentation page to get a quick overview of the features.


回答 1

http://www.andrewhjon.es/save-interactive-python-session-history

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

http://www.andrewhjon.es/save-interactive-python-session-history

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

回答 2

有一个 方法可以做到。将文件存储在~/.pystartup

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

然后PYTHONSTARTUP在您的shell中设置环境变量(例如在中~/.bashrc):

export PYTHONSTARTUP=$HOME/.pystartup

您还可以添加以下内容以免费获取自动完成功能:

readline.parse_and_bind('tab: complete')

请注意,这仅适用于* nix系统。由于readline仅在Unix平台上可用。

There is a way to do it. Store the file in ~/.pystartup

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

and then set the environment variable PYTHONSTARTUP in your shell (e.g. in ~/.bashrc):

export PYTHONSTARTUP=$HOME/.pystartup

You can also add this to get autocomplete for free:

readline.parse_and_bind('tab: complete')

Please note that this will only work on *nix systems. As readline is only available in Unix platform.


回答 3

如果使用的是IPython,则可以使用魔术函数%history-f参数pe 将以前的所有命令保存到文件中:

%history -f /tmp/history.py

If you are using IPython you can save to a file all your previous commands using the magic function %history with the -f parameter, p.e:

%history -f /tmp/history.py

回答 4

安装Ipython并通过运行以下命令打开Ipython会话后:

ipython

从命令行中,只需运行以下Ipython’magic’命令以自动记录整个Ipython会话:

%logstart

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

After installing Ipython, and opening an Ipython session by running the command:

ipython

from your command line, just run the following Ipython ‘magic’ command to automatically log your entire Ipython session:

%logstart

This will create a uniquely named .py file and store your session for later use as an interactive Ipython session or for use in the script(s) of your choosing.


回答 5

同样,重新交互为您提供了类似于笔记本的Python会话界面。

Also, reinteract gives you a notebook-like interface to a Python session.


回答 6

除了IPython,类似的实用程序bpython还具有“将您输入的代码保存到文件中”的功能

In addition to IPython, a similar utility bpython has a “save the code you’ve entered to a file” feature


回答 7

我必须努力寻找答案,我对iPython环境非常陌生。

这会工作

如果您的iPython会话如下所示

In [1] : import numpy as np
....
In [135]: counter=collections.Counter(mapusercluster[3])
In [136]: counter
Out[136]: Counter({2: 700, 0: 351, 1: 233})

您想要保存从1到135的行,然后在同一ipython会话上使用此命令

In [137]: %save test.py 1-135

这会将所有python语句保存在当前目录(启动ipython的位置)的test.py文件中。

I had to struggle to find an answer, I was very new to iPython environment.

This will work

If your iPython session looks like this

In [1] : import numpy as np
....
In [135]: counter=collections.Counter(mapusercluster[3])
In [136]: counter
Out[136]: Counter({2: 700, 0: 351, 1: 233})

You want to save lines from 1 till 135 then on the same ipython session use this command

In [137]: %save test.py 1-135

This will save all your python statements in test.py file in your current directory ( where you initiated the ipython).


回答 8

有%history魔术可用于打印和保存输入历史记录(以及可选的输出)。

要将当前会话存储到名为的文件中my_history.py

>>> %hist -f my_history.py

历史记录IPython既存储您输入的命令,又存储它产生的结果。您可以使用上下箭头键轻松查看以前的命令,或者以更复杂的方式访问历史记录。

您可以使用%history magic函数来检查过去的输入和输出。先前会话的输入历史记录保存在数据库中,并且可以配置IPython来保存输出历史记录。

其他几个魔术功能也可以使用您的输入历史记录,包括%edit,%rerun,%recall,%macro,%save和%pastebin。您可以使用标准格式来引用行:

%pastebin 3 18-20 ~1/1-5

这将占用当前会话的第3行和第18至20行,以及上一会话的第1-5行。

看到%history?Docstring和更多示例。

另外,请确保探索%store magic在IPython中实现变量的轻量级持久性的功能。

在IPython的数据库中存储变量,别名和宏。

d = {'a': 1, 'b': 2}
%store d  # stores the variable
del d

%store -r d  # Refresh the variable from IPython's database.
>>> d
{'a': 1, 'b': 2}

要在启动c.StoreMagic.autorestore = True时自动恢复存储的变量,请在ipython_config.py中指定。

There is %history magic for printing and saving the input history (and optionally the output).

To store your current session to a file named my_history.py:

>>> %hist -f my_history.py

History IPython stores both the commands you enter, and the results it produces. You can easily go through previous commands with the up- and down-arrow keys, or access your history in more sophisticated ways.

You can use the %history magic function to examine past input and output. Input history from previous sessions is saved in a database, and IPython can be configured to save output history.

Several other magic functions can use your input history, including %edit, %rerun, %recall, %macro, %save and %pastebin. You can use a standard format to refer to lines:

%pastebin 3 18-20 ~1/1-5

This will take line 3 and lines 18 to 20 from the current session, and lines 1-5 from the previous session.

See %history? for the Docstring and more examples.

Also, be sure to explore the capabilities of %store magic for lightweight persistence of variables in IPython.

Stores variables, aliases and macros in IPython’s database.

d = {'a': 1, 'b': 2}
%store d  # stores the variable
del d

%store -r d  # Refresh the variable from IPython's database.
>>> d
{'a': 1, 'b': 2}

To autorestore stored variables on startup, specifyc.StoreMagic.autorestore = True in ipython_config.py.


回答 9

只是在碗里放另一个建议: Spyder

它具有历史记录日志变量资源管理器。如果您使用过MatLab,那么您将看到相似之处。

Just putting another suggesting in the bowl: Spyder

It has History log and Variable explorer. If you have worked with MatLab, then you’ll see the similarities.


回答 10

就Linux而言,人们可以使用script命令来记录整个会话。它是util-linux软件包的一部分,因此应在大多数Linux系统上使用。您可以创建将要调用的别名或函数script -c python,并将其保存到typescript文件中。例如,这是一个这样的文件的重印。

$ cat typescript                                                                                                      
Script started on Sat 14 May 2016 08:30:08 AM MDT
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Hello Pythonic World'
Hello Pythonic World
>>> 

Script done on Sat 14 May 2016 08:30:42 AM MDT

这里的一个小缺点是script,无论何时碰到退格键等等,它都会记录所有内容,甚至是换行。因此,您可能希望使用它col来清理输出(请参阅Unix&Linux Stackexchange上的这篇文章)。

As far as Linux goes, one can use script command to record the whole session. It is part of util-linux package so should be on most Linux systems . You can create and alias or function that will call script -c python and that will be saved to a typescript file. For instance, here’s a reprint of one such file.

$ cat typescript                                                                                                      
Script started on Sat 14 May 2016 08:30:08 AM MDT
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Hello Pythonic World'
Hello Pythonic World
>>> 

Script done on Sat 14 May 2016 08:30:42 AM MDT

Small disadvantage here is that the script records everything , even line-feeds, whenever you hit backspaces , etc. So you may want to use col to clean up the output (see this post on Unix&Linux Stackexchange) .


回答 11

%history命令很棒,但不幸的是,它不会让您将%paste的内容保存到sesh中。为此,我认为您必须在一开始就做%logstart (尽管我尚未确认这项工作有效)。

我喜欢做的是

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

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

The %history command is awesome, but unfortunately it won’t let you save things that were %paste ‘d into the sesh. To do that I think you have to do %logstart at the beginning (although I haven’t confirmed this works).

What I like to do is

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

which will save the output, line numbers, and ‘>>>’ before each input (o, n, and p options). See the docs for %history here.


回答 12

还有另一种选择— pyslice。在“ wxpython 2.8文档演示和工具”中,有一个名为“ pyslices”的开源程序。

您可以像编辑器一样使用它,它还可以像控制台一样使用—-像执行即时回显的交互式解释器一样执行每一行。

当然,所有代码块和每个块的结果将自动记录到txt文件中。

结果记录在相应的代码块后面。很方便。

there is another option — pyslice. in the “wxpython 2.8 docs demos and tools”, there is a open source program named “pyslices”.

you can use it like a editor, and it also support using like a console —- executing each line like a interactive interpreter with immediate echo.

of course, all the blocks of codes and results of each block will be recorded into a txt file automatically.

the results are logged just behind the corresponding block of code. very convenient.


回答 13

如果使用bpython,则默认情况下所有命令历史记录都保存到~/.pythonhist

要保存命令以供以后重用,可以将它们复制到python脚本文件中:

$ cp ~/.pythonhist mycommands.py

然后编辑该文件以将其清理并放在Python路径(全局或虚拟环境的站点包,当前目录,*。pth中提及或其他方式)下。

要将命令包括到您的shell中,只需从保存的文件中导入它们:

>>> from mycommands import *

If you use bpython, all your command history is by default saved to ~/.pythonhist.

To save the commands for later reusage you can copy them to a python script file:

$ cp ~/.pythonhist mycommands.py

Then edit that file to clean it up and put it under Python path (global or virtual environment’s site-packages, current directory, mentioning in *.pth, or some other way).

To include the commands into your shell, just import them from the saved file:

>>> from mycommands import *

回答 14

一些评论询问如何立即保存所有IPython输入。对于IPython中的%s magic,可以如下所示以编程方式保存所有命令,以避免出现提示消息,也避免指定输入数字。currentLine = len(In)-1%保存-f my_session 1- $ currentLine

-f选项用于强制替换文件,并len(IN)-1在IPython中显示当前输入提示,从而允许您以编程方式保存整个会话。

Some comments were asking how to save all of the IPython inputs at once. For %save magic in IPython, you can save all of the commands programmatically as shown below, to avoid the prompt message and also to avoid specifying the input numbers. currentLine = len(In)-1 %save -f my_session 1-$currentLine

The -f option is used for forcing file replacement and the len(IN)-1 shows the current input prompt in IPython, allowing you to save the whole session programmatically.


回答 15

对于那些使用spacemacsipython附带的用户python-layer,由于在后台运行恒定的自动完成命令,例如save,魔术会产生很多不需要的输出:

len(all_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all_substa'''))
len(all_substantives_w_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all'''))
';'.join(__PYTHON_EL_get_completions('''all_'''))
';'.join(__PYTHON_EL_get_completions('''all_w'''))
';'.join(__PYTHON_EL_get_completions('''all_wo'''))
';'.join(__PYTHON_EL_get_completions('''all_wor'''))
';'.join(__PYTHON_EL_get_completions('''all_word'''))
';'.join(__PYTHON_EL_get_completions('''all_words'''))
len(all_words_w_logograms)
len(all_verbs)

为了避免这种情况,只需像平时保存其他任何文件一样保存ipython缓冲区即可: spc f s

For those using spacemacs, and ipython that comes with python-layer, save magic creates a lot of unwanted output, because of the constant auto-completion command working in the backround such as:

len(all_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all_substa'''))
len(all_substantives_w_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all'''))
';'.join(__PYTHON_EL_get_completions('''all_'''))
';'.join(__PYTHON_EL_get_completions('''all_w'''))
';'.join(__PYTHON_EL_get_completions('''all_wo'''))
';'.join(__PYTHON_EL_get_completions('''all_wor'''))
';'.join(__PYTHON_EL_get_completions('''all_word'''))
';'.join(__PYTHON_EL_get_completions('''all_words'''))
len(all_words_w_logograms)
len(all_verbs)

To avoid this just save the ipython buffer like you normally save any other: spc f s


回答 16

我想提出另一种在Linux上通过tmux维护python会话的方法。您运行tmux,将自己附加到您打开的会话中(如果直接打开后未附加)。执行python并在上面执行任何操作。然后脱离会话。从tmux会话中分离不会关闭该会话。会话保持打开状态。

这种方法的优点: 您可以从任何其他设备连接到此会话(以防万一您可以SSH电脑)

此方法的缺点: 在您实际存在python解释器之前,此方法不会放弃打开的python会话使用的资源。

I’d like to suggest another way to maintain python session through tmux on linux. you run tmux, attach your self to the session you opened (if not attached after opening it directly). execute python and do whatever you are doing on it. then detach from session. detaching from a tmux session does not close the session. the session remains open.

pros of this method: you can attach to this session from any other device (in case you can ssh your pc)

cons of this method: this method does not relinquish the resources used by the opened python session until you actually exist the python interpreter.


回答 17

要在XUbuntu上保存输入和输出

  1. 在XWindows中,从Xfce终端应用程序运行iPython
  2. Terminal在顶部菜单栏中单击,然后在save contents下拉菜单中查找

我发现这可以保存输入和输出,一直到打开终端时一直返回。这不是ipython特有的,并且可以与ssh会话或从终端窗口运行的其他任务一起使用。

To save input and output on XUbuntu:

  1. In XWindows, run iPython from the Xfce terminal app
  2. click Terminal in the top menu bar and look for save contents in the dropdown

I find this saves the input and output, going all the way back to when I opened the terminal. This is not ipython specific, and would work with ssh sessions or other tasks run from the terminal window.