<p>我可以从你的问题中看出,你可能正在做类似的事情,使用一个试图pickle类实例的类方法。这样做是不明智的,如果你这样做的话……那么在类的外部使用<code>pkl.dump</code>就更明智了(这里<code>pkl</code>是<code>pickle</code>或<code>dill</code>等等)。但是,它<em>仍可以</em>使用此设计,请参见以下内容:</p>
<pre><code>>>> class Thing(object):
... def pickle_myself(self, pkl_file_path):
... with open(pkl_file_path, 'w+') as f:
... pkl.dump(self, f, protocol=2)
...
>>> import dill as pkl
>>>
>>> t = Thing()
>>> t.pickle_myself('foo.pkl')
</code></pre>
<p>然后重新启动。。。</p>
<pre><code>Python 2.7.10 (default, Sep 2 2015, 17:36:25)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> f = open('foo.pkl', 'r')
>>> t = dill.load(f)
>>> t
<__main__.Thing object at 0x1060ff410>
</code></pre>
<p>如果你有一个复杂得多的类,我相信你会遇到麻烦,特别是如果这个类使用了位于同一目录中的另一个文件。</p>
<pre><code>>>> import dill
>>> from bar import Zap
>>> print dill.source.getsource(Zap)
class Zap(object):
x = 1
def __init__(self, y):
self.y = y
>>>
>>> class Thing2(Zap):
... def pickle_myself(self, pkl_file_path):
... with open(pkl_file_path, 'w+') as f:
... dill.dump(self, f, protocol=2)
...
>>> t = Thing2(2)
>>> t.pickle_myself('foo2.pkl')
</code></pre>
<p>然后重新启动</p>
<pre><code>Python 2.7.10 (default, Sep 2 2015, 17:36:25)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> f = open('foo2.pkl', 'r')
>>> t = dill.load(f)
>>> t
<__main__.Thing2 object at 0x10eca8090>
>>> t.y
2
>>>
</code></pre>
<p>好吧…开枪,那也行。你必须发布你的代码,这样我们就可以看到你使用的<code>dill</code>(和<code>pickle</code>)失败的模式是什么。我知道让一个模块导入另一个未“安装”的模块(即在某个本地目录中),并且期望序列化“正常工作”并不适用于所有情况。</p>
<p>参见<code>dill</code>问题:
<a href="https://github.com/uqfoundation/dill/issues/128" rel="nofollow noreferrer">https://github.com/uqfoundation/dill/issues/128</a>
<a href="https://github.com/uqfoundation/dill/issues/129" rel="nofollow noreferrer">https://github.com/uqfoundation/dill/issues/129</a>
所以这个问题:
<a href="https://stackoverflow.com/questions/32363312/why-dill-dumps-external-classes-by-reference-no-matter-what">Why dill dumps external classes by reference, no matter what?</a>
一些失败的例子和潜在的解决办法。</p>
<p><strong>编辑关于更新的问题:</p>
<p>我看不出你的问题。从命令行运行、从解释器导入(<code>import test_serialization</code>)、在解释器中运行脚本(如下所示,并在步骤3-5中指明)都可以工作。这让我觉得你可能用的是老版本的<code>dill</code>?</p>
<pre><code>>>> import os
>>> import make_persist_load
>>>
>>> MAKE_AND_PERSIST = False #True
>>> LOAD = (not MAKE_AND_PERSIST)
>>>
>>> cwd = os.getcwd()
>>> store_path = os.path.join(cwd, "test_stored_env.pkl")
>>>
>>> if MAKE_AND_PERSIST == True:
... make_persist_load.make_env_and_persist()
...
>>> if LOAD == True:
... loaded_env = make_persist_load.load_env(store_path)
...
>>>
</code></pre>
<p><strong>根据评论中的讨论编辑</strong>:</p>
<p>看起来这可能是Windows的问题,因为这似乎是唯一出现错误的操作系统。</p>
<p><strong>完成一些工作后编辑(请参见:<a href="https://github.com/uqfoundation/dill/issues/140" rel="nofollow noreferrer">https://github.com/uqfoundation/dill/issues/140</a>):</p>
<p>使用这个最小的例子,我可以在Windows上重现同样的错误,而在MacOSX上它仍然可以工作</p>
<pre><code># test.py
class Environment():
def __init__(self):
pass
</code></pre>
<p>以及</p>
<pre><code># doit.py
import test
import dill
env = test.Environment()
path = "test.pkl"
with open(path, 'w+') as f:
dill.dump(env, f)
with open(path, 'rb') as _f:
_env = dill.load(_f)
print _env
</code></pre>
<p>但是,如果使用<code>open(path, 'r') as _f</code>,它在Windows和MacOSX上都可以工作。所以看起来Windows上的<code>__import__</code>比非Windows系统对文件类型更敏感。尽管如此,抛出一个<code>ImportError</code>还是很奇怪……但这一个小小的改变应该会让它起作用。</p>