ImportError: dynamic module does not define module export function (PyInit__caffe)

32,336

Solution 1

Update
Caffe supports python 3.3+. Please checkout installation guide and prerequisites.

Original (outdated) answer
Using caffe with python 3 is not currently supported:

Caffe’s Python interface works with Python 2.7. Python 3 or earlier Pythons are your own adventure.

See caffe's installation tutorial.

Solution 2

It is now possible to build Caffe for Python3, and I'm almost sure it was possible in December 16 when the question was asked.

To do this, you need to remove the comments in the Makefile.config with Python3:

# Uncomment to use Python 3 (default is Python 2)
# Check that boost library name is correct here!!!
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
                 /usr/lib/python3.5/dist-packages/numpy/core/include

But therefore you will have caffe only in python3 OR python2, because of the way how caffe installs (with PYTHON_PATH, not a good way indeed).

To workaround this, you can do such trick in your ~/.bashrc:

alias python2="export PYTHONPATH=$PYTHONPATH:/home/undead/reps/caffe_py2/python && python2"
alias python3="export PYTHONPATH=$PYTHONPATH:/home/undead/reps/caffe_py3/python && python3"
alias python="export PYTHONPATH=$PYTHONPATH:/home/undead/reps/caffe_py2/python && python2"

Therefore both will works.

Share:
32,336
Philokey
Author by

Philokey

CS student

Updated on July 16, 2022

Comments

  • Philokey
    Philokey almost 2 years

    I install caffe with python3,but when I import caffe, I get some errors Traceback (most recent call last):

     File "classify.py", line 14, in <module>
        import caffe
      File "/home/hez/caffe-master/python/caffe/__init__.py", line 1, in <module>
        from .pycaffe import Net, SGDSolver
      File "/home/hez/caffe-master/python/caffe/pycaffe.py", line 13, in <module>
        from ._caffe import Net, SGDSolver
    ImportError: dynamic module does not define module export function (PyInit__caffe)
    

    But it work well in python2.7.

    I had add /path/to/caffe/distrubute/python to the PATH, but when I make pycaffe, it shows that

    make: Nothing to be done for `pycaffe'.
    

    How can I solve this problem? Thank you very much.

    • Nate M
      Nate M about 5 years
      You can also change the python version from 2 to 3 in the CMakeLists.txt file now.
  • Ravikrn
    Ravikrn over 6 years
    Do I need to do "make all" after doing this changes?
  • UndeadDragon
    UndeadDragon over 6 years
    @Ravikrn no, you need to build both libraries (.so) and point them in a such way.
  • Ravikrn
    Ravikrn over 6 years
    Hey is it still the case? caffe installation instructions shows python3 as well. Can you update your answer in light of new information
  • Ravikrn
    Ravikrn over 6 years
    Can you please elaborate. I just want python3 and Initially I installed caffe with python2 instructions. What do you mean by build both libraries? My ~/.bashrc has "export PYTHONPATH=/home/path/caffe/python:$PYTHONPATH"
  • UndeadDragon
    UndeadDragon over 6 years
    @Ravikrn in your case you need just to rebuilt caffe with python3. But after that python2 caffe will be lost.
  • user7610
    user7610 almost 5 years
    If you rebuild the module with correct python version, you have to exit the interpreter before trying to import the module again interactively. Otherwise, python will just keep printing the same error, even though files are now right.