tensorflow: ModuleNotFoundError: No module named 'tensorflow_core.keras' in Flask
Please make sure that this is a build/installation issue. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:build_template
System information
- OS X 10.15.1 Catalina
- TensorFlow version: 1.15.0
- Python version: 3.7.3
- virtualenv (Flask)
Describe the problem I’m unable to import and run a model using Flask services because of this error: ModuleNotFoundError: No module named ‘tensorflow_core.keras’
Provide the exact sequence of commands / steps that you executed before running into the problem
- I have pip installed keras and tensorflow into my virtualenv (I’ve also tried pip3 install)
- My code is like this:
import tensorflow as tf
from keras.models import load_model
model = load_model("model.hdf5")
# ... preprocess images
predictions = model.predict(processed_images)
Any other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.
Traceback (most recent call last):
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/bin/flask", line 8, in <module>
sys.exit(main())
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/flask/cli.py", line 966, in main
cli.main(prog_name="python -m flask" if as_module else None)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/flask/cli.py", line 860, in run_command
extra_files=extra_files,
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/werkzeug/serving.py", line 1008, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/werkzeug/_reloader.py", line 337, in run_with_reloader
reloader.run()
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/werkzeug/_reloader.py", line 202, in run
for filename in chain(_iter_module_files(), self.extra_files):
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/werkzeug/_reloader.py", line 24, in _iter_module_files
filename = getattr(module, "__file__", None)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/tensorflow/__init__.py", line 50, in __getattr__
module = self._load()
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/tensorflow/__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "/Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'tensorflow_core.keras'
Using TensorFlow backend.
Using TensorFlow backend.
WARNING:tensorflow:From /Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From /Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From /Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4070: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.
WARNING:tensorflow:From /Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4070: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.
2019-11-26 15:33:29.703531: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fb84403e4f0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2019-11-26 15:33:29.703585: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2019-11-26 15:33:29.705059: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fa1f9bbcd00 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2019-11-26 15:33:29.705389: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
WARNING:tensorflow:From /Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/tensorflow_core/python/ops/math_grad.py:1424: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
WARNING:tensorflow:From /Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/tensorflow_core/python/ops/math_grad.py:1424: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
WARNING:tensorflow:From /Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.
WARNING:tensorflow:From /Users/llemonthyme/Desktop/traffic-app/flaskservice/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 21
- Comments: 68 (2 by maintainers)
I’ve been struggling with this issue for the past few days, I just tried to run flask with debug mode off and the exception no longer occurs. As an annoying work around setting
FLASK_DEBUG=1allows you to continue working but you’ll need to restart your server after every change.I’m still trying to figure out why flask debug mode is causing this issue.
It looks like passing --eager-loading to the
flask runcommand is another work around.I’m getting the same issue. Simply adding the line “from tensorflow import keras” into my flask app.py will cause the error:
ModuleNotFoundError: No module named 'tensorflow_core.keras'this seems to have worked
vs failing
UPDATE: for those using Flask, TF2, and gunicorn
For those running flask through gunicorn, I found that running
was causing issues with the
--reloadflag. So basically you would get theModuleNotFoundError: No module named 'tensorflow_core.keras'error. But if you removed the--reloadflag it would fix the error. However, while developing your app, you want to be able to use--reload. I was using gunicorn = “==19.9.0”.But, when I upgraded to
gunicorn = "==20.0.4"everything was fixed. Here are some of the package versions I was using.As other comments have mentioned, a similar error happens in Django as well. I am facing the same issue with:
Thanks to other comments I have found it only happens with
tensorflow==1.15.0andtensorflow==2.0Hence, a possible workaround is to downgrade totensorflow==1.14.0or below.Another workaround I have found, which is ugly as well, is to include
import tensorflow as tfin the import section of the app’s
manage.pyI hope that information could also help to eventually resolve the bug.
same here with
Flask==1.1.1tensorflow==1.15.0in a ubuntu 18.04 docker. I have to setFLASK_DEBUG=0to let it work.I stopped getting ModuleNotFoundError: No module named ‘tensorflow_core.keras’ in Flask when I used
i.e didn’t use
export FLASK_ENV=developmentFlask==1.1.1 pdfminer.six==20200124 lmdb==0.98 flask-cors==3.0.8 numpy==1.17.4 tensorflow==2.1.0 keras nltk
environment:
did the trick for me.
The recommended way to prevent this is to:
python -m pip install ...python -m pip listOr, to use virtualenvs or similar.
I’ve got the same error, but with django though. Python 3.7.5 TF 1.15.0. With TF 1.14.0 it works fine.
Which is super strange, because when I call the import directly from a python interpreter in terminal - it works:
Even loading a model works.
Same issue here. Just FLASK_DEBUG=0 helps
I am facing the same issue when running my flask app in a docker container.
Does not work. I tried this and still getting same error.
Thank you for the reproduction.
It seems this is going to be solved in 2.2, but it happens in 1.15 and 2.1. Unfortunately, I’m not sure if we can backport a fix safely.
(where
test.pyis the code in the reproducer)pip uninstall tensorflowafter thatpip install tensorflowworked for meI’m also running into this issue with the
flask runcommand in development with the latest versions of tensorflow and flask:FLASK_ENV=development flask runHas anybody gleaned any insight into the problem?
It seems that enough people are taxed by it to warrant an economic incentive for its fix. Any interest in pooling together to establish a reasonable bug bounty?
I have the same error with Python 3.7.5, TensorFlow 2.0.0, Flask 1.1.1 on macOS 10.14.6.
Unfortunately, the error does not occur every time. Sometimes it will take up to 100 reload/req cycles to cause the exception:
The Werkzeug
_reloaderis looping oversys.modules.values().Every keras submodule is in the list. Is it interesting that
tensorflow_core.kerasoccasionally appears twice? It is the only one duplicated in the occurrence below.Also, without
list(), the above code warns thesys.modulesdictionary has changed during iteration. Werkzeug knows about this and useslist()as well.I am not using Django, but it was mentioned up-thread, so I briefly skimmed its reloader. It similarly watches all modules by looping over
sys.moduleskeys.I’m not sure if this is a “bug” in Tensorflow, or a consequence of how other code is reloading modules. I know very little about the Python module system.
Closing this issue as it is solved in 2.2
I has a similar problem while integrating keras model to django framework. Using TensorFlow backend.
But I don’t know how to fix it. Longing for your reply!
I retrained the model with
tensorflow==1.14.0then saved the model and usedgunicornto run flask. This solved my problem.I have found that down grading the tensorflow version to 1.x resolved my issue - is everyone experiencing issues using TF 2?
Hi @nikochiko. Thanks for the reply. Unfortunately the solution mentioned in that thread isn’t quite clear:
I was unable to find any solution online as to what path was and how to add CUDA/cudnn to it.
@MihailSalnikov Thanks, you’re correct in your reply here
I had old dependencies. Uninstalling everything tf and reinstalling with 2.2.0rc3 fixed my problem. Thank you
It works! Thanks a lot!
| | 叶田地 | | 邮箱:18365225454@163.com |
签名由 网易邮箱大师 定制
On 04/22/2020 00:43, Mihai Maruseac wrote:
@TDYe123 solution was mentioned 1 and 2 comments above yours. Either downgrade to 1.14 (not recommended) or upgrade to 2.2 release candidate (final to be released soon)
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.
@TDYe123 solution was mentioned 1 and 2 comments above yours. Either downgrade to 1.14 (not recommended) or upgrade to 2.2 release candidate (final to be released soon)
Recommend upgrading to 2.2 where this issue is also fixed (rc3 is available for testing now, final release will be early next week). This is because 1.14 release has a large number of bugs and inconsistencies and is already outside the support window. Sure, switching to 2.2 means upgrading to use TF2.0 APIs but this is also something we recommend.
Here’s a minimal, reproducible example of this error that only imports tensorflow. EDIT: made even smaller
@mihaimaruseac I’ve seen several people mention that it also affects the Django reloader. I’m wondering if there’s a way to modify TensorFlow so that both the Flask and Django reloaders will work with it.
Same here! Also giving more ram to Docker made my flask app start, otherwise the workers would just restart indefinitely
I’m also facing the same issue when using TensorFlow v2.1.0 and 2.0.0 with Flask in Docker & Docker-Compose. As mentioned in above threads, setting FLASK_DEBUG=0 worked for my case.
Another way that worked for my case and allows keeping FLASK_DEBUG=1 during development is to use package versions
Flask==1.1.1andtensorflow==2.0.0-beta1. By the way, I’m using this flask+docker image from uwsgi-nginx-flask-dockerThanks @enriqueav I worked I was facing this same error with:
python 3.6.4 Django 1.11 Tensorflow 2
Apparently as you said when placing the import:
import tensorflow as tf
In the apps section manage.py The django server runs without any problem.
Sorry for the delay. I had to roll back to Tensorflow 1.13.1 to get this to work.