无法从部分初始化的模块“tensorflow.python.framework.constant_op”导入名称“constant”

Gab*_*iel 4 python keras tensorflow

我已经在虚拟环境中安装了tensorflow和keras,但是当我尝试运行时from tensorflow.keras.models import load_model出现以下错误:

from tensorflow.python.framework.constant_op import constant
ImportError: cannot import name 'constant' from partially initialized module 'tensorflow.python.framework.constant_op' (most likely due to a circular import) (/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py)
Run Code Online (Sandbox Code Playgroud)

我正在运行tensorflow版本2.4.0和Keras 2.4.3和python 3.8.7

我已经卸载并重新安装了tensorflow和keras,但仍然没有机会!知道如何找到导致此问题的原因吗?

PS 这是完整的错误消息:

Traceback (most recent call last):
  File "code.py", line 4, in <module>
    from tensorflow.keras.models import load_model
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/__init__.py", line 46, in <module>
    from tensorflow.python import data
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/__init__.py", line 25, in <module>
    from tensorflow.python.data import experimental
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/experimental/__init__.py", line 96, in <module>
    from tensorflow.python.data.experimental import service
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/experimental/service/__init__.py", line 140, in <module>
    from tensorflow.python.data.experimental.ops.data_service_ops import distribute
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/experimental/ops/data_service_ops.py", line 25, in <module>
    from tensorflow.python.data.experimental.ops import compression_ops
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/experimental/ops/compression_ops.py", line 20, in <module>
    from tensorflow.python.data.util import structure
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/util/structure.py", line 26, in <module>
    from tensorflow.python.data.util import nest
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/util/nest.py", line 41, in <module>
    from tensorflow.python.framework import sparse_tensor as _sparse_tensor
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/sparse_tensor.py", line 29, in <module>
    from tensorflow.python.framework import constant_op
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 29, in <module>
    from tensorflow.python.eager import execute
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/eager/execute.py", line 28, in <module>
    from tensorflow.python.framework import ops
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 63, in <module>
    from tensorflow.python.platform import app
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/platform/app.py", line 23, in <module>
    from absl.app import run as _run
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/absl/app.py", line 35, in <module>
    import pdb
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pdb.py", line 77, in <module>
    import code
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/code.py", line 4, in <module>
    from tensorflow.keras.models import load_model
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/keras/__init__.py", line 14, in <module>
    from . import activations
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/keras/activations/__init__.py", line 10, in <module>
    from tensorflow.python.keras.activations import deserialize
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/keras/__init__.py", line 27, in <module>
    from tensorflow.python.keras import models
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/keras/models.py", line 23, in <module>
    from tensorflow.python.keras import backend as K
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/keras/backend.py", line 37, in <module>
    from tensorflow.python.client import session as session_module
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 40, in <module>
    from tensorflow.python.ops import session_ops
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/ops/session_ops.py", line 30, in <module>
    from tensorflow.python.ops import array_ops
  File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 35, in <module>
    from tensorflow.python.framework.constant_op import constant
ImportError: cannot import name 'constant' from partially initialized module 'tensorflow.python.framework.constant_op' (most likely due to a circular import) (/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py)
Run Code Online (Sandbox Code Playgroud)

小智 7

来自评论

code.py 已在 python 包中定义。因此,将code.py 文件重命名为其他名称后解决了问题(摘自 Macattack)。