如果我使用魔术%paste,如何保存ipython会话? (魔术%save不起作用) [英] How to save an ipython session if i used magic %paste ? (magic %save doesn't work)

查看:90
本文介绍了如果我使用魔术%paste,如何保存ipython会话? (魔术%save不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个非常简单的问题,但我无法使用魔术%save"正确保存我的ipython会话 如果我在会话期间的任何时间使用魔术%paste",则将会话保存到此输出中:

It's seems a fairly simple question but I didn't manage to correctly save my ipython session using magic "%save" If i use the magic "%paste" at any time during the session, saving the session results in this output :

get_ipython().magic(u'paste')

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

Of course, I would like the pasted code to be saved instead.

有什么想法吗?

推荐答案

我可以在Ubuntu上的ipython 3.1.0版本中重现此问题

I can reproduce this with version 3.1.0 of ipython on Ubuntu

$ 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相同的语法, 然后将这些行保存到您指定的文件名中.

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

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

and that's actually what is happening. It is only saving the commands you type at the prompt, and the command you entered was paste, which the magic converts to get_ipython().magic(u'paste ').

有趣的是,尝试editpaste创建的函数会使我陷入vi中,因为他们盯着同一条魔术命令而不是粘贴的函数.

Interestingly, trying to edit the function I created with paste puts me in vi staring at the same magic command and not the pasted function.

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

While the first behavior may not be a bug (even if it's not particularly helpful), this editing behavior definitely seems like one.

这篇关于如果我使用魔术%paste,如何保存ipython会话? (魔术%save不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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