在使用pip install tenorflow安装完tensorflow2.2后,执行
import tensorflow as tf
出现如下错误信息:
ImportError: DLL load failed: 找不到指定的模块。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\__init__.py", line 22, in
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\__init__.py", line 49, in
from tensorflow.python import pywrap_tensorflow
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "D:\ProgramFiles\Anaconda\envs\tensorflow\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 "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
在各种折腾后终于解决。先把解决过程分享以下,避免其他同学踩坑。
1、首先根据错误信息初步判断是缺少DLL。
2、使用命令找出缺少的DLL。
在cmd中执行
Microsoft Visual Studio 14.0安装目录\VC\bin\dumpbin.exe" /dependents tensorflow安装根目录\python\_pywrap_tensorflow_internal.pyd
得到以下类似的输出
KERNEL32.dll
WSOCK32.dll
WS2_32.dll
SHLWAPI.dll
msvcp140.dll
msvcp140_1.dll
………………
然后通过执行where命令查找缺少的DLL
where msvcp140_1.dll
最终发现是缺少msvcp140_1.dll这个库。
3、安装缺失的DLL
在进行搜索之后,终于找到问题原因。原因和解决方案如下:
从 TensorFlow 2.1.0 版开始,此软件包需要 msvcp140_1.dll
文件(旧版可再发行软件包可能不提供此文件)。 该可再发行软件包随附在 Visual Studio 2019 中,但可以单独安装:
确保在 Windows 上启用了长路径。
到此,问题完美解决。