Is it possible to save an IPython workspace (defined functions, different kinds of variables, etc) so that it can be loaded later?
This would be a similar function to save.image()
in MATLAB or R. Similar questions has been asked before, such as:
Save session in IPython like in MATLAB?
However, since a few years passed, I am wondering if there is a good solution now.
Although not so convenient as
save.image()
, you can use one of the checkpoint/restore applications. If you're using Linux, you might try http://criu.org. I'm using it from time to time to dump myipython
state and restore it later.In order to dump a shell app with CRIU, you need to find its PID (e.g.
pstree -p
) and then use something like this (you'll need a second terminal for this; CRIU can't dump stopped jobs):this will write all necessary images to ~/tmp/imgs (remember the
--shell-job
option). In order to restore the state later to the current terminal (don't forget to hit enter to get the nextipython
prompt):Check out the logs in case of any problems.
Obviously CRIU will work with any app (with some limits, of course). It's just an idea so you can use it for
ipython
.You can try
Like if you have input 67 commands and you want to save all of them:
You can use the dill python package:
To install it:
I added a somewhat ad-hoc solution that automates the process of storing/restoring user space variables using the underlying code from IPython's %store magic which is from what I understand what you wanted. See the gist here. Note that this only works for objects that can be pickled.
I can't guarantee its robustness, especially if any of the autorestore mechanisms in IPython change in the future, but it has been working for me with IPython 2.1.0. Hopefully this will at least point you in the right direction.
To reiterate the solution here:
Add this line to your profile's ipython startup script (e.g., $HOME/.ipython/profile_default/startup/startup.py):
get_ipython().ex("import save_user_variables;del save_user_variables")
In your ipython profile config file (by default $HOME/.ipython/profile_default/ipython_config.py) find the following line:
# c.StoreMagics.autorestore = False
Uncomment it and set it to true. This automatically reloads stored variables on startup. Alternatively you can reload the last session manually using %store -r.
save_user_variables.py
you can certainly do this in the ipython notebook.
when the notebook is saved--either manually or by default config--the notebook is persisted as an .ipynb file, which is just a json file (an example in a github gist).
Next time you start the ipython server in the directory where that file resides, the server will detect it.
when you open that notebook in the browser, all of the code & config is there but unexecuted; you can execute the code in every cell by selecting execute all cells from the cells menu.
in addition, you can manually persist snapshots of your notebook, as ipynb_checkpoints, which are stored in a directory of that name preceded by a dot.
and finally, from the file menu option, you can persist your notebook as a pure python source file (.py)