Forums

No module named 'tensorflow.python.keras

Hi guys,

Im trying to implement script on this page: https://medium.com/@guymodscientist/image-prediction-with-10-lines-of-code-3266f4039c7a

I have installed all the packages but getting this error: No module named 'tensorflow.python.keras'

even with just 2 lines of code:

from imageai.Prediction import ImagePrediction

import os

Im getting the same error

Thank you for the help

You have to be a bit more specific when you install modules on PythonAnywhere than you would on your own computer -- because we have many versions of Python installed (2.7, 3.3, 3.4, 3.5, 3.6) and they all have their own collections of installed modules, pip3 might not install it for the version you want to run it in.

So, for example, if you're running your code in Python 3.5, and you want to install the package foo, you should use the bash command

pip3.5 install --user foo

...and then when you run the code, make sure you select a Python 3.5 console from the "Consoles" page, or run python3.5 from bash.

There's more information about installing modules on this help page.

I am trying to leverage tensorflow as well. I am completely new to PythonAnywhere. I have a Flask app running successfully in a localhost. Before the flask app I load a previously trained keras model. I have created a PythonAnywhere flask app and open it, the first thing I need to do is load the model (I have uploaded to PythonAnywhere). In order to load the model I need: from tensorflow.python.keras.models import load_model. When I try to load tensorflow as tf first, I see the execution saying it is loading Python 3.7 (even though I created as Python 3.6 and I know tensorflow does not run on Python 3.7) and then it says that tensorflow does not exist. I use the above on the bash command for Python 3.6 and it says tensorflow is already loaded. I am guessing I have a conceptual error, but appreciate any guidance that can get me going.

Are you running the code from the editor? You don't need to do that for a website -- once you've configured it on the "Web" page, it's automatically started and set up to receive requests without you needing to do anything else. And when you run something in the editor, it will use the default Python version for your account, which is 3.7.

All of that said, Tensorflow doesn't work well inside website code on PythonAnywhere right now; the good news is that as you're using Keras, you can just switch to the Theano backend and it will work fine -- more details here.

OK, thanks, so I guess I should not try and run the code on the editor. I can load all the Flask utilities I need. I just need load_model (from TF Keras) (to load the model that I already trained and already uploaded to python anywhere) and model.predict (from TF Keras). Yes, and saw that post (which I will do). But, how do I get the packages so I can use them? Do I not need to:

from tensorflow.python.keras.models import load_model import pandas as pd import numpy as np import tensorflow as tf

From flask I had to import "request" to get my Post and Get working.......

