0

这似乎是一个相当简单的问题,但我没有设法使用魔法“%save”正确保存我的 ipython 会话如果我在会话期间的任何时间使用魔法“%paste”,保存会话会导致此输出:

get_ipython().magic(u'paste')

当然,我希望保存粘贴的代码。

有任何想法吗?

4

2 回答 2

1

我可以在 Ubuntu 上使用 3.1.0 版的 ipython 重现这一点

$ ipython
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: paste
def f(x):
    return 2**x
## -- End pasted text --

In [2]: f(4)
Out[2]: 16

In [3]: history
paste
f(4)
history

In [4]: save ptest.py 1-3
The following commands were written to file `ptest.py`:
get_ipython().magic(u'paste ')
f(4)
get_ipython().magic(u'history ')
In [5]: cat ptest.py
# coding: utf-8
get_ipython().magic(u'paste ')
f(4)
get_ipython().magic(u'history ')

In [6]: 

但对%save国家的帮助

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

这就是正在发生的事情。它只是保存您在提示符下键入的命令,而您输入的命令是paste,魔法转换为get_ipython().magic(u'paste ').

有趣的是,尝试edit使用我创建的函数paste会让我vi盯着同一个魔法命令而不是粘贴的函数。

虽然第一个行为可能不是错误(即使它不是特别有用),但这种编辑行为肯定看起来像一个错误。

于 2015-04-30T13:51:58.343 回答
1

正如 kdopen 所说,如果你使用%save

仅保存您在提示符下键入的命令

如果你想记录%paste你可以使用的输入%logstart,在你的会话期间,你可以停止并开始记录:%logoff/%logon

以下是有关如何使用此命令的更多信息:logstart

当您想恢复会话时,请记住使用:-i yourlogfile.py命令行选项运行 ipython。IE:ipython3 -i ipython_log.py

于 2015-07-31T15:02:15.223 回答