【问题标题】:how do I correctly import tensorflow?如何正确导入张量流?
【发布时间】:2020-03-26 18:18:08
【问题描述】:

import tensorflow as tf

我在 Anaconda 解释器中收到此消息...有人可以帮忙吗?

谢谢!


ImportError                               Traceback (most recent call last)
~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     57 
---> 58   from tensorflow.python.pywrap_tensorflow_internal import *
     59   from tensorflow.python.pywrap_tensorflow_internal import __version__

~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in <module>
     27             return _mod
---> 28     _pywrap_tensorflow_internal = swig_import_helper()
     29     del swig_import_helper

~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
     23             try:
---> 24                 _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
     25             finally:

~\anaconda3\lib\imp.py in load_module(name, file, filename, details)
    241         else:
--> 242             return load_dynamic(name, filename, file)
    243     elif type_ == PKG_DIRECTORY:

~\anaconda3\lib\imp.py in load_dynamic(name, path, file)
    341             name=name, loader=loader, origin=path)
--> 342         return _load(spec)
    343 

ImportError: DLL load failed: Eine DLL-Initialisierungsroutine ist fehlgeschlagen.

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-1-64156d691fe5> in <module>
----> 1 import tensorflow as tf

~\anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
     22 
     23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
     25 
     26 from tensorflow._api.v1 import app

~\anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
     47 import numpy as np
     48 
---> 49 from tensorflow.python import pywrap_tensorflow
     50 
     51 # Protocol buffers

~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     72 for some common reasons and solutions.  Include the entire stack trace
     73 above this error message when asking for help.""" % traceback.format_exc()
---> 74   raise ImportError(msg)
     75 
     76 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long

ImportError: Traceback (most recent call last):
  File "C:\Users\MIGUEL\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\MIGUEL\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\MIGUEL\anaconda3\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\MIGUEL\anaconda3\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\MIGUEL\anaconda3\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: Eine DLL-Initialisierungsroutine ist fehlgeschlagen.


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.

【问题讨论】:

  • 你能分享一些关于你的环境的信息吗?你是如何安装包的,你还有什么其他的包等等。
  • 看了你的另一篇帖子,你是用基础环境开发的吗??
  • 我刚刚卸载了 anaconda 并在其最新版本中再次安装了所有内容并且它工作正常。谢谢。
  • 不要。没有什么好处,它破坏了使用虚拟环境的意义......特别是考虑到创建环境是多么容易,我认为没有理由不这样做。
  • 您好,终于用tensorflow环境解决了。基础环境在导入 tensorflow 及其库时会出现问题。

标签: python tensorflow anaconda conda


【解决方案1】:

在此(答案)部分中指定答案,即使它出现在评论部分中也是为了社区的利益。

问题通过在New Virtual Environment 中安装Tensorflow 得到解决。

使用虚拟环境具有以下优点

  • 我们可以在多个Virtual Environments 中维护Tensorflow 的多个版本,每个Virtual Environment 包含每个版本,例如1.14, 1.15, 2.0, 2.1, 2.2,etc..
  • 我们可以在每个Virtual Environment中使用不同的Python Versions (2.x, 3.6, 3.7)
  • 如果我们想修改任何Tensorflow APIsource code,我们可以在我们的Virtual Environment 中进行,而不会影响其在其他Virtual Environments 中的功能。

创建一个新的Virtual Environment并在Anaconda中安装Tensorflow的步骤,对于不同的操作系统,如下所示:

# Create a New Virtual Environment
conda create --name TF_2_VE

# When conda asks you to proceed, type y:
proceed ([y]/n)?

# Activate the Virtual Environment. Conda Version > 4.6 
conda activate TF_2_VE

# Activating Virtual Environment, Conda Version < 4.6 and Windows OS
activate TF_2_VE

# Activating Virtual Environment, Conda Version < 4.6 and Linux and Mac OS
source activate TF_2_VE


# Install the TF Version you need
conda install tensorflow

希望这些信息对您有所帮助。快乐学习!

【讨论】:

  • 当 OP 使用 windows 时,这个答案是针对 linux 的
  • 更新了答案,使其更适用于不同的操作系统。感谢您的反馈。
  • 为什么使用 pip 而不是 Conda 安装 TensorFlow??
  • @AMC,将pip install tensorflow==2.1 替换为conda install tensorflow==2.1
  • 为什么是 2.1 版?那是 OP 试图安装的吗?
【解决方案2】:

要安装当前版本的纯 CPU TensorFlow,推荐给初学者:

conda create -n tf tensorflow
conda activate tf

如果你已经有 cuda 软件

conda create -n tf-gpu tensorflow-gpu
conda activate tf-gpu

【讨论】:

  • 不再需要安装 CUDA 软件,一个 conda install tensorflow-gpu 自带了一个预打包版本的 CUDA 工具包和 CUDNN,它驻留在特定的环境中(见这个问题:@ 987654321@)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 2016-08-21
  • 2017-02-11
  • 1970-01-01
相关资源
最近更新 更多