我用以下方式创建一个对象(我知道这很可怕,不要问为什么,这是必要的):
with tempfile.NamedTemporaryFile(suffix='.py') as temp:
temp.write(code.encode('utf-8'))
import importlib.util
spec = importlib.util.spec_from_file_location('a_temp_module', temp.name)
temp_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(temp_module)
special_snowflake_object = temp_module.SpecialSnowflake()
现在SpecialSnowflake
类实现了两种保存和加载方法,它们基本上只是pickle和unpickle它:
def save(self, file_path: str):
with open(file_path, 'w') as fp:
pickle.dump(self, fp)
@staticmethod
def load(file_path: str):
with open(file_path, 'r') as fp:
return pickle.load(fp)
但是,在调用save
时,我得到了错误:
_pickle.PicklingError: Can't pickle <class 'a_temp_module.SpecialSnowflake'>: import of module 'a_temp_module' failed
这是相当奇怪的,因为我不知道你可以携带从调用实例化的对象,而不携带类定义本身,但显然,这就是这里发生的事情
我假设加载和保存可以通过以下方式完成:
with tempfile.NamedTemporaryFile(suffix='.py') as temp:
temp.write(code.encode('utf-8'))
import importlib.util
spec = importlib.util.spec_from_file_location('a_temp_module', temp.name)
temp_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(temp_module)
special_snowflake_object_1.save(path_to_pickle)
special_snowflake_object_2 = temp_module.SpecialSnowflake.load(path_to_pickle)
但是我更希望save
是可调用的,而不必重新加载模块,我想对于load,没有其他选择。我目前的想法是,我可以通过将模块的加载隐藏在save
函数中,并将代码作为对象的属性进行传递来实现这一点。。。但我希望有一个更优雅的解决方案
从
pickle
模块docs引用:pickle
无法picklea_temp_module.SpecialSnowflake
的实例,因为存储在a_temp_module
变量中的模块对象实际上没有使用import
语句导入到全局上下文中正如@martineau在注释中所述,
pickle
模块实际上并不在pickle文件中存储类定义。类定义必须可从同一模块导入再次从
pickle
{a2}引用:SpecialSnowflake
类的定义也没有在__main__
环境(或示例中的代码所在的任何其他环境)的顶层定义,因此您不能在该环境中pickle和unpickleSpecialSnowflake
实例相关问题 更多 >
编程相关推荐