Cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental'

Question:

I am having problems trying to run TensorFlow on my Windows 10 machine. Code runs fine on my MacOS machine.

Traceback (most recent call last):
  File "c:UsersFynnDocumentsGitHubAlpacaTradingBotai.py", line 15, in <module>
    from keras.models import Sequential, load_model
  File "C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packageskeras__init__.py", line 24, in <module>
    from keras import models
  File "C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packageskerasmodels__init__.py", line 18, in <module>
    from keras.engine.functional import Functional
  File "C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packageskerasenginefunctional.py", line 24, in <module>
    from keras.dtensor import layout_map as layout_map_lib
  File "C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packageskerasdtensor__init__.py", line 22, in <module>
    from tensorflow.compat.v2.experimental import dtensor as dtensor_api  # pylint: disable=g-import-not-at-top
ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packagestensorflow_apiv2compatv2experimental__init__.py)
Asked By: Fynn

||

Answers:

This can be caused by an incompatibility between your tensorflow and your keras versions. In particular I see this with tensorflow==2.6.0 and keras==2.9.0, though I would not be surprised if other versions can cause this as well.

Either update your tensorflow version by:

pip install tensorflow==2.8

or downgrade your keras version by:

pip install keras==2.6
Answered By: Jesper Nielsen

I tried many solutions to no avail, in the end this worked for me!

pip3 uninstall tensorflow absl-py astunparse flatbuffers gast google-pasta grpcio h5py keras keras-preprocessing libclang numpy opt-einsum protobuf setuptools six tensorboard tensorflow-io-gcs-filesystem termcolor tf-estimator-nightly typing-extensions wrapt
pip3 install --disable-pip-version-check --no-cache-dir tensorflow
Answered By: Fynn

Before you must delete tensorflow and keras folder at site-package.
Open admin command prompt use this codes

pip uninstall tensorflow 

pip install tensorflow --ignore-installed
Answered By: Metehan AYDEMÄ°R

Just run :

pip install --disable-pip-version-check --no-cache-dir tensorflow
Answered By: Mukul Kirti Verma

Just uninstall and install tensorflow.

Answered By: Isaac