module 'tensorflow._api.v1.compat.v2' has no attribute '__internal__' google colab error

11,062

Solution 1

Try these parameters, it works with me

!pip3 uninstall keras-nightly
!pip3 uninstall -y tensorflow
!pip3 install keras==2.1.6
!pip3 install tensorflow==1.15.0
!pip3 install h5py==2.10.0

Solution 2

One morning I woke up and my code was giving the exact same error (in google colab).

I was running this code to downgrade my tensorflow

!pip install tensorflow==1.13.0

I replaced it with:

%tensorflow_version 1.x

Everything else remains the same. It worked!

Share:
11,062
ddd
Author by

ddd

Updated on June 22, 2022

Comments

  • ddd
    ddd almost 2 years

    I am running a tensorflow model on google colab. Today, I got this error:

     Using TensorFlow backend.
        Traceback (most recent call last):
          File "train.py", line 6, in <module>
            from yolo import create_yolov3_model, dummy_loss
          File "/content/drive/MyDrive/yolo/yolo_plz_work/yolo.py", line 1, in <module>
            from keras.layers import Conv2D, Input, BatchNormalization, LeakyReLU, ZeroPadding2D, UpSampling2D, Lambda
          File "/usr/local/lib/python3.7/dist-packages/keras/__init__.py", line 3, in <module>
            from . import utils
          File "/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py", line 26, in <module>
            from .vis_utils import model_to_dot
          File "/usr/local/lib/python3.7/dist-packages/keras/utils/vis_utils.py", line 7, in <module>
            from ..models import Model
          File "/usr/local/lib/python3.7/dist-packages/keras/models.py", line 10, in <module>
            from .engine.input_layer import Input
          File "/usr/local/lib/python3.7/dist-packages/keras/engine/__init__.py", line 3, in <module>
            from .input_layer import Input
          File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_layer.py", line 7, in <module>
            from .base_layer import Layer
          File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 12, in <module>
            from .. import initializers
          File "/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py", line 124, in <module>
            populate_deserializable_objects()
          File "/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py", line 49, in populate_deserializable_objects
            LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
          File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/module_wrapper.py", line 193, in __getattr__
            attr = getattr(self._tfmw_wrapped_module, name)
        AttributeError: module 'tensorflow._api.v1.compat.v2' has no attribute '__internal__'
    

    Previously, things had been running smoothly, so I'm not sure why this happened. I am using Python 3.7.10, and these are the packages I am supposed to use:

    absl-py==0.9.0
    astor==0.8.1
    gast==0.2.2
    google-pasta==0.1.8
    grpcio==1.26.0
    h5py==2.10.0
    Keras==2.3.1
    Keras-Applications==1.0.8
    Keras-Preprocessing==1.1.0
    Markdown==3.1.1
    numpy==1.18.1
    opencv-contrib-python==4.1.2.30
    opt-einsum==3.1.0
    protobuf==3.11.2
    PyYAML==5.3
    scipy==1.4.1
    six==1.14.0
    tensorboard==1.15.0
    tensorflow==1.15.0
    tensorflow-estimator==1.15.1
    termcolor==1.1.0
    tqdm==4.41.1
    Werkzeug==0.16.0
    wrapt==1.11.2
    

    Perhaps colab recently upgraded some libraries? I am sure that I followed the same installation steps as I usually do.

    EDIT: I think there may be an issue in the keras version. Here are the first few lines of the file I am running:

    from keras.layers import Conv2D, Input, BatchNormalization, LeakyReLU, ZeroPadding2D, UpSampling2D, Lambda
    from keras.layers.merge import add, concatenate
    from keras.models import Model
    from keras.engine.topology import Layer
    import tensorflow as tf
    

    If I remove all of the lines starting with "from keras", I don't get the error. However, I never touched these lines before, so I don't know why they would suddenly cause an error now. Also, it is not the python version causing this error, because colab changed it to 3.7.10 in April and I had no problem.

    • ansev
      ansev almost 3 years
      I have the same problem
    • Tahseen Adit
      Tahseen Adit almost 3 years
      I am facing the same problem in the middle of my thesis. It was working fine few days ago and then suddenly getting this error now. It is driving me crazy.
    • Alexander Higgins
      Alexander Higgins almost 3 years
      Keras is clearly throwing the issue. I would open up a github issue on Tensoflow and Keras: ` File "/usr/local/lib/python3.7/dist-packages/keras/initializers/_‌​_init__.py", line 49, in populate_deserializable_objects LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()`
    • TFer2
      TFer2 almost 3 years
      This issue is due to recent colab was upgraded to TF 2.5.0, forcing an upgrade to keras-nightly. For more information you can refer here. Thanks!
  • blondelg
    blondelg almost 3 years
    Welcome to SO, please provide more details
  • ddd
    ddd almost 3 years
    Wow. This actually worked. Do you know why this happens?
  • keramat
    keramat almost 3 years
    ModuleNotFoundError: No module named 'keras.models'
  • duhaime
    duhaime over 2 years
    Colab loads tensorflow 2 by default. You have to use that %tensorflow_version 1.x declaration to use 1.x