[英]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.