简体   繁体   中英

ImportError: cannot import name '__version__' from partially initialized module 'keras' (most likely due to a circular import)

I have imported the following libraries for my machine learning project but have problem when I try to run my model in the command prompt...

from tensorflow.python.keras import Model

from tensorflow.python.keras.layers import Layer, Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, Lambda

from tensorflow.python.keras.initializers import TruncatedNormal

from keras.optimizers import Adam

from tensorflow.python.keras.callbacks import ModelCheckpoint, LearningRateScheduler, CSVLogger, Callback

from tensorflow.python.keras.models import load_model

from tensorflow.python.keras.utils import Sequence

This is the error message which I get when trying to run the model in the command prompt.

ImportError: cannot import name '__version__' from partially initialized module 'keras' (most likely due to a circular import) (C:\Users\gurun\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py)

You should be consistent and import keras only from one source and the recommended way, as @TFer2 said, is from tensorflow.keras . Your editor maybe can complain that tensorflow.keras cannot be resolved but usually that warning can be ignored and all works fine. If not try to uninstall keras and reinstall it accordingly to the version of your tensorflow installation.

It is always recommend to use tensorflow.keras.* instead of tensorflow.python.* .

This is because anything under tensorflow.python.* is private, intended for development only, rather than for public use.

import tensorflow as tf
print(tf.__version__)

from tensorflow.keras import Model

from tensorflow.keras.layers import Layer, Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, Lambda

from tensorflow.keras.initializers import TruncatedNormal

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, CSVLogger, Callback

from tensorflow.keras.models import load_model

from tensorflow.keras.utils import Sequence

Output:

2.5.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM