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

MacBook Pro M3下Python读取HDF5数据集遇目录无法打开报错

解决MacBook Pro M3上HDF5插件目录无法打开的OSError问题

问题场景

使用MacBook Pro M3设备,通过Python读取HDF5数据集,已安装h5py和hdf5plugin库,运行以下代码时:

import h5py
import hdf5plugin

file_path = './blink.hdf5'

with h5py.File(file_path, 'r+') as h5_file:
    evts = h5_file["CD"]["events"]
    print(len(evts[(evts['t']<49330) & (evts['t']>48270) & (evts['x']<300) & (evts['y']>300)]))

    h5_file.close()

触发错误:

OSError: Can't read data (can't open directory: /usr/local/hdf5/lib/plugin)

解决方案

1. 创建缺失的插件目录

终端执行以下命令,创建报错提示的缺失目录并配置权限:

mkdir -p /usr/local/hdf5/lib/plugin
chmod -R 755 /usr/local/hdf5/lib/plugin

2. 手动指定HDF5插件路径

先找到hdf5plugin自带的插件目录,终端运行:

python -c "import hdf5plugin; print(hdf5plugin.PLUGIN_PATH)"

将输出的路径配置为环境变量,编辑~/.zshrc文件,添加:

export HDF5_PLUGIN_PATH=$(python -c "import hdf5plugin; print(hdf5plugin.PLUGIN_PATH)")

保存后执行source ~/.zshrc使配置生效,再重新运行Python脚本。

3. 用conda重装优化后的依赖包(若使用conda环境)

Apple Silicon设备推荐用conda-forge源安装适配的库,卸载现有包后重装:

conda uninstall h5py hdf5plugin -y
conda install h5py hdf5plugin -c conda-forge

4. 移除代码中的冗余关闭操作

with语句会自动管理文件资源,无需手动调用h5_file.close(),修改后的代码:

import h5py
import hdf5plugin

file_path = './blink.hdf5'

with h5py.File(file_path, 'r+') as h5_file:
    evts = h5_file["CD"]["events"]
    print(len(evts[(evts['t']<49330) & (evts['t']>48270) & (evts['x']<300) & (evts['y']>300)]))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 15:03:27