1

Good day everyone. I got a module from the internet which is a module about NMT. In the module I have an import for tensorflow, but unfortunately even after the installation of tensorflow in my system using pip, I still get the error. Here is the error

from tensorflow.keras.models import load_model

ModuleNotFoundError: No module named 'tensorflow'

The module hello_app.py is below:

from flask import Flask
from flask import request

from flask import jsonify

import uuid
import os
from tensorflow.keras.models import load_model
import numpy as np

EXPECTED = {
  "cylinders":{"min":3,"max":8},
  "displacement":{"min":68.0,"max":455.0},
  "horsepower":{"min":46.0,"max":230.0},
  "weight":{"min":1613,"max":5140},
  "acceleration":{"min":8.0,"max":24.8},
  "year":{"min":70,"max":82},
  "origin":{"min":1,"max":3}

}

# Load neural network when Flask boots up
model = load_model(os.path.join("../dnn/","mpg_model.h5"))

@app.route('/api/mpg', methods=['POST'])
def calc_mpg():
    content = request.json
    errors = []
    
    for name in content:
        if name in EXPECTED:
            expected_min = EXPECTED[name]['min']
            expected_max = EXPECTED[name]['max']
            value = content[name]
            if value < expected_min or value > expected_max:
                errors.append(f"Out of bounds: {name}, has value of: {value}, but should be between {expected_min} and {expected_max}.")
        else:
            errors.append(f"Unexpected field: {name}.")

# Check for missing input fields
           for name in EXPECTED:
               if name not in content:
                   errors.append(f"Missing value: {name}.")



  if len(errors) <1:
        x = np.zeros( (1,7) )


     # Predict
   x[0,0] = content['cylinders']
   x[0,1] = content['displacement'] 
   x[0,2] = content['horsepower']
   x[0,3] = content['weight']
   x[0,4] = content['acceleration'] 
   x[0,5] = content['year']
   x[0,6] = content['origin']



   pred = model.predict(x)
   mpg = float(pred[0])
   response = {"id":str(uuid.uuid4()),"mpg":mpg,"errors":errors}
   else:
       response = {"id":str(uuid.uuid4()),"errors":errors}

   print(content['displacement'])
   return jsonify(response)

   if __name__ == '__main__':
       app.run(host= '0.0.0.0',debug=True)


   

Please I would really appreciate your answers. Thank you.

This is the github repo where I got the code https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_13_01_flask.ipynb

Kyom Dogo
  • 67
  • 1
  • 6
  • Can you please edit the question with proper indentation and formatting in the python code? – ranka47 Dec 18 '20 at 15:04
  • 1
    Try checking if tensorflow is installed by this command `python -c 'import tensorflow as tf; print(tf.__version__)' ` – ranka47 Dec 18 '20 at 15:08
  • @ranka47 the code is now formatted – Kyom Dogo Dec 18 '20 at 16:24
  • Are you sure you're running this script in the same Python installation in which you installed TensorFlow? It's possible you have more than one. Perhaps take a look at [this question and its answer](https://stackoverflow.com/questions/63387372/terminal-claims-no-pyperclip-module-found-but-that-ive-already-installed-pyperc/63387522) for what might be a similar problem. – CrazyChucky Dec 18 '20 at 17:34
  • @CrazyChucky thank you for your response. Sir when I type where python on my command line I have two directories. The first one is stored here – Kyom Dogo Dec 19 '20 at 00:08
  • @CrazyChucky thank you for your response. Sir, when I use the pip command, it shows the requirements have been satisfied, showing me the directory in which it is stored. When I check the directory I also find the other library packages there, so I dont know the problem. Sir, please I am a novice, please do not be angry if I dont understand. Thank you – Kyom Dogo Dec 19 '20 at 00:18
  • It's fine, and you don't have to call me sir. Did you try ranka47's command line suggestion? If so, what happened? – CrazyChucky Dec 19 '20 at 00:30
  • 1
    When I typed it in the cmd, it gave me some errors: On entry to DGEBAL parameter number 3 had an illegal value. The next line it says: on entry to DGEHRD parameter 2 had an illegal value. The next line it says: On entry to DOGHR DORGQR parameter number 2 had an illegal value. On the next line it says: On entry to DHSEQR parameter 4 had an illagal parameter. Then it gives a runtime error which says that the current numpy installation which is at (C:\\Users\\Kyom\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\numpy\\__init__.py) fity check due to a bug in the windows runtime. – Kyom Dogo Dec 19 '20 at 02:35
  • @CrazyChucky the response is above – Kyom Dogo Dec 19 '20 at 02:35
  • Does this answer your question? [How do you fix "runtimeError: package fails to pass a sanity check" for numpy and pandas?](https://stackoverflow.com/questions/64654805/how-do-you-fix-runtimeerror-package-fails-to-pass-a-sanity-check-for-numpy-an) – CrazyChucky Dec 19 '20 at 03:05
  • Good day sir. The previous link you sent about python version, I tried them, but it gives the same version of 3.8.3. In the terminal, I tried python --version it showed 3.8.3. In the script I used sys.version it still gave 3.8.3, so I dont know why the tensorflow is not been acknowledged considering it has only one version. Thank you for your response – Kyom Dogo Dec 19 '20 at 12:13

1 Answers1

0

To avoid package or version conflict, one can use virtual environment.

pip install virtualenv
virtualenv -p /usr/bin/python3 tf
source tf/bin/activate 
tf$ pip install tensorflow

If you have Anaconda or conda

#Set Up Anaconda Environments
conda create --name tf python=3

#Activate the new Environment
source activate tf
tf$pip install tensorflow