ImportError: DLL load failed: 找不到指定的模块。Anaconda下tensorflow的安装教程。

问题:在windows下,Anaconda环境中,安装tensorflow时遇到ImportError: DLL load failed: 找不到指定的模块 问题。
环境:windows10+Anaconda3+python3.6.5+cuda10.1+cuDNN7.6.4.3,编译器使用VS2017和Pycharm。
本来使用的是pytorch,因为pytorch没有可视化工具,因此想安装tensorboard,要安装tensorboard,需要安装tensorflow。使用pip、conda搞了一天,总算折腾好了。
解决方法:换个版本。。。
尝试安装tensorflow2.1.0各种方法都不成功,虽然tensoeflow官网上写的支持这套环境的是tensorflow2.1.0。一直报的 错误如图所示:

(base) C:\Users\Administrator>python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\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 "D:\VS_2017\Anaconda3_64\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "D:\VS_2017\Anaconda3_64\lib\imp.py", line 343, 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 "<string>", line 1, in <module>
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "D:\VS_2017\Anaconda3_64\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\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 "D:\VS_2017\Anaconda3_64\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "D:\VS_2017\Anaconda3_64\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。

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.

最后查到一位同学的安装环境跟我很像,他安装的tensorflow1.12可用,于是放弃了tensorflow2.1.0,改为安装tensorflow1.12。
先把原来安装的都卸载了,

pip uninstall tensorflow
pip uninstall tensorflow-estimator
pip uninstall tensorboard

然后安装tensorflow1.12, 更正:现在tensorflow1.12已经不支持了,最低要1.13,因此我安装了tensorflow1.14,同样可用。

pip install tensorflow==1.12
pip install tensorflow==1.14

安装好后检验下tensorflow是否可用,结果没有报错,但是出了几个 warning,本来打算忽略的,但是每次调试都会出现红红的warning,太难受了,看起来不多,简单修改下就好。

>>> import tensorflow
D:\VS_2017\Anaconda3_64\lib\site-packages\tensorflow\python\framework\dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
D:\VS_2017\Anaconda3_64\lib\site-packages\tensorflow\python\framework\dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
D:\VS_2017\Anaconda3_64\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
D:\VS_2017\Anaconda3_64\lib\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
D:\VS_2017\Anaconda3_64\lib\site-packages\tensorflow\python\framework\dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
D:\VS_2017\Anaconda3_64\lib\site-packages\tensorflow\python\framework\dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])

按照warning提示,显然是tensorflow的版本较低,导致其和新版numpy不能很好兼容,打开 D:\VS_2017\Anaconda3_64\lib\site-packages\tensorflow\python\framework\dtypes.py这个文件,按照提示的格式修改好即可。之后再使用tensorflow就不会出现warning了。
tensorflow安装好后,tensorbord也默认安装了。打开测试下。
先写个.py文件,内容如下:

import tensorflow as tf
#定义命名空间
with tf.name_scope('input'):
  #fetch:就是同时运行多个op的意思
  input1 = tf.constant(3.0,name='A')#定义名称,会在tensorboard中代替显示
  input2 = tf.constant(4.0,name='B')
  input3 = tf.constant(5.0,name='C')
with tf.name_scope('op'):
  #加法
  add = tf.add(input2,input3)
  #乘法
  mul = tf.multiply(input1,add)
with tf.Session() as ss:
  #默认在当前py目录下的logs文件夹,没有会自己创建
  wirter = tf.summary.FileWriter('logs/',ss.graph)
  result = ss.run([mul,add])
  print(result)

运行后,在.py文件的同目录下会出现logs文件夹,生成的
events.out.tfevents.1588234302.计算机名存在其中。打开 cmd或者anaconda,tensorflow安装在哪就打开哪个。我安装在anaconda下,就打开的Anaconda Prompt。输入如下命令:

(base) C:\Users\Administrator>tensorboard --logdir=C:\Users\Administrator\Desktop\try\logs

可能会出现这个问题:

Traceback (most recent call last):
  File "d:\vs_2017\anaconda3_64\envs\open-mmlab\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "d:\vs_2017\anaconda3_64\envs\open-mmlab\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\VS_2017\Anaconda3_64\envs\open-mmlab\Scripts\tensorboard.exe\__main__.py", line 7, in <module>
  File "d:\vs_2017\anaconda3_64\envs\open-mmlab\lib\site-packages\tensorboard\main.py", line 59, in run_main
    program.get_default_assets_zip_provider())
  File "d:\vs_2017\anaconda3_64\envs\open-mmlab\lib\site-packages\tensorboard\program.py", line 144, in __init__
    self.plugin_loaders = [make_loader(p) for p in plugins]
  File "d:\vs_2017\anaconda3_64\envs\open-mmlab\lib\site-packages\tensorboard\program.py", line 144, in <listcomp>
    self.plugin_loaders = [make_loader(p) for p in plugins]
  File "d:\vs_2017\anaconda3_64\envs\open-mmlab\lib\site-packages\tensorboard\program.py", line 143, in make_loader
    raise ValueError("Not a TBLoader or TBPlugin subclass: %s" % plugin)
ValueError: Not a TBLoader or TBPlugin subclass: <class 'tensorboard_plugin_wit.wit_plugin_loader.WhatIfToolPluginLoader'>

解决方法是:

pip uninstall tensorboard-plugin-wit

然后就work了。

运行后,给出一个网址:

TensorBoard 1.12.2 at http://DESKTOP-SQVOKH9:6006 (Press CTRL+C to quit)

把这个网址复制到谷歌或者火狐浏览器中就可以打开了,其他浏览器不行。由于我回家之后谷歌浏览器就不太正常,一直不能使用搜索功能,因此我又下载了个火狐浏览器,终于是能用了。
在这里插入图片描述
总结:多看github,多看博客。

猜你喜欢

转载自blog.csdn.net/AI414010/article/details/105867396