首页 > 解决方案 > 在anaconda环境中导入tensorflow时出现DLL错误

问题描述

我已经在 anaconda 环境中安装了 tensorflow。我已经通过“conda install tensorflow-gpu”安装了最新的 anaconda 和 tensorflow。当我尝试导入 tensorflow 时,它可以在 jupyter 笔记本中使用。但是,当我尝试从 python 终端运行或通过从 vs 代码“开始调试”运行时,确实会运行相同的代码。这是我得到的错误:

    Traceback (most recent call last):
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\__main__.py", line 410, in main
    run()
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\__main__.py", line 291, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "d:\Projects\SASRec\SASRec Pytorch\main.py", line 7, in <module>
    import tensorflow as tf
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\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:\Users\Admin\Anaconda3\envs\tf-gpu\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\Admin\Anaconda3\envs\tf-gpu\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

谁能给我一个解决方案,让我的生活更轻松。谢谢!

标签: pythontensorflowvisual-studio-code

解决方案


在使用 Python 3.7 安装 Anaconda 并准备使用 TensorFlow 之前,您可能已经安装了某些版本的 python(例如 3.6.6)。因为 Anaconda 带有自己的 Python 实例,所以您已将 TensorFlow 依赖项安装到 Python 的 Anaconda 实例中。

当您使用控制台时,您很可能使用非 Anaconda-Python 实例,该实例没有安装 TensorFlow 及其所有依赖项。

您可以为非 Anaconda-Python 安装缺少的依赖项,或者使用 Anaconda 版本的 Python 启动脚本。您可以稍后通过使用完整路径调用 python/path/to/anaconda/bin/python yourscriptusingtensorflow.py或将 anaconda 二进制路径添加到控制台的 $PATH 变量来完成。


推荐阅读