错误 运行 基本 tensorflow 示例

Error running basic tensorflow example

我刚刚在 ubuntu 上重新安装了最新的 tensorflow:

$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl
[sudo] password for ubuntu: 
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting tensorflow==0.7.1 from https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl
  Downloading https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl (13.8MB)
    100% |████████████████████████████████| 13.8MB 32kB/s 
Requirement already up-to-date: six>=1.10.0 in /usr/local/lib/python2.7/dist-packages (from tensorflow==0.7.1)
Requirement already up-to-date: protobuf==3.0.0b2 in /usr/local/lib/python2.7/dist-packages (from tensorflow==0.7.1)
Requirement already up-to-date: wheel in /usr/local/lib/python2.7/dist-packages (from tensorflow==0.7.1)
Requirement already up-to-date: numpy>=1.8.2 in /usr/local/lib/python2.7/dist-packages (from tensorflow==0.7.1)
Requirement already up-to-date: setuptools in /usr/local/lib/python2.7/dist-packages (from protobuf==3.0.0b2->tensorflow==0.7.1)
Installing collected packages: tensorflow
  Found existing installation: tensorflow 0.7.1
    Uninstalling tensorflow-0.7.1:
      Successfully uninstalled tensorflow-0.7.1
Successfully installed tensorflow-0.7.1

当按照说明进行测试时,它失败了 cannot import name pywrap_tensorflow:

$ ipython

/git/tensorflow/tensorflow/__init__.py in <module>()
     21 from __future__ import print_function
     22 
---> 23 from tensorflow.python import *

/git/tensorflow/tensorflow/python/__init__.py in <module>()
     43 _default_dlopen_flags = sys.getdlopenflags()
     44 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL)
---> 45 from tensorflow.python import pywrap_tensorflow
     46 sys.setdlopenflags(_default_dlopen_flags)
     47 

ImportError: cannot import name pywrap_tensorflow

我的 python 或 ubuntu/bash 环境是否需要额外更改?

从堆栈跟踪中的路径 (/git/tensorflow/tensorflow/…) 看来,您的 Python 路径可能正在从源目录而不是您安装的版本加载 tensorflow 库。结果,无法找到安装在不同目录中的(已编译)pywrap_tensorflow 库。

一个常见的解决方案是在开始 pythonipython.

之前 cd 离开 /git/tensorflow 目录

我解决了这个问题。 尝试以下命令:

pip install --upgrade pip

我从源代码 (GitHub: https://github.com/tensorflow/tensorflow) in a Python 2.7 virtual environment (venv). It worked fine, but I needed (as others have mentioned, e.g. user "mrry" at ) 编译、安装了 TensorFlow,从我编译 TensorFlow 的分区 cd 到另一个分区,以便能够导入 tensorflow进入 Python。否则,我会收到各种错误,具体取决于我所在的(源分区)目录。澄清一下:

         source: /mnt/Vancouver/apps/tensorflow
can't import tf: Python launched in any of /mnt/...
  can import tf: Python launched in /home/victoria/...

后来我只是按照这里的说明操作,

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#anaconda-installation

一切正常,很好。

作为参考,我正在手术

  • Arch Linux [4.6.3-1-ARCH] x86_64
  • 英特尔 i7-4790
  • xfce 4.12 桌面环境

安装步骤:

根据您的喜好修改路径、venv 名称。

  1. 创建 tf-env:

    cd /home/victoria/anaconda3/envs
    
    conda create -n tf-env python=2.7 anaconda
    

注意:附加 'anaconda' 元包会安装所有 Anaconda 包(NumPy;...)。

  1. source 激活那个venv (tf-env)

    source activate tf-env
    

注意:添加到 ~/.bashrc 作为别名:

alias tf='echo "  [TensorFlow in Anaconda Python 2.7 venv (source activate tf]" && source activate tf-env'
  1. 在 tf-env 环境中安装 TensorFlow:

    (tf-env)$ conda install -c conda-forge tensorflow
    

这避免了使用 pip(包括 *.whl 安装脚本),这是一种安装 TensorFlow 的替代方法,如下所述:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md).

成功了!

(tf-env)[victoria@victoria ~]$ P

  [P: python]
Python 2.7.12 |Anaconda 4.1.1 (64-bit)| (default, Jul  2 2016, 17:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

>>> import tensorflow
>>> print tensorflow.__version__
0.9.0
>>> [Ctrl-D]

(tf-env)[victoria@victoria ~]$

然后您可以在 tf-env 中使用 TensorFlow;例如,在这个 (tf-env) venv 中启动的 Jupyter 笔记本中。

下面的命令帮助了我。

 pip install tensorflow --upgrade --force-reinstall

在我的 python -m venv environment 中,它似乎被修复了:

pip uninstall tensorflow

requirements.txt 改为 tensorflow==1.5.0 而不是 tensorflow==1.8.0

pip install -r requirements.txt

如果您使用 CPU 2011 年之前生产的产品,请将 tensorflow 版本从 1.8.0 降级到 1.5.0 或 1.2.0,然后尝试导入它对我有用的模块。

通过以下命令安装解决了问题:

pip install --upgrade

这是link下载

我发现 TensorFlow 1.12.0 仅适用于 Python 版本 3.5.2。我有 Python 3.7,但没有用。所以,我不得不降级 Python 然后我可以安装 TensorFlow 它可以正常工作。

将 python 版本从 3.7 降级到 3.6

conda install python=3.6.8

我正在使用 tensorflow 1.13.1。只是重新安装 tensorflow 对我有用。

我也有这个问题,研究了很久。 python 3+上好像没有这个问题。在py2.7-问题实际上是在google.protobuf

解决方案一:

pip uninstall protobuf
pip uninstall google
pip install google
pip install protobuf
pip install google-cloud

方案二:

在 "google" 文件夹中创建一个 __init__.py

cd /path/to/your/env/lib/python2.7/site-packages/google
touch __init__.py

希望它能奏效。

Tensorflow 2.0 Compatible Answer: mrry 的解决方案和 neustart47 的解决方案都适用于 Tensorflow Version >= 2.0.

答案 1:cd 在开始之前从 /git/tensorflow 目录中退出 python 或者ipython.

答案 2:pip install tensorflow --upgrade --force-reinstall
!pip install tensorflow==2.0 --force-reinstall