Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 786
Philokey Avatar asked Dec 15 '15 17:12

Philokey


2 Answers

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.

like image 200
Shai Avatar answered Nov 11 '22 17:11

Shai


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.

like image 34
UndeadDragon Avatar answered Nov 11 '22 16:11

UndeadDragon