PyCharm中加载TensorFlow原生运行时失败问题求助
你的运行环境
- 操作系统:Windows 10
- PyCharm版本:2020.1.1(社区版)
- Python版本:3.7.7
- TensorFlow版本:2.2.0
代码导入片段
import math import pandas_datareader as web import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense, LSTM import matplotlib.pyplot as plt plt.style.use('fivethirtyeight')
报错信息
H:\Python\AI\venv\lib\site-packages\pandas_datareader\compat_init_.py:7: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
from pandas.util.testing import assert_frame_equal
Using TensorFlow backend.
Traceback (most recent call last):
File "H:\Python\AI\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "H:\Python\AI\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "H:\Python\AI\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "C:\Users\Chan\AppData\Local\Programs\Python\Python37\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "C:\Users\Chan\AppData\Local\Programs\Python\Python37\lib\imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: DLL load failed: The specified module could not be found.During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "H:/Python/AI/LSMT.py", line 7, in
from keras.models import Sequential
File "H:\Python\AI\venv\lib\site-packages\keras_init_.py", line 3, in
from . import utils
File "H:\Python\AI\venv\lib\site-packages\keras\utils_init_.py", line 6, in
from . import conv_utils
File "H:\Python\AI\venv\lib\site-packages\keras\utils\conv_utils.py", line 9, in
from .. import backend as K
File "H:\Python\AI\venv\lib\site-packages\keras\backend_init_.py", line 1, in
from .load_backend import epsilon
File "H:\Python\AI\venv\lib\site-packages\keras\backend\load_backend.py", line 90, in
from .tensorflow_backend import *
File "H:\Python\AI\venv\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in
import tensorflow as tf
File "H:\Python\AI\venv\lib\site-packages\tensorflow_init_.py", line 41, in
from tensorflow.python.tools import module_util as module_util
File "H:\Python\AI\venv\lib\site-packages\tensorflow\python_init.py", line 50, in
from tensorflow.python import pywrap_tensorflow
File "H:\Python\AI\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 69, in
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "H:\Python\AI\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "H:\Python\AI\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "H:\Python\AI\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "C:\Users\Chan\AppData\Local\Programs\Python\Python37\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "C:\Users\Chan\AppData\Local\Programs\Python\Python37\lib\imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: DLL load failed: The specified module could not be found.Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors for some common reasons and solutions.
Include the entire stack trace above this error message when asking for help.Process finished with exit code 1
针对性解决方案
你已经尝试了重装Python、TensorFlow等操作,但这个DLL加载失败问题在Windows环境下通常和缺少VC++运行库或者**CUDA依赖(如果用GPU版本)**有关,试试下面的步骤:
1. 安装/修复VC++ Redistributable for Visual Studio 2015-2019
TensorFlow 2.x依赖特定版本的VC++运行库,直接去微软官网下载对应x64版本的安装包(你的Python是64位,必须匹配),安装后重启电脑再尝试运行代码。如果已经安装,选择修复选项重新安装一次,确保文件没有损坏。
2. 切换到兼容性更好的TensorFlow版本
虽然Python3.7.7和TF2.2.0是兼容的,但TF2.3.0对Windows环境的适配更稳定,建议尝试:
pip uninstall tensorflow -y pip install tensorflow==2.3.0
3. 排查GPU版本的CUDA/cuDNN匹配问题
如果你安装的是GPU版TensorFlow,必须保证CUDA和cuDNN版本严格匹配TF2.2.0:
- CUDA版本需为10.1
- cuDNN版本需为7.6.5
同时要把CUDA的bin目录和cuDNN的bin目录添加到系统环境变量PATH中。如果不需要GPU加速,直接安装CPU版TensorFlow:
pip install tensorflow-cpu==2.2.0
4. 重建虚拟环境
有时候虚拟环境里的依赖会出现隐性冲突,建议删除现有的venv文件夹,重新创建干净的环境:
# 手动删除旧的venv文件夹后,执行以下命令 python -m venv venv # 激活虚拟环境 venv\Scripts\activate # 依次安装依赖 pip install pandas_datareader numpy pandas scikit-learn keras matplotlib tensorflow==2.3.0
5. 定位具体缺失的DLL
可以用Dependency Walker工具打开报错路径中的_pywrap_tensorflow_internal.pyd文件,查看具体缺失哪些系统DLL,然后针对性下载对应x64版本的DLL补充到系统目录或者TensorFlow的依赖目录中。
内容的提问来源于stack exchange,提问作者user2697873

