tensorflow: AttributeError: module 'tensorflow' has no attribute 'compat' when importing tensorflow
System information
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 18.04
- TensorFlow installed from (source or binary): binary
- TensorFlow version: 2.1.0
- Python version: 3.6.8
- Installed using virtualenv? pip? conda?: Installed with poetry
- Bazel version (if compiling from source):
- GCC/Compiler version (if compiling from source):
- CUDA/cuDNN version:
- GPU model and memory:
Describe the problem
After installing tensorflow-cpu 2.1.0, when I try to import it in python I get an error saying AttributeError: module 'tensorflow' has no attribute 'compat'. I also tried with the tensorflow package but I ran into the same issue.
Provide the exact sequence of commands / steps that you executed before running into the problem
I create and activate a virtual env with poetry:
poetry shell
I install tensorflow-cpu:
poetry add tensorflow-cpu
then I run a python console:
python
>> import tensorflow
and I get the error
Any other info / logs This is the traceback I get when importing tensorflow:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow/__init__.py", line 101, in <module>
from tensorflow_core import *
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/__init__.py", line 46, in <module>
from . _api.v2 import compat
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/__init__.py", line 39, in <module>
from . import v1
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v1/__init__.py", line 32, in <module>
from . import compat
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v1/compat/__init__.py", line 39, in <module>
from . import v1
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v1/compat/v1/__init__.py", line 29, in <module>
from tensorflow._api.v2.compat.v1 import app
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/__init__.py", line 39, in <module>
from . import v1
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v1/__init__.py", line 32, in <module>
from . import compat
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v1/compat/__init__.py", line 39, in <module>
from . import v1
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v1/compat/v1/__init__.py", line 667, in <module>
from tensorflow_estimator.python.estimator.api._v1 import estimator
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_estimator/__init__.py", line 10, in <module>
from tensorflow_estimator._api.v1 import estimator
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_estimator/_api/v1/estimator/__init__.py", line 10, in <module>
from tensorflow_estimator._api.v1.estimator import experimental
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_estimator/_api/v1/estimator/experimental/__init__.py", line 10, in <module>
from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/canned/dnn.py", line 33, in <module>
from tensorflow_estimator.python.estimator import estimator
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 53, in <module>
from tensorflow_estimator.python.estimator import util as estimator_util
File "/home/luis/.cache/pypoetry/virtualenvs/eae-85aUoSbr-py3.6/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/util.py", line 75, in <module>
class _DatasetInitializerHook(tf.compat.v1.train.SessionRunHook):
AttributeError: module 'tensorflow' has no attribute 'compat'
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 10
- Comments: 27 (3 by maintainers)
Links to this issue
Commits related to this issue
- fix tensorflow install, see https://github.com/tensorflow/tensorflow/issues/37525\#issuecomment-670926083 — committed to datasci-osu/dsosuk8s by oneilsh 4 years ago
- Pin tensorflow-estimator to 2.1.* see https://github.com/tensorflow/tensorflow/issues/37525 — committed to umd-fire-coml/2020-Object-Detection-In-Aerial-Images by Epikastema 4 years ago
Adding (as a temp workaround)
tensorflow-estimator = "==2.1.0"in pyproject.toml for poetry fixes this for me under poetry. (else it gets tensorflow-estimator 2.2.0rc0 which seems to cause the issue)To bo honest with you, this is a bit of a crappy response. Poetry is getting more and more traction and is really better at dependency resolution than pip… Moreover the thread on poetry issue is related to functools32 which is not related to this issue at all.
@LuisCebrian, Can you install with
PIPas follows,pip install tensorflow==2.1Let us know if issue still persists. Thanksfor me, below commands in sequence solved the issue
pip uninstall tensorflow-estimator
pip install tensorflow-estimator==2.1
I had a look into this and think I found the root cause(s).
tensorflow-> … ->tensorflow_estimator.python.estimator.util->tensorflow; the effect of that is thattensorflow_estimator.python.estimator.utilsees a partially-initialisedtensorflowmodule, which in particular doesn’t have thecompatattribute. I’ve confirmed that replacingimport tensorflow as tfwithfrom tensorflow.compat.v1.train import SessionRunHook(and updating the usages ofSessionRunHookaccordingly) fixes the issue.Same with Anaconda, but reinstall with tensorflow=2.1 does not solve issue. “conda install tensorflow” brings tensorflow-estimator 2.2, but 2.1 is needed.
Solution: First install Estimator=2.1 then the rest:
Helped here.
YES! This worked great - make sure not to forget the equals sign here:
Are you satisfied with the resolution of your issue? Yes No
issue solved thank you verymuch
Not sure what’s the issue with tensorflow 2.1, but also happened to me.
Then
I get the same error when I recently installed Tensorflow 2.4.0.
When I run the below import
import tensorflow.compat.v1 as tfI get the same error:
ModuleNotFoundError: No module named 'tensorflow.compat'I really don’t want to use pip to install something as full of dependencies as tensorflow. It becomes inevitable that something will break, even in a separate environment.
Just had the same issue and it was solved after installing as described. Before I was installing TensorFlow through poetry.
Why does it not work anymore?
@gadagashwini @ymodak I had the same issue with pip3.
System information:
I uninstalled and reinstalled Tensorflow with the aforementioned command. However, get the error when
import tensorflow as tf:import tensorflow as tf pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 101 <module> from tensorflow_core import * pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 46 <module> from . _api.v2 import compat pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 39 <module> from . import v1 pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 32 <module> from . import compat pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 39 <module> from . import v1 pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 29 <module> from tensorflow._api.v2.compat.v1 import app pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 39 <module> from . import v1 pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 32 <module> from . import compat pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 39 <module> from . import v1 pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 667 <module> from tensorflow_estimator.python.estimator.api._v1 import estimator pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 10 <module> from tensorflow_estimator.python.estimator.api._v1.estimator import experimental pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) init.py 10 <module> from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) dnn.py 33 <module> from tensorflow_estimator.python.estimator import estimator pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) estimator.py 53 <module> from tensorflow_estimator.python.estimator import util as estimator_util pydev_import_hook.py 21 do_import module = self._system_import(name, *args, **kwargs) util.py 75 <module> class _DatasetInitializerHook(tf.compat.v1.train.SessionRunHook): AttributeError: module ‘tensorflow’ has no attribute ‘compat’