The two sets of commands (in addition to np and pd) that I need to get working in my flask app (and work fine on my local host are:

comment field (load trained model) MODEL_PATH = 'Churn_model_1' flask_model = load_model(MODEL_PATH) flask_model._make_predict_function()

And

(comment field) make prediction after I process the input data on the web page using np and pd preds = flask_model.predict(processed_features).flatten()

I am not sure how I shold proceed. Should I stop what I am doing in the editor and do something else? Should I start learning how to create the virtual environment and load the needed packages in there?

Thanks in advance

When I put in the editor: from keras import load_model

I get: Traceback (most recent call last): File "/home/jmstadt/mysite/flask_app.py", line 8, in <module> from keras import load_model File "/usr/lib/python3.7/site-packages/keras/init.py", line 3, in <module> from . import utils File "/usr/lib/python3.7/site-packages/keras/utils/init.py", line 6, in <module> from . import conv_utils File "/usr/lib/python3.7/site-packages/keras/utils/conv_utils.py", line 9, in <module> from .. import backend as K File "/usr/lib/python3.7/site-packages/keras/backend/init.py", line 89, in <module> from .tensorflow_backend import * File "/usr/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 5, in <module> import tensorflow as tf ModuleNotFoundError: No module named 'tensorflow'

Which per your above does using the editor always revert to Python 3.7 even though I started by setting up the HelloWorld web page per the instructions here using 3.6 https://blog.pythonanywhere.com/121/

Thanks in advance for a recommended strategy (and for a novice like myself)

The thing is, you don't need to run it in the editor -- it's already running (using Python 3.6) because you've set it up on the "Web" page.

If you want to run it from the editor just to do a syntax check or for something like that, you can tell the editor to use Python 3.6 by using the technique on this help page.

My application runs well on local host but when a try to run it over cloud platform it displays the following error

Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/home/ateetk/mysite/Crop_Prediction_And_Guidance-master/app.py", line 3, in <module>
    import keras
ModuleNotFoundError: No module named 'keras'

[edit by admin: formatting]

You can install packages using the instructions on this help page. That said, if you're doing machine learning stuff, and especially if you're using Tensorflow, I'd recommend using Python 3.7 rather than 3.8 -- Keras and Tensorflow are pre-installed on PythonAnywhere for that Python version, so you won't need to install them yourself, and Tensorflow in particular works much better with 3.7.

Hi I uploaded a model then created file for fetching prediction from model when I run that file from bash using python command(by calling function)it works perfectly but when I try it from editor(run) it gives me an error:

' pythonmodulenotfounderror no module named 'tensorflow'

And it doesn't work in my web app too.when I checked sys.executable for both in editor it shows me "/usr/bin/python3.8" And in bash it shows me "/usr/bin/python". There is nothing in my error logs
Thanks in advance.

You have to decide what version of python to run, and it has to match the version keras is installed for. See: How can I use a different version of Python for the Save & Run button? | PythonAnywhere help

Thank you it worked!! in editor but not in web app.i am creating a chatbot ,i have created a file of functions for extracting prediction from model and im importing it in my routes fiile(and there is no import error as i tried calling function from editor console). in routes.py file i created a route for prediction


@app.route('/get')
def get_bot_response():
     msg = str(request.args.get('msg'))
     res= chatbot_response(msg)
     # res = msg     to check if javascript and request for http works
     return str(res)

i have checked my javascript works properly by excecuting commented code(res=msg), and my model working perfectly in editor console [ i tried res= chatbot_response("hello") and other examples] . I am not able figure out why its not working and there is no errors in error logs so it would be great help if you could help me with it. link for functions file code

Hi, there are no error logs since the web app crashed before it could log (you can check it in the server logs). Tensorflow would not work in the web app, there is a workaround, please check this help page: https://help.pythonanywhere.com/pages/MachineLearningInWebsiteCode/

Ohh thanks.. how can I switch to theano back-end

You need to use keras<=2.3.1. Then follow How to switch Backend with Keras (from TensorFlow to Theano)

Im using keras 2.3.1 and I changed keras.json file I replaced TensorFlow with Theano . There was an error that no module named Theano so I installed it using pip . But my website still not working.

Does anything get written to the server log (not the error log) when the site has problems? I can see some errors from earlier on today, but nothing since it was restarted at 17:15:23 (about ten minutes before you posted above).

2021-03-16 03:35:29 *** Starting uWSGI 2.0.17.1 (64bit) on [Tue Mar 16 03:35:24 2021] *** 2021-03-16 03:35:29 compiled with version: 5.4.0 20160609 on 07 May 2020 19:40:56 2021-03-16 03:35:29 os: Linux-5.4.0-1029-aws #30 SMP Tue Nov 10 18:03:06 UTC 2020 2021-03-16 03:35:29 nodename: green-liveweb4 2021-03-16 03:35:29 machine: x86_64 2021-03-16 03:35:29 clock source: unix 2021-03-16 03:35:29 pcre jit disabled 2021-03-16 03:35:29 detected number of CPU cores: 2 2021-03-16 03:35:29 current working directory: (unreachable)/etc/uwsgi/vassals 2021-03-16 03:35:29 detected binary path: /usr/local/bin/uwsgi 2021-03-16 03:35:29 *** dumping internal routing table *** 2021-03-16 03:35:29 [rule: 0] subject: path_info regexp: .svgz$ action: addheader:Content-Encoding:gzip 2021-03-16 03:35:29 *** end of the internal routing table *** 2021-03-16 03:35:29 chdir() to /home/yashvardhanv/ 2021-03-16 03:35:29 limiting number of processes to 60... 2021-03-16 03:35:29 your processes number limit is 60 2021-03-16 03:35:29 your memory page size is 4096 bytes 2021-03-16 03:35:29 detected max file descriptor number: 123456 2021-03-16 03:35:29 building mime-types dictionary from file /etc/mime.types... 2021-03-16 03:35:29 552 entry found 2021-03-16 03:35:29 lock engine: pthread robust mutexes 2021-03-16 03:35:29 thunder lock: disabled (you can enable it with --thunder-lock) 2021-03-16 03:35:29 uwsgi socket 0 bound to UNIX address /var/sockets/yashvardhanv.pythonanywhere.com/socket fd 3 2021-03-16 03:35:29 Python version: 3.7.5 (default, Nov 14 2019, 22:26:37) [GCC 5.4.0 20160609] 2021-03-16 03:35:29 *** Python threads support is disabled. You can enable it with --enable-threads *** 2021-03-16 03:35:29 Python main interpreter initialized at 0x11aa460 2021-03-16 03:35:29 your server socket listen backlog is limited to 100 connections 2021-03-16 03:35:29 your mercy for graceful operations on workers is 60 seconds 2021-03-16 03:35:29 setting request body buffering size to 65536 bytes 2021-03-16 03:35:29 mapped 334256 bytes (326 KB) for 1 cores 2021-03-16 03:35:29 *** Operational MODE: single process *** 2021-03-16 03:35:29 initialized 38 metrics 2021-03-16 03:35:29 /usr/lib/python3.7/site-packages/flask_sqlalchemy/init.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.#012 'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and ' 2021-03-16 03:35:29 2021-03-16 03:35:29.808824: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA 2021-03-16 03:35:29 2021-03-16 03:35:29.816838: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2300045000 Hz 2021-03-16 03:35:29 2021-03-16 03:35:29.817116: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x52a4e50 executing computations on platform Host. Devices: 2021-03-16 03:35:29 2021-03-16 03:35:29.817143: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version 2021-03-16 03:35:29 WSGI app 0 (mountpoint='') ready in 5 seconds on interpreter 0x11aa460 pid: 1 (default app) 2021-03-16 03:35:29 *** uWSGI is running in multiple interpreter mode *** 2021-03-16 03:35:29 gracefully (RE)spawned uWSGI master process (pid: 1) 2021-03-16 03:35:29 spawned uWSGI worker 1 (pid: 23, cores: 1) 2021-03-16 03:35:29 metrics collector thread started 2021-03-16 03:35:29 spawned 2 offload threads for uWSGI worker 1 2021-03-16 03:36:27 announcing my loyalty to the Emperor... 2021-03-16 03:41:40 Tue Mar 16 03:41:39 2021 - *** HARAKIRI ON WORKER 1 (pid: 23, try: 1) *** 2021-03-16 03:41:40 Tue Mar 16 03:41:39 2021 - HARAKIRI !!! worker 1 status !!! 2021-03-16 03:41:40 Tue Mar 16 03:41:39 2021 - HARAKIRI [core 0] 10.0.0.93 - GET /get?msg=Hello since 1615865798 2021-03-16 03:41:40 Tue Mar 16 03:41:39 2021 - HARAKIRI !!! end of worker 1 status !!! 2021-03-16 03:41:40 DAMN ! worker 1 (pid: 23) died, killed by signal 9 :( trying respawn ... 2021-03-16 03:41:40 Respawned uWSGI worker 1 (new pid: 29) 2021-03-16 03:41:40 spawned 2 offload threads for uWSGI worker 1

This is getting written when I reload website from web dashboard

It looks like it is still using TensorFlow.