Skip to content Skip to sidebar Skip to footer

How To Save An Ipython Session If I Used Magic %paste ? (magic %save Doesn't Work)

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

Solution 1:

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]: historypaste
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]: 

But the help for %save states that

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

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 ').

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.

Solution 2:

As kdopen's says, if you use %save

is only saving the commands you type at the prompt

If you want to record the input of %paste you could use %logstart, during your session, you can stop and start logging with: %logoff/%logon

Here is more info on how to use this command: logstart

When you want to restore your session remember to run ipython with: -i yourlogfile.py command line option. ie: ipython3 -i ipython_log.py

Post a Comment for "How To Save An Ipython Session If I Used Magic %paste ? (magic %save Doesn't Work)"