How to Save All the Variables in the Current Python Session

How to save all the variables in the current python session?

If you use shelve, you do not have to remember the order in which the objects are pickled, since shelve gives you a dictionary-like object:

To shelve your work:

import shelve


my_shelf =,'n') # 'n' for new

for key in dir():
my_shelf[key] = globals()[key]
except TypeError:
# __builtins__, my_shelf, and imported modules can not be shelved.
print('ERROR shelving: {0}'.format(key))

To restore:

my_shelf =
for key in my_shelf:

# Hiya
# [1, 2, 3]

Save variables in Python session?

I think the closest thing you're going to find is ipython. I'm not sure if it does the variable saving thing, but I believe it does everything else you're asking for.

How to save all python objects from working directory to a file

If I understood your need, you want to backup your session.
If that is the case, here is a solution using pickle. Kr.

First solution is:

import pickle

def is_picklable(obj):
except Exception:
return False
return True

bk = {}
for k in dir():
obj = globals()[k]
if is_picklable(obj):
bk.update({k: obj})
except TypeError:

# to save session
with open('./your_bk.pkl', 'wb') as f:
pickle.dump(bk, f)

# to load your session

with open('./your_bk.pkl', 'rb') as f:
bk_restore = pickle.load(f)

***Second solution is with dill. You might have error if in your workspace, there are some unpicklable objects ***:

import dill

#to restore session:

Third option go with shelve package:

import shelve

bk ='./your_bk_shelve.pkl','n')
for k in dir():
bk[k] = globals()[k]
except Exception:

# to restore
bk_restore ='./your_bk_shelve.pkl')
for k in bk_restore:
tmp[k] = bk_restore[k]

Check and let's know about your trial.

Credits: The second and third solution are nearly a shameless copy/paste from those two links belows. I adapted the handling of errors as the original answer will lead to error for pickling of module.

dill solution

shelve solution

How to save a Python interactive session?

IPython is extremely useful if you like using interactive sessions. For example for your use-case there is the %save magic command, you just input %save my_useful_session 10-20 23 to save input lines 10 to 20 and 23 to (to help with this, every line is prefixed by its number).

Furthermore, the documentation states:

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

This allows for example, to reference older sessions, such as

%save current_session ~0/
%save previous_session ~1/

Look at the videos on the presentation page to get a quick overview of the features.

Related Topics

Leave a reply