Unable to import tensorflow

Hello community,

I am deploying my first app and have worked through a lot of debugging but have reached an problem that I am unable to solve. I have read several other posts here about the same issue but none of the solutions have been successful for me.

I keep getting this error:

ModuleNotFoundError: No module named 'tensorflow'

I have added tensorflow to both my requirements and packages .txt files both with and without the version number I am running on my local machine.

I am running Python 3.9.16 with tensorflow 2.11.0.

Here is a link to my public repot on GitHub where my notebook and files are located.

Any help would be very much appreciated :blush:

Welcome to the community!
You dont’t need the packages.txt for tensorflow, delete it.
The tensorflow version should be available for this python version.
Is tensorflow installed on your local machine?
What do you get with:

pip show tensorflow

Thank you Franky!

I will get rid of the packages.txt👍

Here is what I get:

Name: tensorflow

Version: 2.11.0

This is the version I have specified in my requirements, but maybe I am not labeling it correctly?

This app works from streamlit on my local machine but not from streamlit online…

You cannot install operator, it is part of the starndard library. And you are not using it in your program anyway.

Thank you Goyo!

I will make that change.

Do you have any insight on the Tensorflow issue?

You mean this one?

ModuleNotFoundError: No module named 'tensorflow'

It didn’t happen when I deployed your code.

Thank you again Goyo!

I appreciate you taking the time to run the app on your own :blush:

I am able to do streamlit run in my terminal and the app works fine!

However, when I run the app from my Github on streamlit.io from my apps list I keep getting package errors. I have pasted them below.

I am still pretty new at this so I am not sure how to point it in the right direction beyond the requirements.txt file.

Any advice would be very much appreciated!

Traceback (most recent call last):
File “/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in _run_script
exec(code, module.dict)
File “/app/hotdogathon/hotdog.py”, line 8, in
from IPython.display import Image
ModuleNotFoundError: No module named ‘IPython’

Updating the app files has failed: git pull, cmd: sudo -u appuser GIT_SSH_COMMAND=‘ssh -i /home/appuser/.ssh/id_github -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o “LogLevel ERROR”’ git fetch origin && sudo -u appuser git reset --hard origin/main: error: exit status 128: error: unable to create file .streamlit/config.toml: Permission denied
fatal: Could not reset index file to revision ‘origin/main’.strong text

Uncaught app exception
Traceback (most recent call last):
File “/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in _run_script
exec(code, module.dict)
File “/app/hotdogathon/hotdog.py”, line 7, in
import tensorflow as tf
ModuleNotFoundError: No module named ‘tensorflow’

Never mind, I got it to work!!!

I realized my app had not been updating as I made changes to it in Github (I probably should have known that). So I deleted it and added it again and it works!!!

Thanks again for your help @Goyo!!!

import streamlit as st
import tensorflow as tf

st.set_option(‘deprecation.showfileUploaderEncoding’, False)
@st.cache(allow_output_mutation=True)

def load_model():
model = tf.keras.models.load_model(‘/Users/snehagupta/model.h5’)
return model

model = load_model()
st.write(“# Crack Detection”)

file = st.file_uploader(“Please upload a surface image”, type=[“jpg”, “png”])

from PIL import Image, ImageOps
import numpy as np

def import_and_predict(image_data, model):
size = (256, 256)
image = ImageOps.fit(image_data, size, Image.ANTIALIAS)
img = np.array(image) / 255
prediction = model.predict(np.expand_dims(img, axis=0))
return prediction

if file is None:
st.text(“Please upload an image file”)
else:
image = Image.open(file)
st.image(image, use_column_width=True)
predictions = import_and_predict(image, model)
result = “No crack detected” if predictions < 0.5 else “It has a crack”
st.write(f"Prediction: {result} (Confidence: {predictions[0][0]:.2f})")

i am running this in vs code still getting the error tensorflow modeule not found

Is necessary reboot the app, in the section manage main you can push reboot button.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.