在ubuntu上导入caffe时,ImportError无法导入名称BytesIO

时间:2014-12-10 08:49:26

标签: python python-2.7 ubuntu scikit-image caffe

我想在装有Ubuntu 12.04LTS的机器上运行caffe。 完成Installation page上的所有步骤后,我成功训练了LeNet模型并尝试将其用作here的教程。然后我收到以下错误:

Traceback (most recent call last): 
    File "<string>", line 1, in <module>
ImportError: No module named caffe
Error in sys.excepthook:
Traceback (most recent call last):
    File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
      from apport.fileutils import likely_packaged, get_recent_crashes
    File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
      from apport.report import Report
    File "/usr/lib/python2.7/dist-packages/apport/report.py", line 18, in <module>
      import problem_report
    File "/usr/lib/python2.7/dist-packages/problem_report.py", line 14, in <module>
      import zlib, base64, time, sys, gzip, struct, os
    File "/usr/lib/python2.7/gzip.py", line 10, in <module>
      import io
    File "${HOME}/path/to/caffe/python/caffe/io.py", line 2, in <module>
      import skimage.io
    File "/usr/local/lib/python2.7/dist-packages/skimage/io/__init__.py", line 11, in <module>
      from ._io import *
    File "/usr/local/lib/python2.7/dist-packages/skimage/io/_io.py", line 1, in <module>
      from io import BytesIO
ImportError: cannot import name BytesIO

Original exception was:
Traceback (most recent call last): 
    File "<string>", line 1, in <module>
ImportError: No module named caffe

在我执行上述操作之前,我在PYTHONPATH文件中设置了.bashrc。 问题是什么?任何人都可以给出一些暗示吗?我真的很困惑。 在目录中运行命令python -c 'import io; print io.__file__'之后:

Traceback (most recent call last): 
    File "${HOME}/path/to/caffe/python/caffe/io.py", line 2, in <module>
      import skimage.io
    File "/usr/local/lib/python2.7/dist-packages/skimage/io/__init__.py", line 11, in <module>
      from ._io import *
    File "/usr/local/lib/python2.7/dist-packages/skimage/io/_io.py", line 1, in <module>
      from io import BytesIO
ImportError: cannot import name BytesIO
Error in sys.excepthook:
Traceback (most recent call last):
    File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
      from apport.fileutils import likely_packaged, get_recent_crashes
    File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
      from apport.report import Report
    File "/usr/lib/python2.7/dist-packages/apport/report.py", line 18, in <module>
      import problem_report
    File "/usr/lib/python2.7/dist-packages/problem_report.py", line 14, in <module>
      import zlib, base64, time, sys, gzip, struct, os
    File "/usr/lib/python2.7/gzip.py", line 10, in <module>
      import io
    File "${HOME}/path/to/caffe/python/caffe/io.py", line 2, in <module>
      import skimage.io
    File "/usr/local/lib/python2.7/dist-packages/skimage/io/__init__.py", line 11, in <module>
      from ._io import *
    File "/usr/local/lib/python2.7/dist-packages/skimage/io/_io.py", line 1, in <module>
      from io import BytesIO
ImportError: cannot import name BytesIO

Original exception was:
Traceback (most recent call last): 
    File "${HOME}/path/to/caffe/python/caffe/io.py", line 2, in <module>
      import skimage.io
    File "/usr/local/lib/python2.7/dist-packages/skimage/io/__init__.py", line 11, in <module>
      from ._io import *
    File "/usr/local/lib/python2.7/dist-packages/skimage/io/_io.py", line 1, in <module>
      from io import BytesIO
ImportError: cannot import name BytesIO

那么,问题就变成了:如何解决名称问题? P.S。:我还在the repository of caffe插入了一个问题。

2 个答案:

答案 0 :(得分:11)

您的Python路径中似乎有一个名为io的包或模块,它正在屏蔽标准库包。它是导入的,但没有要导入的BytesIO对象。

尝试跑步:

python -c 'import io; print io.__file__'

在运行教程的同一位置,重命名或移动该导入命名的文件,假设它不是标准库版本(以lib/python2.7/io.pyc结尾)。

可能是您将Python路径设置为错误的目录。你应该包括path/to/caffe/python,而不是path/to/caffe/python/caffe,也不应该尝试运行python,后者作为你当前的工作目录。在两种情况下caffe/python/caffe/io.py而不是标准库版本。

这里的安装说明没有错;他们明确告诉你使用:

export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH

请注意该路径末尾缺少/caffe

答案 1 :(得分:2)

我也遇到了这个问题,按照BVLC github repo上所述的脚本在AWS ubuntu 14.04实例上安装caffe:"Caffe on EC2 Ubuntu 14.04"

我按照指示设置了python路径。正如@Martijn Pieters所诊断的那样,问题在于caffe正在导入自己的io库,然后导入scikit-image的io库,而后者正在尝试(但失败)加载标准的python io库( BytesIO所在的位置)。相反,由于python路径,当scikit-images尝试从模块io导入BytesIO时,它会循环返回到caffe的io模块。

我还发现,即使没有尝试导入caffe,但由于设置了我的python路径以包含caffe,这个问题也会在其他地方遇到。

可能有几种方法可以解决这个问题。但实质是咖啡的顶级进口是错误的。为了验证这一点,我改变了caffe代码,如下所示:

  1. 我将.../caffe/io.py模块重命名为.../caffe/caffe_io.py是安全的(尽管有正确的命名空间关注,但这不是必需的)

  2. 我修改了pycaffe.py模块顶部的导入:import caffe.ioimport caffe.caffe_io

  3. 我以同样的方式修改了__init__.py中的导入(从import caffe.ioimport caffe.caffe_io

  4. 现在,当你从python导入io时,它不会在caffe中获取io库。当你导入caffe时,它会导入它的自定义caffe_io库,一切都应该很好。您可能希望对python caffe模块进行更彻底的扫描,以确保我不会忽视导入需要更改的其他位置。

    我希望这会有所帮助。也许当我有时间时,我会发出一个拉动请求,对caffe github repo进行这些(或类似的)更改。