1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
StarBucK
  • 209
  • 4
  • 18

2 Answers2

0

May not be the answer you're looking for, but depending on what IDE you're using you may be able to save your session from there. I know Spyder has this functionality.

Mgaraz
  • 81
  • 5
0

The dir() function without an argument returns a list of all the names in the current scope. This list includes the function's local variables, such as my_shelf and pathSavedVariables.

But since these are local variables, globals() won't include them, since it only returns global variables.

You don't need to save the local variables, so you shouldn't use dir(). Use globals() to get all the global variables.

for key in globals():

The reason it works in the other question is that the code is not in a function, so there are no local variables to exclude.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you for your answer. Even if I switch from dir() to globals() in the for, I get an error: "Can't pickle : it's not the same object as builtins.input" – StarBucK Sep 21 '20 at 16:01
  • Some data types, such as functions, can't be serialized. You probably should only save your application variables, not all the built-in variables. – Barmar Sep 21 '20 at 16:02
  • So in the end what I want is really that the variable that I made during my script (not the functions, only the variables) are saved into a file. I have many of them so I cannot do it "one by one". How can I proceed ? – StarBucK Sep 21 '20 at 16:03
  • See the comments in the original question. Change `except TypeError:` to a more general `except:` – Barmar Sep 21 '20 at 16:03
  • You should make a list of those variables, and loop through that instead of using `globals()`. – Barmar Sep 21 '20 at 16:04