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 "", 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,

pip install tensorflow==1.12

安装好后检验下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

运行后,给出一个网址:

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

把这个网址复制到谷歌或者火狐浏览器中就可以打开了,其他浏览器不行。由于我回家之后谷歌浏览器就不太正常,一直不能使用搜索功能,因此我又下载了个火狐浏览器,终于是能用了。
ImportError: DLL load failed: 找不到指定的模块。Anaconda下tensorflow的安装教程。_第1张图片
总结:敢情我这一天就配了个环境,还是使用的换个版本重装的方法。以后配环境还是要多看看他人经验,就算是官网推荐的也不一定就是完美兼容的版本。感谢大佬们的分享,不管是可用的方法还是不可用的方法,总能给一些人解决问题。

你可能感兴趣的:(Anaconda配置)