It would be useful to save the session variables which could be loaded easily into memory at a later stage.
-
2Have you considered `pickle`? It's pretty nice for saving things so they can be loaded again later ... – mgilson Sep 20 '12 at 01:30
-
3Related: [How to save a Python interactive session?](http://stackoverflow.com/questions/947810/) – Piotr Dobrogost Sep 20 '12 at 10:01
5 Answers
In [23]: %logstart /tmp/session.log
Activating auto-logging. Current session state plus future input saved.
Filename : /tmp/session.log
Mode : backup
Output logging : False
Raw input log : False
Timestamping : False
State : active
In [24]: x = 1
In [25]: %logstop
In [26]: quit()
Do you really want to exit ([y]/n)? y
Then we can restore the session with:
% ipython -log /tmp/session.log
Activating auto-logging. Current session state plus future input saved.
Filename : ipython_log.py
...
In [1]: x
Out[1]: 1
For more on "Session logging and restoring" see the docs.
Note that this merely stores the commands run by IPython. It does not save the state of the IPython session. Restoring the session requires re-execution of the commands.
If you set the PYTHONSTARTUP environment variable to point at a file called, say, startup.py
:
PYTHONSTARTUP=/path/to/startup.py
then put the following in /path/to/startup.py:
try:
# https://stackoverflow.com/a/5377051/190597 (Tom Dunham)
__IPYTHON__
except NameError:
pass
else:
# https://stackoverflow.com/a/15898875/190597 (user2261139)
from IPython import get_ipython
ipython = get_ipython()
ipython.magic("%logstart /tmp/session.log")
then IPython will call %logstart automatically whenever you start an interactive session.

- 842,883
- 184
- 1,785
- 1,677
-
27Why was this answer accepted? This does not address the question of saving all of the variables and data defined in the session (for which there is not a clean solution to my knowledge for Python). – Wes McKinney Oct 24 '12 at 16:15
-
Totally agree - whilst its interesting its not the solution, I don't think. Does anyone know anything more about this? I would love to be able to just load a session and start from where I left off. Its common in other data analysis languages/suites – nrob Jul 30 '13 at 10:25
-
2Well. To the best of my knowledge, it is the solution if you know about it before you defined those variables. Another way would be to figure out how to run this by default when you start ipython command-line. – Navneet Aug 20 '13 at 21:49
-
Can this be started automatically at python/ipython startup? Then it would be very useful – alpha_989 Jan 06 '18 at 18:00
-
1
Looking for something similar I came across save_ipython_variables:
save-ipython-variables
lets you ... save your global IPython variables to disk easily, and load them back into the global namespace when you need them again, even in a whole new IPython session.
I haven't had much chance to use it yet, but looks promising.

- 2,097
- 1
- 16
- 24
-
Did anyone get the line `data = build_data_dictionary_from_db()` from this package to work? – cardamom Nov 08 '16 at 18:37
I haven't tried this yet, but starting from AE Drew's answer, I found a possible alternative. Looks like IPython has a built in magic command that does this called %store:
%store magic for lightweight persistence. Stores variables, aliases and macros in IPython’s database. To automatically restore stored variables at startup, add this to your ipython_config.py file:
c.StoreMagic.autorestore = True

- 1,707
- 2
- 13
- 17
-
1You need to call %store to store every variable you want to save. If your variable has refreshed value you need to call %store again. That sounds very annoying. – czxttkl Apr 04 '16 at 12:14
-
Agree that this is a pain, but it's still useful and better than not having the ability to do this. – Michael Szczepaniak Oct 03 '16 at 19:13
There is also a magic command, history
, that can be used to write all the commands/statements given by user.
Syntax : %history -f file_name
.
Also %save file_name start_line-end_line
, where star_line is the starting line number and end_line is ending line number. Useful in case of selective save.
%run
can be used to execute the commands in the saved file

- 2,137
- 21
- 23
-
if you are using ipython, you dont need to worry about the commands. Those are automatically saved as a session, anytime you enter a command. OP is asking about saving variables – alpha_989 Jan 06 '18 at 17:58
Not my solution, but this seems to be the closest solution, if you are using ipython: https://stackoverflow.com/a/28552465/4752883

- 4,882
- 2
- 37
- 48