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)

Commits related to this issue

Most upvoted comments

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 PIP as follows, pip install tensorflow==2.1 Let us know if issue still persists. Thanks

for 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).

  1. a bug in poetry, which causes version 2.2.0rc0 of tensorflow-estimator to get installed despite tensorflow specifying a version of <2.2.0 (https://github.com/python-poetry/poetry/issues/2271)
  2. tensorflow-estimator 2.2.0rc0 has a circular import, introduced in https://github.com/tensorflow/estimator/commit/a70da58fef26d2c027cc379b15d92f16ccbd513b, where we import tensorflow -> … -> tensorflow_estimator.python.estimator.util -> tensorflow; the effect of that is that tensorflow_estimator.python.estimator.util sees a partially-initialised tensorflow module, which in particular doesn’t have the compat attribute. I’ve confirmed that replacing import tensorflow as tf with from tensorflow.compat.v1.train import SessionRunHook (and updating the usages of SessionRunHook accordingly) 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:

  • conda remove tensorflow
  • conda install tensorflow-estimator 2.1
  • conda install tensorflow-gpu=2.1

Helped here.

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:

  • conda remove tensorflow
  • conda install tensorflow-estimator 2.1
  • conda install tensorflow-gpu=2.1

Helped here.

YES! This worked great - make sure not to forget the equals sign here:

  • conda install tensorflow-estimator=2.1

Are you satisfied with the resolution of your issue? Yes No

@LuisCebrian, Can you install with PIP as follows, pip install tensorflow==2.1 Let us know if issue still persists. Thanks

@gadagashwini The issue is solved if I install it that way. Why does tensorflow do not work if I install it with poetry?

@asequeira-os Thank you so much for your workaround

issue solved thank you verymuch

Not sure what’s the issue with tensorflow 2.1, but also happened to me.

pip uninstall tensorflow
pip uninstall tensorflow-estimator

Then

pip install tensorflow

I get the same error when I recently installed Tensorflow 2.4.0.

When I run the below import import tensorflow.compat.v1 as tf

I 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:

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 18.04
  • TensorFlow installed from (source or binary): source
  • TensorFlow version: 2.1.0
  • Python version: 3.6.8
  • Installed using virtualenv? pip? conda?: Installed with pip
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:

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’