ImportError cannot import name BytesIO when import caffe on ubuntu

后端 未结 2 959
终归单人心
终归单人心 2021-02-15 18:39

I am trying to make caffe running on my machine equipped with Ubuntu 12.04LTS. After finishing all the steps on the Installation page, I trained the LeNet model successfully an

相关标签:
2条回答
  • 2021-02-15 19:08

    I ran into this problem as well, installing caffe on an AWS ubuntu 14.04 instance following the script as outlined on the BVLC github repo here: "Caffe on EC2 Ubuntu 14.04".

    I have setup the python path as instructed. As diagnosed by @Martijn Pieters, the problem is that caffe is importing its own io library, which is then importing scikit-image's io library, which in turn is trying (but failing) to load the standard python io library (where BytesIO is located). Instead, due to the python path, when scikit-image tries to import BytesIO from the module io, it is circularly leading back to caffe's io module.

    I also found that even when not trying to import caffe, but due to having set my python path to include caffe, that this same problem hits me elsewhere.

    There are probably several ways to address this. But the essence is that the top-level import of caffe is at fault. To verify this, I altered the caffe code as follows:

    1. I renamed .../caffe/io.py module to .../caffe/caffe_io.py to be safe (although with correct namespace care, this shouldn't be necessary)

    2. I modified the import at the top of the pycaffe.py module from: import caffe.io to import caffe.caffe_io

    3. I modified the import in __init__.py the same way (from import caffe.io to import caffe.caffe_io)

    Now, when you import io from python, it won't pick up the io library in caffe. When you import caffe, it will import its custom caffe_io library, and all should be well. You may want to do a more thorough scan through the python caffe modules to ensure I haven't overlooked other places where the import needs to change.

    I hope this helps. Perhaps when I have time, I'll issue a pull request with these (or similar) changes to the caffe github repo.

    0 讨论(0)
  • 2021-02-15 19:23

    You appear to have a package or module named io in your Python path that is masking the standard library package. It is imported instead but doesn't have a BytesIO object to import.

    Try running:

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

    in the same location you are running the tutorial and rename or move the file named by that import, presuming it is not the standard library version (ending in lib/python2.7/io.pyc).

    It could be you set your Python path to the wrong directory. You should include path/to/caffe/python, not path/to/caffe/python/caffe, nor should you try and run python with the latter as your current working directory. In both cases caffe/python/caffe/io.py instead of the standard library version.

    The installation instructions are not at fault here; they clearly tell you to use:

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

    Note the lack of /caffe at the end of that path.

    0 讨论(0)
提交回复
热议问题