【AI】在win10上安装TensorFlow2,安装成功,但是import tensorflow时报错:pywrap_tensorflow.py", line 58

一、问题描述

在win10上安装TensorFlow2成功,但是在使用时(import tensorflow )报错。这个问题困扰了我两天两夜,熬了两宿终于解决了,赶紧记录下来。
错误信息如下:

 import tensorflow as tf
Traceback (most recent call last):
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow\__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\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/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

二、原因分析

导致错误的本质原因没有找到,在尝试安装v2.0版本后,才没有出现这个错误。

三、处理过程

截至今天(2020-2-12)TensorFlow的最新稳定版本是v2.1,在win10 + python3.6 + pip2.0环境下默认安装的就是最新的版本v2.1。安装过程没有异常,但是在使用时就会报上述错误。导致错误的本质原因没有找到,在尝试安装v2.0版本后,才没有出现这个错误。
其它尝试都会报错,如:

将CUDA版本改为10.1、10.2;
尝试安装v2.1的cpu和gpu版本;
尝试Python3.7和Python3.6;
尝试删除protobuf

四、解决方法

使用pip安装时指定版本:

pip install tensorflow==2.0.0

pip install tensorflow-gpu==2.0.0

五、安装2.1和2.0命令的不同点

在使用命令pip install tensorflow安装TensorFlow2.1时,将会同时安装cpu和gpu;
安装TensorFlow2.1或2.0的命令略有不用,汇总如下

1、TensorFlow2.0

单独安装CPU:pip install tensorflow ==2.0.0
单独安装GPU:pip install tensorflow-gpu ==2.0.0

2、TensorFlow2.1

单独安装CPU:pip install tensorflow-cpu == 2.1.0
单独安装GPU:pip install tensorflow-gpu== 2.1.0
同时安装CPU和GPU:pip install tensorflow==2.1.0

六、使用TenforFlow2.0-GPU时,报错:cudart64_100.dll not found

1、错误信息如下
import tensorflow as tf
2020-02-12 01:12:14.047720: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
2、原因分析

cudart64_100.dll是CUDA10.0版本中的库,而我安装是最新的(截至2020-2-12)CUDA10.2,这个版本中cudart64库的名字是cudart64_102.dll,因此报错没有找到cudart64_100.dll。

3、解决方法

一种解决方法是重新安装CUDA10.0的版本;
另外一种简单方法是,直接找到cudart64_102.dll将它修改为cudart64_100.dll(这个方法不太严谨,不知道以后会不会报错)
更改后的打印信息

>>> import tensorflow as tf
2020-02-12 01:15:35.685062: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll

七、测试TensorFlow是否支持GPU

1、测试对cuda的支持
>>> tf.test.is_built_with_cuda()
True
2、测试对GPU的支持
>>> tf.test.is_gpu_available(cuda_only=False,min_cuda_compute_capability=None)
2020-02-12 01:17:21.667124: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-02-12 01:17:21.681120: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-02-12 01:17:22.159708: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.189
pciBusID: 0000:01:00.0
2020-02-12 01:17:22.208181: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2020-02-12 01:17:22.246881: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2020-02-12 01:17:22.579081: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-02-12 01:17:22.587504: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0
2020-02-12 01:17:22.593051: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N
2020-02-12 01:17:22.601454: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/device:GPU:0 with 1464 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0)
True
发布了324 篇原创文章 · 获赞 266 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/u010168781/article/details/104272729