Philokey
Philokey

Reputation: 491

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.

Upvotes: 10

Views: 35221

Answers (3)

FantasyJXF
FantasyJXF

Reputation: 984

make sure you are using the RIGHT python version.

import platform
print(platform.python_version())

I met the problem in Python3, and it worked Okay with Python2.

Upvotes: 0

Shai
Shai

Reputation: 114786

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.

Upvotes: 5

UndeadDragon
UndeadDragon

Reputation: 707

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.

Upvotes: 2

Related Questions