Forums

ModuleNotFoundError

I have developed a rest API using flask to serve a Deep Learning model developed in Python3.6/tensorflow/keras. The app is using a virtual environment in which keras (and all other required modules) are installed. I specified the path to the virtual env in the app page (Web tab), as well as the working directory and the code directory.

When I just go to the API root URL, it outputs 'Test 123' as expected, however when I try to query the API, nothing happens (no reply, browser shows like the page is loading forever).

When I inspect the logs, here's what I get:

2018-02-17 17:01:44,932: Error running WSGI application
2018-02-17 17:01:44,937: ModuleNotFoundError: No module named 'keras'
2018-02-17 17:01:44,937:   File "/var/www/tixierae_pythonanywhere_com_wsgi.py", line 16, in <module>
2018-02-17 17:01:44,937:     from flask_app import app as application
2018-02-17 17:01:44,938: 
2018-02-17 17:01:44,938:   File "/home/tixierae/mysite/flask_app.py", line 5, in <module>
2018-02-17 17:01:44,938:     from keras.models import load_model

However, when I run the tixierae_pythonanywhere_com_wsgi.py file, it works and opens a console as expected. From there I am able to execute keras.models import load_model without any issue.

Any help would be much appreciated. I have reloaded the app multiple times.

Is keras a module that you installed yourself? If so, which version of python did you install it into? (vs what version of python is your webapp running?) and if you used a virtualenv, did you set the virtualenv path for your PythonAnywhere webapp config?

if keras is some folder in your source code, then perhaps your working directory is not what you think it is.

Thanks for the quick reply. Following the advice on https://help.pythonanywhere.com/pages/Flask/, I created a virtual environment in Python 3.6. (which is the version that my app is running) and installed all the required Python modules (including Keras) in it via pip. I did specify the path to the virtual env in the virtualenv section of the app configuration page (not in the wsgi.py file though, don't know if I should do that or how to do it).

In the virtual env, when I execute the wsgi.py, I don't receive any error but it doesn't open a python console anymore.

You shouldn't set anything virtualenv-related in your wsgi file. If you've configured it from the web app, that's all you need.

I don't really know what you're talking about when you say it doesn't open a python console anymore.

Section 'Debugging sys.path issues in web apps' here: https://help.pythonanywhere.com/pages/DebuggingImportError/

It reads, when running the wsgi file, if everything loads OK, it will drop you into a Python shell

When I run the file, I get no error but not Python shell is opened

Are you sure you're including the '-i'?

With the -i, it opens a shell and from there I am able to execute from keras.models import load_model without receiving any error. But it still doesn't work. When I try to submit a GET request, for example:

http://tixierae.pythonanywhere.com/predict_cnn?text=good%20movie

It looks like the page is loading forever.

The error logs:

2018-02-19 13:43:19,509: OSError: write error
2018-02-19 13:43:19,510: Error running WSGI application
2018-02-19 13:43:19,510: GeneratorExit

Server logs:

2018-02-19 13:43:19 Mon Feb 19 13:43:19 2018 - SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request / (ip 10.0.0.132) !!!
2018-02-19 13:43:19 Mon Feb 19 13:43:19 2018 - uwsgi_response_writev_headers_and_body_do(): Broken pipe [core/writer.c line 296] during GET / (10.0.0.132)
2018-02-19 13:43:19 RuntimeError: generator ignored GeneratorExit

The app works fine locally.

Have a look at our help page about that.

This seems to be unrelated to my problem. What about the OSError: write error?

It's the same thing.