PyCharm运行代码报DNN library is not found错误,终端执行正常
PyCharm中TensorFlow Conv2D报错"DNN library is not found"的排查与解决
问题背景
执行pip install waymo-open-dataset-tf-2-6-0 --user安装Waymo数据集后,TensorFlow被自动从2.11降级到2.6。恢复TensorFlow 2.11版本后,代码在终端运行完全正常,但在PyCharm中执行时触发如下报错:
11/Mar/23 13:07:49 - utils.aux_funcs - ERROR - Exception encountered when calling layer 'conv2d' (type Conv2D). {{function_node __wrapped__Conv2D_device_/job:localhost/replica:0/task:0/device:GPU:0}} DNN library is not found. [Op:Conv2D] Call arguments received by layer 'conv2d' (type Conv2D): • inputs=tf.Tensor(shape=(64, 128, 128, 1), dtype=float32) Traceback (most recent call last): File "/home/USER/projects/QANetV2/qanet/tf_train.py", line 87, in <module> trained_model = train_model( File "/home/USER/projects/QANetV2/qanet/tensor_flow/utils/tf_utils.py", line 484, in train_model model.fit( File "/home/USER/anaconda3/envs/qanet/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "/home/USER/projects/QANetV2/qanet/tensor_flow/custom/tf_models.py", line 208, in call return self.model(inputs) tensorflow.python.framework.errors_impl.UnimplementedError: Exception encountered when calling layer 'conv2d' (type Conv2D). {{function_node __wrapped__Conv2D_device_/job:localhost/replica:0/task:0/device:GPU:0}} DNN library is not found. [Op:Conv2D] Call arguments received by layer 'conv2d' (type Conv2D): • inputs=tf.Tensor(shape=(64, 128, 128, 1), dtype=float32)
已做排查:
- 确认终端与PyCharm使用的解释器完全一致
- 尝试创建新的conda环境,问题仍未解决
- 怀疑是PyCharm缓存导致的差异
解决方案
1. 清理PyCharm缓存并重启
- 点击顶部菜单栏
File->Invalidate Caches... - 勾选
Clear file system cache and local history,点击Invalidate and Restart - 重启后等待PyCharm完成项目索引重建
2. 同步PyCharm的环境变量
- 打开
Run/Debug Configurations(右上角运行按钮旁下拉框选择Edit Configurations) - 进入目标运行配置的
Environment variables标签页 - 点击
Load environment variables from shell,直接导入终端的环境变量配置 - 保存配置后重新运行代码
3. 刷新PyCharm的包索引
- 打开
File->Settings->Project: [你的项目名]->Python Interpreter - 点击解释器列表旁的刷新图标🔄,等待PyCharm重新扫描已安装包
- 确认TensorFlow 2.11及
cudatoolkit、cudnn等依赖包状态正常
4. 验证GPU设备识别
- 在代码开头添加以下片段,分别在终端和PyCharm中运行,对比输出:
import tensorflow as tf print("Available GPUs:", tf.config.list_physical_devices('GPU'))
- 如果PyCharm无法识别GPU,检查运行配置中是否添加了
CUDA_VISIBLE_DEVICES=0环境变量(GPU编号根据实际情况调整)
5. 通过PyCharm重新安装TensorFlow
- 在
Python Interpreter界面找到TensorFlow,点击减号(-)卸载 - 点击加号(+)搜索
tensorflow==2.11.0并安装 - 全程通过PyCharm的包管理界面操作,确保PyCharm能正确追踪包的安装路径
内容的提问来源于stack exchange,提问作者Michael
相关产品推荐
相关产品推荐

