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

TensorFlow导入失败及与Keras协同运行问题求助

TensorFlow导入DLL错误解决方法

问题详情

我的代码如下:

import os
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
import tensorflow as tf
from tensorflow.keras.layers import Conv1D, MaxPooling1D, GlobalAveragePooling1D, Dense, Dropout, BatchNormalization
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping

在Visual Studio Code中运行时,TensorFlow相关模块无法正常导入,触发以下错误:

traceback (most recent call last):
  File "c:\Users\aliabzakh77\OneDrive\Desktop\PPG CNN\PPG Hypertension CNN.py", line 16, in <module>
    import tensorflow as tf
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\__init__.py", line 55, in <module>
    from tensorflow._api.v2 import compat
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\_api\v2\compat\__init__.py", line 8, in <module>
    from tensorflow._api.v2.compat import v1
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\_api\v2\compat\v1\__init__.py", line 30, in <module>
    from tensorflow._api.v2.compat.v1 import compat
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\_api\v2\compat\v1\compat\__init__.py", line 8, in <module>    
    from tensorflow._api.v2.compat.v1.compat import v1
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\_api\v2\compat\v1\compat\v1\__init__.py", line 47, in <module>
    from tensorflow._api.v2.compat.v1 import lite
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\_api\v2\compat\v1\lite\__init__.py", line 9, in <module>      
    from tensorflow._api.v2.compat.v1.lite import experimental
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\_api\v2\compat\v1\lite\experimental\__init__.py", line 8, in <module>
    from tensorflow._api.v2.compat.v1.lite.experimental import authoring
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\_api\v2\compat\v1\lite\experimental\authoring\__init__.py", line 8, in <module>
    from tensorflow.lite.python.authoring.authoring import compatible # line: 263
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\lite\python\authoring\authoring.py", line 42, in <module>     
    from tensorflow.lite.python import convert
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\lite\python\convert.py", line 27, in <module>
    from tensorflow.compiler.mlir.lite.python import wrap_converter
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\compiler\mlir\lite\python\wrap_converter.py", line 20, in <module>
    from tensorflow.compiler.mlir.quantization.tensorflow.python import py_function_lib
  File "C:\Users\aliabzakh77\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\compiler\mlir\quantization\tensorflow\python\py_function_lib.py", line 29, in <module>
    from tensorflow.compiler.mlir.quantization.tensorflow.python import pywrap_function_lib

ImportError: DLL load failed while importing pywrap_function_lib: The specified procedure could not be found.

已尝试更新pip和TensorFlow,问题仍未解决,求可行解决方案。

解决方案

  • 降级Python版本:TensorFlow 2.x对Python 3.11的兼容性存在不稳定情况,建议降级到Python 3.9或3.10,这两个版本是TensorFlow官方推荐的稳定适配版本。
  • 彻底卸载重装TensorFlow:
    1. 卸载现有TensorFlow:pip uninstall tensorflow -y
    2. 清理pip缓存:pip cache purge
    3. 安装指定稳定版本(如2.15.0):pip install tensorflow==2.15.0
  • 安装Microsoft Visual C++ Redistributable:该错误多因系统缺失必要的C运行库导致,下载并安装对应系统位数的Microsoft Visual C Redistributable for Visual Studio 2015-2022。
  • 修复环境变量:确保Python的Scripts目录和TensorFlow安装目录已添加到系统PATH中;可临时在代码开头添加以下语句测试:
    os.environ['PATH'] += ';' + os.path.dirname(tf.__file__) + '\\lite\\python\\authoring'
    
  • 使用虚拟环境隔离依赖:
    1. 创建虚拟环境:python -m venv tf_env
    2. 激活虚拟环境:Windows执行tf_env\Scripts\activate,Linux/macOS执行source tf_env/bin/activate
    3. 在虚拟环境内安装TensorFlow:pip install tensorflow==2.15.0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 12:32:02