我在 my_module 中定义了一个类 MyClass。 MyClass 有一个方法 pickle_myself 可以 pickle 相关类的实例:
def pickle_myself(self, pkl_file_path):
with open(pkl_file_path, 'w+') as f:
pkl.dump(self, f, protocol=2)
我已确保 my_module 在 PYTHONPATH 中。在解释器中,执行 __import__('my_module') 工作正常:
>>> __import__('my_module')
<module 'my_module' from 'A:\my_stuff\my_module.pyc'>
但是,当最终加载文件时,我得到:
File "A:\Anaconda\lib\pickle.py", line 1128, in find_class
__import__(module)
ImportError: No module named my_module
我确定的一些事情:
我没有更改 my_module.py 的位置 ( Python pickling after changing a module's directory )
我尝试使用 dill 代替,但仍然出现相同的错误 ( More on python ImportError No module named )
编辑——重现错误的玩具示例:
示例本身分布在一堆文件中。
首先,我们有模块ball(存储在名为ball.py 的文件中):
class Ball():
def __init__(self, ball_radius):
self.ball_radius = ball_radius
def say_hello(self):
print "Hi, I'm a ball with radius {}!".format(self.ball_radius)
然后,我们有了模块test_environment:
import os
import ball
#import dill as pkl
import pickle as pkl
class Environment():
def __init__(self, store_dir, num_balls, default_ball_radius):
self.store_dir = store_dir
self.balls_in_environment = [ball.Ball(default_ball_radius) for x in range(num_balls)]
def persist(self):
pkl_file_path = os.path.join(self.store_dir, "test_stored_env.pkl")
with open(pkl_file_path, 'w+') as f:
pkl.dump(self, f, protocol=2)
然后,我们有一个模块,它具有创建环境、持久化和加载它们的功能,称为 make_persist_load:
import os
import test_environment
#import pickle as pkl
import dill as pkl
def make_env_and_persist():
cwd = os.getcwd()
my_env = test_environment.Environment(cwd, 5, 5)
my_env.persist()
def load_env(store_path):
stored_env = None
with open(store_path, 'rb') as pkl_f:
stored_env = pkl.load(pkl_f)
return stored_env
然后我们有一个脚本将它们放在一起,在 test_serialization.py 中:
import os
import make_persist_load
MAKE_AND_PERSIST = 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)
为了方便使用这个玩具示例,I have put it all up on in a Github repository that simply needs to be cloned into your directory of choice. .请参阅包含说明的 README,我也将其复制在这里:
说明:
1) 将存储库克隆到目录中。
2) 将存储库目录添加到 PYTHONPATH。
3) 打开test_serialization.py,将变量MAKE_AND_PERSIST设置为True。在解释器中运行脚本。
4) 关闭之前的解释器实例,并启动一个新的。在 test_serialization.py 中,将 MAKE_AND_PERSIST 更改为 False,这将以编程方式将 LOAD 设置为 True。在解释器中运行脚本,导致 ImportError: No module named test_environment。
5) 默认情况下,测试设置为使用 dill,而不是 pickle。要更改此设置,请进入 test_environment.py 和 make_persist_load.py,根据需要更改导入。
编辑:切换到 dill '0.2.5.dev0' 后,dill.detect.trace(True) 输出
C2: test_environment.Environment
# C2
D2: <dict object at 0x000000000A9BDAE8>
C2: ball.Ball
# C2
D2: <dict object at 0x000000000AA25048>
# D2
D2: <dict object at 0x000000000AA25268>
# D2
D2: <dict object at 0x000000000A9BD598>
# D2
D2: <dict object at 0x000000000A9BD9D8>
# D2
D2: <dict object at 0x000000000A9B0BF8>
# D2
# D2
编辑:玩具示例在 Mac/Ubuntu(即类 Unix 系统?)上运行时运行良好。它只在 Windows 上失败。
最佳答案
我可以从你的问题中看出你可能正在做这样的事情,用一个试图 pickle 类实例的类方法。这样做是不明智的,如果你这样做的话……在类外部使用 pkl.dump 更明智(其中 pkl 是 pickle 或 dill 等)。但是,它可以仍然适用于这种设计,见下文:
>>> 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')
然后重新启动...
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>
如果您有一个复杂得多的类(我相信您有),那么您很可能会遇到麻烦,尤其是当该类使用位于同一目录中的另一个文件时。
>>> 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')
然后重新启动...
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
>>>
好吧……开枪吧,这也行。你必须发布你的代码,这样我们才能看到你使用的是什么模式,dill(和 pickle)失败了。我知道让一个模块导入另一个未“安装”的模块(即在某个本地目录中)并期望序列化“正常工作”并不适用于所有情况。
查看 dill 问题:
https://github.com/uqfoundation/dill/issues/128
https://github.com/uqfoundation/dill/issues/129
这个问题:
Why dill dumps external classes by reference, no matter what?
一些失败的例子和潜在的解决方法。
EDIT 关于更新的问题:
我没有看到你的问题。从命令行运行,从解释器导入 (import test_serialization),并在解释器中运行脚本(如下所示,并在您的步骤 3-5 中指出)都可以。这使我认为您可能正在使用旧版本的 dill?
>>> 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)
...
>>>
编辑基于评论中的讨论:
看起来这可能是 Windows 的问题,因为这似乎是唯一出现错误的操作系统。
编辑一些工作后(参见:https://github.com/uqfoundation/dill/issues/140):
使用这个最小的示例,我可以在 Windows 上重现相同的错误,而在 MacOSX 上它仍然有效……
# test.py
class Environment():
def __init__(self):
pass
和
# 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
但是,如果您使用 open(path, 'r') as _f,它可以在 Windows 和 MacOSX 上运行。所以看起来 Windows 上的 __import__ 比非 Windows 系统对文件类型更敏感。尽管如此,抛出一个 ImportError 还是很奇怪……但是这个小改动应该可以让它工作。
关于python - `pickle` : yet another `ImportError: No module named my_module` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968685/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在学习如何在我的Ruby代码中使用Module.prepend而不是alias_method_chain,我注意到有些人使用send调用它(example):ActionView::TemplateRenderer.send(:prepend,ActionViewTemplateRendererWithCurrentTemplate)而其他人直接调用它(example):ActionView::TemplateRenderer.prepend(ActionViewTemplateRendererWithCurrentTemplate)而且,虽然我还没有看到任何人使用这种风格,但我从
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
我想解析一个已经存在的.mid文件,改变它的乐器,例如从“acousticgrandpiano”到“violin”,然后将它保存回去或作为另一个.mid文件。根据我在文档中看到的内容,该乐器通过program_change或patch_change指令进行了更改,但我找不到任何在已经存在的MIDI文件中执行此操作的库.他们似乎都只支持从头开始创建的MIDI文件。 最佳答案 MIDIpackage会为您完成此操作,但具体方法取决于midi文件的原始内容。一个MIDI文件由一个或多个音轨组成,每个音轨是十六个channel中任何一个上的
本文主要介绍在使用Selenium进行自动化测试或者任务时,对于使用了iframe的页面,如何定位iframe中的元素文章目录场景描述解决方案具体代码场景描述当我们在使用Selenium进行自动化测试的时候,可能会遇到一些界面或者窗体是使用HTML的iframe标签进行承载的。对于iframe中的标签,如果直接查找是无法找到的,会抛出没有找到元素的异常。比如近在咫尺的例子就是,CSDN的登录窗体就是使用的iframe,大家可以尝试通过F12开发者模式查看到的tag_name,class_name,id或者xpath来定位中的页面元素,会抛出NoSuchElementException异常。解决
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
ValidPalindromeGivenastring,determineifitisapalindrome,consideringonlyalphanumericcharactersandignoringcases. [#125]Example:"Aman,aplan,acanal:Panama"isapalindrome."raceacar"isnotapalindrome.Haveyouconsiderthatthestringmightbeempty?Thisisagoodquestiontoaskduringaninterview.Forthepurposeofthisproblem