【问题标题】:Error importing Tensorflow when import tensorflow as tf将 tensorflow 导入为 tf 时导入 TensorFlow 时出错
【发布时间】:2018-11-27 22:28:38
【问题描述】:

我最近完成了 tensorflow 的安装(并且有点挣扎),当我相信我已经得到它时,我现在在运行仅包含 import tensorflow as tf 的文件时遇到这些导入错误,我运行了 cmd: python3 tftest.py 并得到这些导入错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
  File "/usr/lib/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /usr/local/lib/python3.4/dist-packages/tensorflow/python/_pywrap_tensorflow.so: invalid ELF header

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "stockprice.py", line 1, in <module>
    import tensorflow as tf
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/__init__.py", line 60, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
  File "/usr/lib/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /usr/local/lib/python3.4/dist-packages/tensorflow/python/_pywrap_tensorflow.so: invalid ELF header


Error importing tensorflow.  Unless you are using bazel,
you should not try to import tensorflow from its source directory;
please exit the tensorflow source tree, and relaunch your python interpreter
from there.

我确实安装了 tensorflow

Name: tensorflow
Version: 0.12.0
Location: /usr/local/lib/python3.4/dist-packages
Requires: numpy, six, protobuf, wheel

我已经尝试解决这个问题好几个小时了,有没有人遇到过这个或类似的错误?我查看了安装手册,并按照我能做的每一个步骤进行操作。我已经尝试重新安装 protobuf

我没有在 tensorflow 目录上运行我的测试文件。

我真的很感激任何帮助,因为这个错误正在耗尽我的大脑。

【问题讨论】:

  • 您是从源代码构建的吗?它可能会变得凌乱,我建议使用 conda 进行干净简单的安装。

标签: python tensorflow import installation


【解决方案1】:

尝试手动删除库文件并使用 pip 重新安装

【讨论】:

  • 哇,这工作..我想在我的整个安装过程中我一定搞砸了一些东西。我去了 tensorflow 文件夹和 /bin/rm -r 所有 tensorflow 文件夹,使用 pip3 install --user --upgrade tensorflow 重新安装,现在运行代码没有导入错误,希望现在可以正确使用 tensorflow
  • 如果您之前安装了其他一些与 tensorflow 混淆的软件包,就会发生这种情况。不知道为什么会这样
【解决方案2】:

检查 pip list 是否没有安装 tensorflow-gpu 库,因为某些 GPU 不受支持。

如果是这种情况,卸载 tensor flow-gputensorflow-estimator 重新安装 tensorflow :

pip uninstall tensorflow-gpu

pip uninstall tensorflow-estimator

pip install tensorflow

确保您使用带有pip -V 命令的python 3.6

【讨论】:

    【解决方案3】:

    我所做的是点击

    运行时 -> 重新启动运行时

    然后重新运行代码,它就像魔术一样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 2017-08-30
      • 2021-03-04
      • 2018-01-22
      相关资源
      最近更新 更多