如何保存当前python会话中的所有变量?

时间:2021-03-16 22:50:17

I want to save all the variables in my current python environment. It seems one option is to use the 'pickle' module. However, I don't want to do this for 2 reasons:

我想在当前的python环境中保存所有的变量。一个选择似乎是使用“pickle”模块。但是,我不想这样做有两个原因:

1) I have to call pickle.dump() for each variable
2) When I want to retrieve the variables, I must remember the order in which I saved the variables, and then do a pickle.load() to retrieve each variable.

1)我必须为每个变量调用pickle.dump() 2)当我想检索变量时,我必须记住保存变量的顺序,然后执行pickle.load()来检索每个变量。

I am looking for some command which would save the entire session, so that when I load this saved session, all my variables are restored. Is this possible?

我正在寻找一些可以保存整个会话的命令,这样当我加载这个保存的会话时,所有的变量都会被恢复。这是可能的吗?

Thanks a lot!
Gaurav

谢谢!Gaurav

Edit: I guess I don't mind calling pickle.dump() for each variable that I would like to save, but remembering the exact order in which the variables were saved seems like a big restriction. I want to avoid that.

编辑:我想我不介意为每个我想要保存的变量调用pickle.dump(),但是记住变量保存的确切顺序似乎是一个很大的限制。我想避免这种情况。

7 个解决方案

#1


57  

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:

如果使用shelve,则不需要记住对对象进行pickle的顺序,因为shelve提供了一个类似于字典的对象:

To shelve your work:

把你的工作:

import shelve

T='Hiya'
val=[1,2,3]

filename='/tmp/shelve.out'
my_shelf = shelve.open(filename,'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()

To restore:

恢复:

my_shelf = shelve.open(filename)
for key in my_shelf:
    globals()[key]=my_shelf[key]
my_shelf.close()

print(T)
# Hiya
print(val)
# [1, 2, 3]

#2


28  

Having sat here and failed to save the globals() as a dictionary, I discovered you can pickle a session using the dill library.

坐在这里并没有将globals()保存为字典,我发现您可以使用dill库对会话进行pickle。

This can be done by using:

这可以通过以下方法实现:

import dill                            #pip install dill --user
filename = 'globalsave.pkl'
dill.dump_session(filename)

# and to load the session again:
dill.load_session(filename)

#3


4  

Here is a way saving the Spyder workspace variables using the spyderlib functions

这里有一种使用spyderlib函数保存Spyder工作区变量的方法

#%%  Load data from .spydata file
from spyderlib.utils.iofuncs import load_dictionary

globals().update(load_dictionary(fpath)[0])
data = load_dictionary(fpath)



#%% Save data to .spydata file
from spyderlib.utils.iofuncs import save_dictionary
def variablesfilter(d):
    from spyderlib.widgets.dicteditorutils import globalsfilter
    from spyderlib.plugins.variableexplorer import VariableExplorer
    from spyderlib.baseconfig import get_conf_path, get_supported_types

    data = globals()
    settings = VariableExplorer.get_settings()

    get_supported_types()
    data = globalsfilter(data,                   
                         check_all=True,
                         filters=tuple(get_supported_types()['picklable']),
                         exclude_private=settings['exclude_private'],
                         exclude_uppercase=settings['exclude_uppercase'],
                         exclude_capitalized=settings['exclude_capitalized'],
                         exclude_unsupported=settings['exclude_unsupported'],
                         excluded_names=settings['excluded_names']+['settings','In'])
    return data

def saveglobals(filename):
    data = globalsfiltered()
    save_dictionary(data,filename)


#%%

savepath = 'test.spydata'

saveglobals(savepath) 

Let me know if it works for you. David B-H

如果它对你有用,请告诉我。大卫b - h

#4


2  

What you're trying to do is to hibernate your process. This was discussed already. The conclusion is that there are several hard-to-solve problems exist while trying to do so. For example with restoring open file descriptors.

您要做的是休眠进程。这是已经讨论。结论是,在尝试解决这些问题时,存在着几个难以解决的问题。例如,恢复打开的文件描述符。

It is better to think about serialization/deserialization subsystem for your program. It is not trivial in many cases, but is far better solution in long-time perspective.

最好考虑程序的序列化/反序列化子系统。在许多情况下,它并不是微不足道的,但从长期的角度来看,它是更好的解决方案。

Although if I've exaggerated the problem. You can try to pickle your global variables dict. Use globals() to access the dictionary. Since it is varname-indexed you haven't to bother about the order.

尽管我夸大了问题。可以尝试pickle全局变量dict.使用globals()访问字典。由于它是可变索引的,所以您不必费心考虑顺序。

#5


1  

One very easy way that might satisfy your needs. For me, it did pretty well:

一个很简单的方法可以满足你的需要。对我来说,它做得很好:

Simply, click on this icon on the Variable Explorer (right side of Spider):

简单地说,单击变量Explorer (Spider的右侧)上的图标:

Saving all the variables in *.spydata format

保存*中的所有变量。spydata格式

Loading all the variables or pics etc.

加载所有变量或图片等。

#6


0  

If you want the accepted answer abstracted to function you can use:

如果你想要被接受的答案抽象为函数,你可以使用:

    import shelve

    def save_workspace(filename, names_of_spaces_to_save, dict_of_values_to_save):
    '''
        filename = location to save workspace.
        names_of_spaces_to_save = use dir() from parent to save all variables in previous scope.
            -dir() = return the list of names in the current local scope
        dict_of_values_to_save = use globals() or locals() to save all variables.
            -globals() = Return a dictionary representing the current global symbol table.
            This is always the dictionary of the current module (inside a function or method,
            this is the module where it is defined, not the module from which it is called).
            -locals() = Update and return a dictionary representing the current local symbol table.
            Free variables are returned by locals() when it is called in function blocks, but not in class blocks.

        Example of globals and dir():
            >>> x = 3 #note variable value and name bellow
            >>> globals()
            {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'x': 3, '__doc__': None, '__package__': None}
            >>> dir()
            ['__builtins__', '__doc__', '__name__', '__package__', 'x']
    '''
    print 'save_workspace'
    print 'C_hat_bests' in names_of_spaces_to_save
    print dict_of_values_to_save
    my_shelf = shelve.open(filename,'n') # 'n' for new
    for key in names_of_spaces_to_save:
        try:
            my_shelf[key] = dict_of_values_to_save[key]
        except TypeError:
            #
            # __builtins__, my_shelf, and imported modules can not be shelved.
            #
            #print('ERROR shelving: {0}'.format(key))
            pass
    my_shelf.close()

    def load_workspace(filename, parent_globals):
        '''
            filename = location to load workspace.
            parent_globals use globals() to load the workspace saved in filename to current scope.
        '''
        my_shelf = shelve.open(filename)
        for key in my_shelf:
            parent_globals[key]=my_shelf[key]
        my_shelf.close()

an example script of using this:
import my_pkg as mp

x = 3

mp.save_workspace('a', dir(), globals())

to get/load the workspace:

获得/载入工作区:

import my_pkg as mp

x=1

mp.load_workspace('a', globals())

print x #print 3 for me

it worked when I ran it. I will admit I don't understand dir() and globals() 100% so I am not sure if there might be some weird caveat, but so far it seems to work. Comments are welcome :)

当我运行它的时候,它起作用了。我承认我不完全理解dir()和globals(),因此我不确定是否存在某种奇怪的警告,但到目前为止它似乎是有效的。欢迎评论:)


