【问题标题】:Error running basic tensorflow example运行基本张量流示例时出错
【发布时间】:2016-06-27 11:09:39
【问题描述】:

我刚刚在 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 环境进行其他更改?

【问题讨论】:

    标签: python ubuntu tensorflow


    【解决方案1】:

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

    一个常见的解决方案是在启动pythonipython 之前将cd 移出/git/tensorflow 目录。

    【讨论】:

    • 抱歉耽搁了。看来你是对的:我刚从我的主目录启动 ipython,它工作正常 import tensorflow as tf
    • 这对我不起作用。我可以看到 C:\Python35\Lib\site-packages\tensorflow\python_pywrap_tensorflow_internal.pyd 但得到错误:from tensorflow.python.pywrap_tensorflow_internal import * ImportError: DLL load failed: The specified module could not be found.
    • 我仍然对这个解决方案摸不着头脑。它可以工作,但是 python 包不应该依赖当前工作目录来加载依赖项。结果,我似乎无法将我的 IDE (pydev) 配置为使用tensorflow-gputensorflow (cpu) 工作正常。我错过了什么吗?
    【解决方案2】:

    下面的命令帮助了我。

     pip install tensorflow --upgrade --force-reinstall
    

    【讨论】:

      【解决方案3】:

      我在 Python 2.7 虚拟环境 (venv) 中从源代码 (GitHub: https://github.com/tensorflow/tensorflow) 编译和安装了 TensorFlow。它工作得很好,但我需要(正如其他人提到的,例如Error running basic tensorflow example 的用户“mrry”)从我编译 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. 源激活那个 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 笔记本中。

      【讨论】:

        【解决方案4】:

        Tensorflow 2.0 兼容答案:mrry 的解决方案和 neustart47 对 Tensorflow 版本 >= 2.0 的工作。

        答案 1:cd 在启动 python 或 ipython 之前离开 /git/tensorflow 目录。

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

        【讨论】:

          【解决方案5】:

          在我的python -m venv environment 中,它似乎是由以下人员修复的:

          pip uninstall tensorflow
          

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

          pip install -r requirements.txt
          

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

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

              pip install --upgrade

              这是要下载的link

              【讨论】:

                【解决方案8】:

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

                将您的 python 版本从 3.7 降级到 3.6

                conda install python=3.6.8
                

                【讨论】:

                  【解决方案9】:

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

                  【讨论】:

                    【解决方案10】:

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

                    解决方案 1:

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

                    解决方案 2:

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

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

                    希望它会起作用。

                    【讨论】:

                      【解决方案11】:

                      我解决了这个问题。 试试下面的命令:

                      pip install --upgrade pip

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2018-05-04
                        • 1970-01-01
                        • 1970-01-01
                        • 2019-01-30
                        • 2012-10-08
                        • 2017-02-03
                        • 2017-12-27
                        • 2016-03-10
                        相关资源
                        最近更新 更多