简体   繁体   English

在子包中加载一个pickle文件->模块'__main__'错误

[英]load a pickle file in a sub package --> error with module '__main__'

I get this error message when I try to load a pickle file: 尝试加载泡菜文件时收到以下错误消息:

Can't get attribute 'CustomNMF' on <module '__main__' from '/Users/cricket/Documents/ONline classes/A6 - Stackoverflow/Tag-generator-Stackoverflow/app.py'

My question where am I missing an import CustomNMF or any other thing that would make it work ? 我的问题是我在哪里缺少import CustomNMF或任何其他可以使它起作用的东西?

It's a flask project I'm working on, the structure is: 这是我正在研究的烧瓶项目,结构为:

project folders: 项目文件夹:

+ app.py
+ app (sub-folder)
++ __init__.py
++ views.py
++ CustomNMF.py
++ and other things...

Content of app.py : app.py内容:

from app import app
if __name__ == "__main__":
    app.run(debug=True)

content of __init__.py __init__.py内容

from flask import Flask
app = Flask(__name__)
from app import views

Content of views.py : views.py内容:

....
from app.CustomNMF import CustomNMF
customNMF = CustomNMF()  #That works fine !

def loadpickle(filename = 'customNMF.pickle'):
    with open(os.path.join(os.getcwd(),'app', filename), 'rb') as f:
        return pickle.load(f)

customNMF = loadpickle('customNMF.pickle')  #Line that is generating the error :-(
....

Content of CustomNMF.py : (A simple copy paste from the notebook used to create the class ) CustomNMF.py内容CustomNMF.py用于创建class的笔记本中的简单复制粘贴)

class CustomNMF(BaseEstimator, TransformerMixin):
    def __init__(self,popularitytags=None, **params): 
        self.popularitytags = popularitytags
        .......

I'm sure it's obvious for most of you, and the solution is just a small detail I'm missing. 我相信这对于大多数人来说都是显而易见的,而解决方案只是我所缺少的一小部分。 Any help will be appreciated. 任何帮助将不胜感激。

I think I found the issue: I saved the customNMF trained from the python notebook. 我想我找到了问题:我保存了从python笔记本训练而来的customNMF I had to slightly modify the classes in the dask version not not call some unnecessary packages (cross validation and metrics from sklearn ) for the website version. 我不得不稍微修改一下dask版本中的类,而不是为网站版本调用一些不必要的包(来自sklearn交叉验证和指标)。 I assume those minor modifications are the source of the problem. 我认为这些小修改是问题的根源。

Now the CustomNMF is trained and pickled for later usage by the website server. 现在,CustomNMF已经过培训和腌制,供网站服务器以后使用。 Not sure it's the best approach, but it's working! 不确定这是最好的方法,但它是否有效!

If anyone as good lecture about packages structure and how to articulate them (simplified version ;-) just for me), I would really appreciate. 如果有人能很好地介绍软件包的结构以及如何表达它们(简化版;-),那我将非常感激。

Thanks 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python pickle:ImportError:没有名为__main__的模块 - Python pickle: ImportError: No module named __main__ ModuleNotFoundError:没有名为&#39;__main __。web_app&#39;的模块; 从子文件夹导入包时,&#39;__ main__&#39;不是包 - ModuleNotFoundError: No module named '__main__.web_app'; '__main__' is not a package while importing package from sub folder pickle/joblib AttributeError: 模块 &#39;__main__&#39; 在 pytest 中没有属性 &#39;thing&#39; - pickle/joblib AttributeError: module '__main__' has no attribute 'thing' in pytest Pickle 序列化:模块 &#39;__main__&#39; 没有属性 &#39;tokenize&#39; - Pickle serialization: module '__main__' has no attribute 'tokenize' ImportError:没有名为__main__的模块(Python 2.7和Pickle) - ImportError: No module named __main__ (Python 2.7 & Pickle) Python 3 - 在同一目录中导入.py文件 - ModuleNotFoundError:没有名为'__main __。char'的模块; '__main__'不是包 - Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package 我遇到一个错误“ ModuleNotFoundError:没有名为&#39;__main __。models&#39;的模块; “ __main__”不是软件包” - I'm having an error “ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package” 错误:没有名为“__main__.forms”的模块; &#39;__main__&#39; 不是 Python3 中的包 - Error: No module named '__main__.forms'; '__main__' is not a package in Python3 解决“ModuleNotFoundError: ...'__main__' is not a package”错误 - Working around "ModuleNotFoundError: ...'__main__' is not a package" error ModuleNotFoundError:没有名为'__main __。models'的模块; '__main__'不是包 - ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM