导入tensorflow时出错:ImportError: DLL load failed: 找不到指定的模块

问题如上,控制台回显如下,我看网上说是第三方库安装不完整,或者安装的第三方库被覆盖或者破坏,但是我看不出是哪个库出错了


Python 3.6.15 (default, Dec  3 2021, 18:25:24) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 658, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: DLL load failed: 找不到指定的模块。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\__init__.py", line 51, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 52, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 658, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: DLL load failed: 找不到指定的模块。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Users\72803\.conda\envs\tensorflow-gpu\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'


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.

看起来像是由于缺少某个库导致了DLL加载失败。以下是一些可能的解决方案:

检查您的环境变量。如果您已经安装了TensorFlow并且正在尝试在新的Python环境中导入它,那么您需要确保将TensorFlow的路径添加到新环境的系统变量中。您可以通过在命令行中运行以下命令来检查TensorFlow的路径:

python -c "import tensorflow; print(tensorflow.file)"
这将打印出TensorFlow的路径,您可以将其添加到新环境的系统变量中。

重新安装TensorFlow。如果您已经安装了TensorFlow并且尝试在新的Python环境中导入它,那么您需要确保您的安装是完整的,没有被覆盖或者破坏。您可以尝试卸载并重新安装TensorFlow,以确保它与您的Python环境兼容。请注意,这可能会影响到您的其他库。

检查您的第三方库。如果上述解决方案都不起作用,那么您可能需要检查您的第三方库是否完整。您可以尝试运行以下命令来查找并安装任何缺失的库:

pip freeze
这将打印出当前已安装的所有库及其版本。然后,您可以搜索并安装任何缺失的库,例如:

pip install numpy==1.19.5

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇文章讲的很详细,请看:导入tensorflow报错:ImportError: DLL load failed: 找不到指定的模块
  • 除此之外, 这篇博客: 导入tensorflow包报错——ImportError: DLL load failed: 找不到指定的模块中的 一、运行报错 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    执行导入包命令

    import tensorflow as tf
    

    报错显示如下
    报错
    找不到指定模块,于是尝试解决,以下是漫长的解决历史。

  • 以下回答来自chatgpt:

    解决方案: 根据参考资料中的段落1,尝试将protobuf版本回退到3.6.0,使用以下命令: pip install protobuf==3.6.0 如果还是不能解决问题,可以按照参考资料中的段落6,下载并安装vc_redist应用程序来获取必要的msvcp140_1.dll文件。 如果以上两种方法都不能成功解决问题,可以重新检查自己安装的第三方库是否完整,并试着重新安装或升级缺失或损坏的库。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^