You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已安装h5py仍出现ImportError: cannot import name '_errors'的原因

解决h5py循环导入错误(ImportError: cannot import name '_errors')

问题说明

已安装h5py库,但运行包含TensorFlow、GAN的Python代码时触发以下错误:

文件路径:~\Desktop\gan\venv\lib\site-packages\h5py_init_.py:25
代码行:from . import _errors
错误信息:ImportError: cannot import name '_errors' from partially initialized module 'h5py' (most likely due to a circular import) (C:\Users\Qazal\Desktop\gan\venv\lib\site-packages\h5py\__init__.py)

问题根源

你的代码开头错误添加了h5py库内部的导入语句:from . import _errors和import h5py.h5py_errors,这两行是h5py自身初始化时的代码,在你的脚本中导入会直接触发循环引用,导致h5py无法正常初始化。

解决方案

1. 删除错误导入语句

直接删掉代码开头的这两行:

from . import _errors
import h5py.h5py_errors

2. 修复代码拼写错误

代码中存在一处类名拼写错误,将:

from tensorflow.keras.layers import Conv2D, Dense, Flatten, Reshape, LeakyReL

修改为:

from tensorflow.keras.layers import Conv2D, Dense, Flatten, Reshape, LeakyReLU

(LeakyReL少写了一个U,会导致找不到该层类)

3. (可选)重新安装h5py

如果上述操作后仍报错,说明虚拟环境中的h5py可能损坏,执行以下命令重装:

pip uninstall h5py -y
pip install h5py

确保h5py版本与TensorFlow兼容(TensorFlow 2.x建议搭配h5py 3.x版本)

修正后的代码片段示例

import tensorflow as tf
from keras.layers import LeakyReLU, UpSampling2D, Dropout

# 配置GPU显存增长
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus: 
    tf.config.experimental.set_memory_growth(gpu, True)

# 导入依赖库
import tensorflow_datasets as tfds
from matplotlib import pyplot as plt

内容的提问来源于stack exchange,提问作者Ghazaleh Alizadegan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 09:33:23