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:
- 卸载现有TensorFlow:
pip uninstall tensorflow -y - 清理pip缓存:
pip cache purge - 安装指定稳定版本(如2.15.0):
pip install tensorflow==2.15.0
- 卸载现有TensorFlow:
- 安装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' - 使用虚拟环境隔离依赖:
- 创建虚拟环境:
python -m venv tf_env - 激活虚拟环境:Windows执行
tf_env\Scripts\activate,Linux/macOS执行source tf_env/bin/activate - 在虚拟环境内安装TensorFlow:
pip install tensorflow==2.15.0
- 创建虚拟环境:
内容的提问来源于stack exchange,提问作者Ali Abzakh
相关产品推荐
相关产品推荐

