Jupyter Notebook导入TensorFlow遇DLL加载失败错误求助
TensorFlow导入DLL初始化失败问题解决
问题重现
- 打开Anaconda Navigator,点击「环境」选项,点击「更新索引」
- 在下拉列表选择「未安装」,搜索TensorFlow,选中Keras和TensorFlow后点击「应用」,等待安装完成退出
- 打开Jupyter Notebook,执行
import tensorflow as tf时触发以下错误:
ImportError Traceback (most recent call last) ~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module> 61 try: ---> 62 from tensorflow.python._pywrap_tensorflow_internal import * 63 # This try catch logic is because there is no bazel equivalent for py_extension. ImportError: DLL load failed while importing _pywrap_tensorflow_internal: 动态链接库(DLL)初始化例程失败。 During handling of the above exception, another exception occurred: ImportError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_8976\3793406994.py in <module> ----> 1 import tensorflow as tf ~\anaconda3\lib\site-packages\tensorflow\__init__.py in <module> 35 import typing as _typing 36 ---> 37 from tensorflow.python.tools import module_util as _module_util 38 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader 39 ~\anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module> 34 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top 35 ---> 36 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow 37 from tensorflow.python.eager import context 38 ~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module> 75 sys.setdlopenflags(_default_dlopen_flags) 76 except ImportError: ---> 77 raise ImportError( 78 f'{traceback.format_exc()}' 79 f'\n\nFailed to load the native TensorFlow runtime.\n' ImportError: Traceback (most recent call last): File "C:\Users\Amir\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 62, in <module> from tensorflow.python._pywrap_tensorflow_internal import * ImportError: DLL load failed while importing _pywrap_tensorflow_internal: 动态链接库(DLL)初始化例程失败。 无法加载原生TensorFlow运行时。
解决步骤
- 核对CUDA与cuDNN版本:TensorFlow对CUDA、cuDNN版本有严格匹配要求,比如TensorFlow 2.10对应CUDA 11.2、cuDNN 8.1,版本不匹配会直接引发DLL加载失败。
- 安装VC++运行库:下载并安装对应版本的Microsoft Visual C++ Redistributable包,TensorFlow依赖的部分DLL需要该组件支持。
- 更新显卡驱动:过时的显卡驱动可能导致CUDA运行异常,前往显卡厂商官网下载最新驱动安装。
- 修复Anaconda环境:打开Anaconda Prompt,激活安装TensorFlow的环境,执行
conda list检查TensorFlow、Keras、numpy等依赖包状态,若有损坏,先执行conda uninstall tensorflow keras,再重新安装指定版本:conda install tensorflow=2.10 keras=2.10(版本号可根据需求调整)。 - 添加环境变量路径:将Anaconda目标环境下的
Library\bin路径(例如C:\Users\Amir\anaconda3\envs\你的环境名\Library\bin)添加到系统环境变量Path中,确保DLL文件能被系统正确识别。
内容的提问来源于stack exchange,提问作者Amir Jabari
相关产品推荐
相关产品推荐

