首页 > 解决方案 > TensorFlow 导入错误

问题描述

我使用 pip install 安装了 Tensorflow 2.2.0,在 Python 3.8 中导入时遇到了一些问题。发生以下错误,我想知道如何解决此问题:

Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Program Files\Python38\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Program Files\Python38\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:\Program Files\Python38\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Program Files\Python38\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/[name]/Downloads/AnomalyDetection/Paysim.py", line 2, in <module>
    import tensorflow as tf
  File "C:\Program Files\Python38\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Program Files\Python38\lib\site-packages\tensorflow\python\__init__.py", line 50, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Program Files\Python38\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 69, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Program Files\Python38\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Program Files\Python38\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:\Program Files\Python38\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Program Files\Python38\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.

标签: pythontensorflow

解决方案


根据GitHub上这个问题的官方线程,这个问题显然有两种解决方案:

第一个解决方案:

从更简单的解决方案开始,您可以尝试将您的tensorflow版本降级为2.0.0喜欢的版本。要安装此版本,您至少需要pip 20.1

# update pip first
pip install -U pip

# install tensorflow 2.0 
pip install tensorflow==2.0

第二种解决方案:

如果第一个解决方案不适合您,那么您可以分别从此处此处下载并安装新的 C++ 可再发行 x86 和 x64 包。


推荐阅读