tensorflow 2 安装以及 ImportError: DLL load failed解决

tensorflow 2的个人安装过程,以及解决出现的一些问题。

系统要求

我安装的版本是 tensorflow 2.1.0,这个版本以及可以支持 CPU 和 GPU 的运行与计算。

  • Python 3.5-3.7
  • pip 19.0 或更高版本(需要 manylinux2010 支持)
  • Ubuntu 16.04 或更高版本(64 位)
  • macOS 10.12.6 (Sierra) 或更高版本(64 位)(不支持 GPU)
  • Windows 7 或更高版本(64 位)(仅支持 Python 3)
  • Raspbian 9.0 或更高版本
  • GPU 支持需要使用支持 CUDA® 的卡(适用于 Ubuntu 和 Windows)

Windows安装过程

本人是Windows系统,下面写一下Windows安装的过程。Windows安装需要注意以下几点:

  1. 安装适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ Redistributable。从 TensorFlow 2.1.0 版开始,此软件包需要 msvcp140_1.dll 文件(旧版可再发行软件包可能不提供此文件)。 该可再发行软件包随附在 Visual Studio 2019 中,但可以单独安装。
  2. 确保在 Windows 上启用了长路径
  3. 安装 64 位适用于 Windows 的 Python 3 版本

Microsoft Visual C++ Redistributable安装

如果电脑里安装了Visual Studio 2015, 2017 and 2019,这一步就不用了,如果没有,你那这一步还是好好安装,要不然就会出现ImportError: DLL load failed错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
>>> import tensorflow as tf
Traceback (most recent call last):
File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Development\Python\Python37\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:\Development\Python\Python37\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "C:\Development\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 "<stdin>", line 1, in <module>
File "C:\Development\Python\Python37\lib\site-packages\tensorflow\__init__.py", line 101, in <module>
from tensorflow_core import *
File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
from tensorflow.python.tools import module_util as _module_util
File "C:\Development\Python\Python37\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
module = self._load()
File "C:\Development\Python\Python37\lib\site-packages\tensorflow\__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "C:\Development\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 74, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Development\Python\Python37\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:\Development\Python\Python37\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "C:\Development\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.

解决方案就是去Microsoft Visual C++下载页面,下载x64: vc_redist.x64.exe, 完成安装。因为你的电脑必须要是64位的,才能安装64位的 Python 3,才能满足 tensorflow 2.1.0的安装要求。

长路径

长路径好像不启用,也没什么问题,但保险起见,还是打开吧,反正也挺容易的。

  1. win+R,输入 gpedit.msc,打开本地策略组编辑器
  2. 依次选择 计算机配置 > 管理模板 > 系统 > 文件系统
  3. 文件系统 界面下,选择 启用 Win32 长路径,双击打开,选择已启用,然后确定关闭。

64位 Python 3

一定要安装64位的 Python 3。查看 Python 3 版本方式有很多,下面写两个最简单的。

  1. 打开 cmd 命令行, 输入 python -VVpython -V 是简略的版本信息,python -VV 信息更加详细,包括位数信息,我的64位版本信息如下:

    1
    Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)]
  2. 打开python命令行,最开始出现的消息里面就有位数信息。

    1
    2
    3
    4
    C:\Users\*******>python
    Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

在 Windows 上,仅支持在 Python 3 64位版本下, 安装 TensorFlow 2.1.0,如果是 Python 2 版本, 在 Windows 上是实现不了的。

安装 TensorFlow 2.1.0

安装使用 pip 工具,19.0 或者更高版本。如果在虚拟环境里安装,注意先要更新 pip,然后在进行 pip 的安装 ( python -m pip install --upgrade pip )。

1
pip install tensorflow

由于安装包比较大,推荐使用国内镜像,在命令行后加 -i [https://xxxxx] 即可,下面是使用清华镜像源,你也可以永久修改默认源的使用,这里就不介绍了。

1
pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple

GPU 安装支持

如果你进行了以上安装,不出意外的话,tensorflow 应该是安装成功了,但你在使用时可能会看到以下警告,因为现在的 tensorflow 只能使用 CPU,无法使用 GPU。

1
2
3
2020-xx-xx xx:xx:xx.xxxxxx: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found

2020-xx-xx xx:xx:xx.xxxxxx: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.

首先查看你的 GPU 是否支持 CUDA,点击查询页面查找自己的显卡是否在列表内。或者使用”显卡名称 + specification“进行搜索,直接查看自己的显卡信息。

显卡支持后,如果要使用 GPU,还需要安装下面几个软件:

CUDA 选择 10.1 版本,cuDNN 选择配套版本。安装完成后,import tensorflow 不报错,无警告,应该就成功了。

检查使用情况

最后,在看看自己设备上的使用信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 11567553738839476818
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 6661821563
locality {
bus_id: 1
links {
}
}
incarnation: 17494855292993829603
physical_device_desc: "device: 0, name: GeForce GTX 1070 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1"
]

至此,结束,感觉挺麻烦的。

-------------本文结束感谢您的阅读-------------
0%