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

Python 3.12中TensorFlow安装DLL初始化错误的解决方法

Python 3.12下TensorFlow DLL加载失败的解决方法

问题复现

运行导入TensorFlow的代码时出现如下错误:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Roaming\Python\Python312\site-packages\tensorflow\python\pywrap_tensorflow.py", line 70, in <module>
    from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: A dynamic link library (DLL) initialization routine failed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\midterm\1st.py", line 5, in <module>
    from deepface import DeepFace
  File "C:\Users\user\AppData\Roaming\Python\Python312\site-packages\deepface\DeepFace.py", line 15, in <module>
    import tensorflow as tf
  File "C:\Users\user\AppData\Roaming\Python\Python312\site-packages\tensorflow\__init__.py", line 38, in <module>
    from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow  # pylint: disable=unused-import
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python312\site-packages\tensorflow\python\pywrap_tensorflow.py", line 85, in <module>
    raise ImportError(
ImportError: Traceback (most recent call last):
  File "C:\Users\user\AppData\Roaming\Python\Python312\site-packages\tensorflow\python\pywrap_tensorflow.py", line 70, in <module>
    from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: A dynamic link library (DLL) initialization routine failed.

解决步骤

  • 降级Python版本:TensorFlow官方当前未支持Python 3.12,建议切换到Python 3.10或3.11。可通过虚拟环境隔离版本:
    # 创建Python3.11虚拟环境
    py -3.11 -m venv tf_env
    # Windows下激活虚拟环境
    tf_env\Scripts\activate
    
  • 安装VC++运行库:缺失Microsoft Visual C++ Redistributable 2015-2022版本会引发该错误,安装对应版本后重启电脑。
  • 重装适配版TensorFlow:卸载现有版本后,安装与Python版本兼容的TensorFlow,例如:
    pip uninstall tensorflow -y
    pip install tensorflow==2.15.0
    
  • GPU版本额外检查:若使用GPU版TensorFlow,需确保CUDA、cuDNN版本与TensorFlow严格匹配,并将CUDA的bin和libnvvp目录添加到系统PATH环境变量。
  • 隔离依赖环境:全程在虚拟环境中操作,避免全局Python环境的依赖包冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 06:50:04