TensorFlow 2.4.0导入np_config报错:无法从numpy_ops导入该名称
TensorFlow导入np_config报错解决
问题详情
尝试导入代码:
import tensorflow as tf from tensorflow.python.ops.numpy_ops import np_config
触发错误:
ImportError: cannot import name 'np_config' from 'tensorflow.python.ops.numpy_ops' (C:\Anaconda3\lib\site-packages\tensorflow\python\ops\numpy_ops\__init__.py)
当前环境版本:
import tensorflow print(tf.__version__) import keras print(keras.__version__)
输出:
2.4.0 2.4.0
已尝试相关方案但未解决。
解决办法
1. 升级TensorFlow版本
np_config是TensorFlow 2.6及以上版本才新增的配置模块,2.4版本不包含该组件。执行升级命令:
- 使用pip:
pip install --upgrade tensorflow - 使用conda环境:
conda update tensorflow
升级完成后,即可正常导入并使用:
import tensorflow as tf from tensorflow.python.ops.numpy_ops import np_config np_config.enable_numpy_behavior()
2. 不升级版本的替代方案
若无法升级,2.4版本可通过以下方式启用TensorFlow与NumPy的兼容行为:
import tensorflow as tf from tensorflow.python.ops.numpy_ops import np_ops np_ops.enable_numpy_behavior()
内容的提问来源于stack exchange,提问作者Avv
相关产品推荐
相关产品推荐

