I have a pickled file of my class 'Classifier', that I want to use when the user sends a request to my flask webpage. However, the code where the pickled file is loaded gives me an error. The same code works perfectly when run on the console, but gives an error (in the error log) and doesn't work on the webpage. Here is briefly how the code looks:
from flask import Flask
import pickle
class Classifier():
#class definition
classifier = None
with open('/home/saurabhshirodkar/appname/savefile', 'rb') as savefile:
classifier = pickle.load(savefile)
app = Flask(__name__)
@app.route('/')
def hello_world():
return('Hello from flask!')
And this is the error from the error log:
2018-04-27 15:36:54,334: Error running WSGI application
2018-04-27 15:36:54,338: AttributeError: Can't get attribute 'Classifier' on <module '__main__' (built-in)>
2018-04-27 15:36:54,338: File "/var/www/saurabhshirodkar_pythonanywhere_com_wsgi.py", line 16, in <module>
2018-04-27 15:36:54,339: from flask_server import app as application # noqa
2018-04-27 15:36:54,339:
2018-04-27 15:36:54,339: File "/home/saurabhshirodkar/instagive/flask_server.py", line 24, in <module>
2018-04-27 15:36:54,339: classifier = pickle.load(savefile)
Again, the above code works with the console. This error occurs when I open the webpage, and it works when I remove the pickle load code. Why is this the case, and how to solve or work around this problem?