安装TensorFlow时踩的一个小坑

不记得之前安装的是什么版本,这次(2020/3)让pip自动安装选择的是2.1.0,但这个版本安装后使用import tensorflow会报错,如下:

Traceback (most recent call last):
  File "[...]\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "[...]\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "[...]\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 "[...]\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "[...]\lib\imp.py", line 342, 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 "[...]\lib\site-packages\tensorflow\__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "[...]\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "[...]\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "[...]\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "[...]\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "[...]\lib\site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "[...]\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "[...]\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "[...]\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "[...]\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 "[...]\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "[...]\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。

报错十分奇怪,提示缺少DLL,但在网上找到的信息只有需要system32里的msvc140p.dll可能是问题所在,如果缺少可以安装Microsoft Visual C++ 2015 Redistributable解决。在检查后发现这个文件已经存在。尝试 https://blog.csdn.net/qq_42580947/article/details/104175457 上的方法后才解决。问题出在版本上,换成2.0.0正常运行。pip install tensorflow==2.0.0。另附上国内比较好用的pip源:

  • 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/

  • 中科大:https://pypi.mirrors.ustc.edu.cn/simple/

使用的时候在命令后面加上-i [源]即可。