after some more research if you call save_workspace as I suggested with globals and save_workspace is within a function it won't work as expected if you want to save the veriables in a local scope. For that use locals(). This happens because globals takes the globals from the module where the function is defined, not from where it is called would be my guess.

在进行了更多的研究之后,如果您像我建议的那样调用save_workspace和save_workspace,那么如果您想要将可验证性保存在本地范围内,那么save_workspace将不会像预期的那样工作。使用当地人()。这是因为全局变量从函数定义的模块中获取全局变量,而不是从它被调用的位置来判断。

#7


0  

You can save it as a text file or a CVS file. People use Spyder for example to save variables but it has a known issue: for specific data types it fails to import down in the road.

您可以将其保存为文本文件或CVS文件。例如,人们使用Spyder来保存变量,但它有一个已知的问题:对于特定的数据类型,它不能在路上导入。

#1


57  

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:

如果使用shelve,则不需要记住对对象进行pickle的顺序,因为shelve提供了一个类似于字典的对象:

To shelve your work:

把你的工作:

import shelve

T='Hiya'
val=[1,2,3]

filename='/tmp/shelve.out'
my_shelf = shelve.open(filename,'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()

To restore:

恢复:

my_shelf = shelve.open(filename)
for key in my_shelf:
    globals()[key]=my_shelf[key]
my_shelf.close()

print(T)
# Hiya
print(val)
# [1, 2, 3]

#2


28  

Having sat here and failed to save the globals() as a dictionary, I discovered you can pickle a session using the dill library.

坐在这里并没有将globals()保存为字典,我发现您可以使用dill库对会话进行pickle。

This can be done by using:

这可以通过以下方法实现:

import dill                            #pip install dill --user
filename = 'globalsave.pkl'
dill.dump_session(filename)

# and to load the session again:
dill.load_session(filename)

#3


4  

Here is a way saving the Spyder workspace variables using the spyderlib functions

这里有一种使用spyderlib函数保存Spyder工作区变量的方法

#%%  Load data from .spydata file
from spyderlib.utils.iofuncs import load_dictionary

globals().update(load_dictionary(fpath)[0])
data = load_dictionary(fpath)



#%% Save data to .spydata file
from spyderlib.utils.iofuncs import save_dictionary
def variablesfilter(d):
    from spyderlib.widgets.dicteditorutils import globalsfilter
    from spyderlib.plugins.variableexplorer import VariableExplorer
    from spyderlib.baseconfig import get_conf_path, get_supported_types

    data = globals()
    settings = VariableExplorer.get_settings()

    get_supported_types()
    data = globalsfilter(data,                   
                         check_all=True,
                         filters=tuple(get_supported_types()['picklable']),
                         exclude_private=settings['exclude_private'],
                         exclude_uppercase=settings['exclude_uppercase'],
                         exclude_capitalized=settings['exclude_capitalized'],
                         exclude_unsupported=settings['exclude_unsupported'],
                         excluded_names=settings['excluded_names']+['settings','In'])
    return data

def saveglobals(filename):
    data = globalsfiltered()
    save_dictionary(data,filename)


#%%

savepath = 'test.spydata'

saveglobals(savepath) 

Let me know if it works for you. David B-H

如果它对你有用,请告诉我。大卫b - h

#4


2  

What you're trying to do is to hibernate your process. This was discussed already. The conclusion is that there are several hard-to-solve problems exist while trying to do so. For example with restoring open file descriptors.

您要做的是休眠进程。这是已经讨论。结论是,在尝试解决这些问题时,存在着几个难以解决的问题。例如,恢复打开的文件描述符。

It is better to think about serialization/deserialization subsystem for your program. It is not trivial in many cases, but is far better solution in long-time perspective.

最好考虑程序的序列化/反序列化子系统。在许多情况下,它并不是微不足道的,但从长期的角度来看,它是更好的解决方案。

Although if I've exaggerated the problem. You can try to pickle your global variables dict. Use globals() to access the dictionary. Since it is varname-indexed you haven't to bother about the order.

尽管我夸大了问题。可以尝试pickle全局变量dict.使用globals()访问字典。由于它是可变索引的,所以您不必费心考虑顺序。

#5


1  

One very easy way that might satisfy your needs. For me, it did pretty well:

一个很简单的方法可以满足你的需要。对我来说,它做得很好:

Simply, click on this icon on the Variable Explorer (right side of Spider):

简单地说,单击变量Explorer (Spider的右侧)上的图标:

Saving all the variables in *.spydata format

保存*中的所有变量。spydata格式

Loading all the variables or pics etc.

加载所有变量或图片等。

#6


0  

If you want the accepted answer abstracted to function you can use:

如果你想要被接受的答案抽象为函数,你可以使用:

    import shelve

    def save_workspace(filename, names_of_spaces_to_save, dict_of_values_to_save):
    '''
        filename = location to save workspace.
        names_of_spaces_to_save = use dir() from parent to save all variables in previous scope.
            -dir() = return the list of names in the current local scope
        dict_of_values_to_save = use globals() or locals() to save all variables.
            -globals() = Return a dictionary representing the current global symbol table.
            This is always the dictionary of the current module (inside a function or method,
            this is the module where it is defined, not the module from which it is called).
            -locals() = Update and return a dictionary representing the current local symbol table.
            Free variables are returned by locals() when it is called in function blocks, but not in class blocks.

        Example of globals and dir():
            >>> x = 3 #note variable value and name bellow
            >>> globals()
            {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'x': 3, '__doc__': None, '__package__': None}
            >>> dir()
            ['__builtins__', '__doc__', '__name__', '__package__', 'x']
    '''
    print 'save_workspace'
    print 'C_hat_bests' in names_of_spaces_to_save
    print dict_of_values_to_save
    my_shelf = shelve.open(filename,'n') # 'n' for new
    for key in names_of_spaces_to_save:
        try:
            my_shelf[key] = dict_of_values_to_save[key]
        except TypeError:
            #
            # __builtins__, my_shelf, and imported modules can not be shelved.
            #
            #print('ERROR shelving: {0}'.format(key))
            pass
    my_shelf.close()

    def load_workspace(filename, parent_globals):
        '''
            filename = location to load workspace.
            parent_globals use globals() to load the workspace saved in filename to current scope.
        '''
        my_shelf = shelve.open(filename)
        for key in my_shelf:
            parent_globals[key]=my_shelf[key]
        my_shelf.close()

an example script of using this:
import my_pkg as mp

x = 3

mp.save_workspace('a', dir(), globals())

to get/load the workspace:

获得/载入工作区:

import my_pkg as mp

x=1

mp.load_workspace('a', globals())

print x #print 3 for me

it worked when I ran it. I will admit I don't understand dir() and globals() 100% so I am not sure if there might be some weird caveat, but so far it seems to work. Comments are welcome :)

当我运行它的时候,它起作用了。我承认我不完全理解dir()和globals(),因此我不确定是否存在某种奇怪的警告,但到目前为止它似乎是有效的。欢迎评论:)


after some more research if you call save_workspace as I suggested with globals and save_workspace is within a function it won't work as expected if you want to save the veriables in a local scope. For that use locals(). This happens because globals takes the globals from the module where the function is defined, not from where it is called would be my guess.

在进行了更多的研究之后,如果您像我建议的那样调用save_workspace和save_workspace,那么如果您想要将可验证性保存在本地范围内,那么save_workspace将不会像预期的那样工作。使用当地人()。这是因为全局变量从函数定义的模块中获取全局变量,而不是从它被调用的位置来判断。

#7


0  

You can save it as a text file or a CVS file. People use Spyder for example to save variables but it has a known issue: for specific data types it fails to import down in the road.

您可以将其保存为文本文件或CVS文件。例如,人们使用Spyder来保存变量,但它有一个已知的问题:对于特定的数据类型,它不能在路上导入。