运行基本的TensorFlow示例时出错


67

我刚刚在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

当按照说明进行测试时,它失败并无法导入名称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环境是否需要进行其他更改?

Answers:


173

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

常见的解决方案是在启动或之前cd退出/git/tensorflow目录。pythonipython


3
对于延迟,我们深表歉意。看来您是对的:我刚刚从我的主目录启动了ipython,它运行正常 import tensorflow as tf
StephenBoesch

这对我不起作用。我可以看到C:\ Python35 \ Lib \ site-packages \ tensorflow \ python_pywrap_tensorflow_internal.pyd,但得到的错误是:从tensorflow.python.pywrap_tensorflow_internal import * ImportError:DLL加载失败:找不到指定的模块。
史蒂文·马克·福特

1
我仍在为这种解决方案挠头。它可以工作,但是python包永远不要依赖当前工作目录来加载依赖项。结果,我似乎无法配置要使用的IDE(pydev)tensorflow-gputensorflow(cpu)工作正常。我想念什么吗?
马赫

10

下面的命令帮助了我。

 pip install tensorflow --upgrade --force-reinstall

4

我在Python 2.7虚拟环境(venv)中从源代码(GitHub:https : //github.com/tensorflow/tensorflow)编译安装了TensorFlow 。它工作正常,但我需要(如其他人所提到的,例如Error运行基本tensorflow示例的错误处的用户“ mrry” )cd从我编译TensorFlow的分区中移出到另一个分区,以便能够导入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桌面环境

安装步骤:

根据您的喜好修改路径,虚拟名称。

  1. 创建tf-env:

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

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

  1. 源激活该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 venv中安装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笔记本中。


1

在我python -m venv environment看来,它是通过以下方式解决的:

pip uninstall tensorflow

更改requirements.txttensorflow==1.5.0不是tensorflow==1.8.0

pip install -r requirements.txt

1

如果您使用的是2011年之前制造的CPU,则将tensorflow版本从1.8.0降级为1.5.0或1.2.0并尝试导入对我有用的模块。


1

Tensorflow 2.0兼容答案:mrry的解决方案和neustart47都适用于Tensorflow版本> = 2.0。

答案1:在启动python或ipython之前先cd退出/git/tensorflow目录。

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



0

我发现TensorFlow 1.12.0仅适用于Python版本3.5.2。我有Python 3.7,但没有用。因此,我不得不降级Python,然后可以安装TensorFlow使其工作。

要将python版本从3.7降级到3.6

conda install python=3.6.8


0

我也有这个问题,并且已经研究了很长时间。似乎在python 3+上没有这样的问题。在py2.7上-问题实际上出在google.protobuf上

解决方案1:

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

解决方案2:

__init__.py在“ google”文件夹中创建一个。

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

希望它会起作用。


-7

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

pip install --upgrade pip

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.