I followed this topic How to save all the variables in the current python session? to save all my python variables in a file.
I did the following code:
import shelve
def saveWorkspaceVariables(pathSavedVariables):
# This functions saves all the variables in a file.
my_shelf = shelve.open(pathSavedVariables,'n') # 'n' for new
for key in dir():
try:
my_shelf[key] = globals()[key]
except TypeError:
#
# __builtins__, my_shelf, and imported modules can not be shelved.
#
print('ERROR shelving: {0}'.format(key))
my_shelf.close()
T="test"
saveWorkspaceVariables("file.out")
However, it raises: KeyError: 'my_shelf'
.
Why so? How to solve this issue?