Python AttributeError: 'module' 对象没有属性(使用 cPickle 时)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3363281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:39:41  来源:igfitidea点击:

AttributeError: 'module' object has no attribute (when using cPickle)

python

提问by Harikrishnan R

I am trying to load the function in a remote environment using cPickle. But I got the error "the 'module' object has no attribute ..." . Where I really stuck is the namespace has already contain that attributes , even though it fails to load Please Help

我正在尝试使用 cPickle 在远程环境中加载该函数。但我收到错误“'模块'对象没有属性......”。我真正坚持的地方是命名空间已经包含该属性,即使它无法加载请帮助

import inspect
import cPickle as pickle
from run import run


def get_source(func): 
 sourcelines = inspect.getsourcelines(func)[0]
 sourcelines[0] = sourcelines[0].lstrip()
 return "".join(sourcelines)

def fun(f):
 return f()

def fun1():
 return 10 

funcs = (fun, fun1) 

sources = [get_source(func) for func in funcs]

funcs_serialized = pickle.dumps((fun.func_name,sources),0)

args_serialized = pickle.dumps(fun1,0) 

#Creating the Environment where fun & fun1 doesnot exist 
del globals()['fun']
del globals()['fun1']

r = run() 

r.work(funcs_serialized,args_serialized) 

Here is run.py

这是run.py

import cPickle as pickle

class run():
 def __init__(self):
  pass

 def work(self,funcs_serialized,args_serialized):

  func, fsources = pickle.loads(funcs_serialized)

  fobjs = [compile(fsource, '<string>', 'exec') for fsource in fsources]

    #After eval fun and fun1 should be there in globals/locals
  for fobj in fobjs:
   try: 
    eval(fobj)
    globals().update(locals())
   except:
    pass

  print "Fun1 in Globals: ",globals()['fun1']   
  print "Fun1 in locals: ",locals()['fun1']   
  arg = pickle.loads(args_serialized)

The error is

错误是

Fun1 in Globals:  <function fun1 at 0xb7dae6f4>
Fun1 in locals:  <function fun1 at 0xb7dae6f4>
Traceback (most recent call last):
  File "fun.py", line 32, in <module>
    r.work(funcs_serialized,args_serialized) 
  File "/home/guest/kathi/python/workspace/run.py", line 23, in work
    arg = pickle.loads(args_serialized)
AttributeError: 'module' object has no attribute 'fun1'

采纳答案by Yariv

I found this link helpful: http://stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.html

我发现这个链接很有帮助:http: //stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.html

It gives two solutions. The better solution is to add to the head of the loading module (or __main__):

它给出了两种解决方案。更好的解决方案是添加到加载模块的头部(或__main__):

from myclassmodule import MyClass

But I think a better solution should exist.

但我认为应该存在更好的解决方案。

回答by John La Rooy

The module name of the function is saved into the pickle, when you are doing the loadsit is looking for fun1in __main__or whereever it was originally

该功能的模块名称保存到泡菜,当你正在做loads的是寻找fun1__main__或徘徊无论它原来

回答by Jeremy Brown

From http://docs.python.org/library/pickle.html#what-can-be-pickled-and-unpickled:

来自http://docs.python.org/library/pickle.html#what-c​​an-be-pickled-and-unpickled

Note that functions (built-in and user-defined) are pickled by “fully qualified” name reference, not by value. This means that only the function name is pickled, along with the name of module the function is defined in. Neither the function's code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised.

请注意,函数(内置和用户定义的)由“完全限定”名称引用而不是值来腌制。这意味着只有函数名和定义函数的模块的名称被腌制。函数的代码和它的任何函数属性都不会被腌制。因此定义模块必须在 unpickling 环境中可导入,并且模块必须包含命名对象,否则将引发异常。

You deleted the reference to fun1 in the module that defines fun1, thus the error.

您删除了定义 fun1 的模块中对 fun1 的引用,因此出现错误。

回答by HVNSweeting

try to add

尝试添加

from your_first_module import fun,fun1 

into run.py

进入run.py