从H5文件创建数据集遇阻:HDF5插件目录读取报错求助
处理H5文件时的OSError:无法打开hdf5/plugin目录
错误信息
加载H5文件时触发以下错误:
OSError: Can't read data (can't open directory: /opt/conda/envs/AE4353/lib/hdf5/plugin)
加载代码
确认文件路径正确且依赖已安装,但运行以下代码仍报错:
def load_h5_file(h5_file_path): with h5py.File(h5_file_path, "r") as file: images = file["images"][:] targets = file["targets"][:] Images = torch.tensor(images) Targets = torch.tensor(targets) return Images, Targets load_h5_file(h5_file_path)
已尝试的操作
- conda安装hdf5-plugin失败:
(AE4353_user_env) vscode ➜ /workspaces/AE4353-Y24 (main) $ conda install -c conda-forge hdf5-plugin Channels: - conda-forge - defaults Platform: linux-64 Collecting package metadata (repodata.json): done Solving environment: failed PackagesNotFoundError: The following packages are not available from current channels: - hdf5-plugin Current channels: - https://conda.anaconda.org/conda-forge - defaults - pip安装h5py[hdf5plugin]出现警告:
Requirement already satisfied: h5py[hdf5plugin] in /home/kibangadas/.local/lib/python3.8/site-packages (3.11.0) WARNING: h5py 3.11.0 does not provide the extra 'hdf5plugin' Requirement already satisfied: numpy>=1.17.3 in /home/kibangadas/.local/lib/python3.8/site-packages (from h5py[hdf5plugin]) (1.24.4) - 单独安装hdf5plugin成功,但问题依旧:
Collecting hdf5plugin Downloading hdf5plugin-5.0.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (45.6 MB) |████████████████████████████████| 45.6 MB 14.1 MB/s Requirement already satisfied: h5py>=3.0.0 in /home/kibangadas/.local/lib/python3.8/site-packages (from hdf5plugin) (3.11.0) Requirement already satisfied: numpy>=1.17.3 in /home/kibangadas/.local/lib/python3.8/site-packages (from h5py>=3.0.0->hdf5plugin) (1.24.4) Installing collected packages: hdf5plugin Successfully installed hdf5plugin-5.0.0
解决方案
1. 手动指定HDF5_PLUGIN_PATH环境变量
hdf5plugin安装后,需要让HDF5库找到插件路径,可在代码开头添加:
import os import hdf5plugin os.environ['HDF5_PLUGIN_PATH'] = hdf5plugin.PLUGIN_PATH
或者在终端运行代码前设置环境变量:
export HDF5_PLUGIN_PATH=$(python -c "import hdf5plugin; print(hdf5plugin.PLUGIN_PATH)")
2. 确保conda环境使用正确的hdf5plugin安装
检查VSCode的Python解释器是否选中了AE4353_user_env,然后激活该环境重新安装:
conda activate AE4353_user_env pip install hdf5plugin
3. 创建缺失的插件目录并软链接
直接补全报错中缺失的目录,将hdf5plugin的插件文件链接过去:
mkdir -p /opt/conda/envs/AE4353/lib/hdf5/plugin ln -s $(python -c "import hdf5plugin; print(hdf5plugin.PLUGIN_PATH)")/* /opt/conda/envs/AE4353/lib/hdf5/plugin/
4. 调整h5py版本兼容
部分h5py版本与hdf5plugin存在兼容性问题,尝试降级h5py:
pip install h5py==3.9.0
内容的提问来源于stack exchange,提问作者Anon
相关产品推荐
相关产品推荐

