caffe icon indicating copy to clipboard operation
caffe copied to clipboard

python can't import _caffe module

Open ghost opened this issue 12 years ago • 56 comments

I suspect this is some stupid set-up problem that I make since no one else seems to have trouble running it..

So I was trying to run the pre-trained network by following the instruction, when I tried to run the following command:

python ../python/caffe/detection/detector.py --crop_mode=selective_search --pretrained_model=../examples/imagenet/caffe_reference_imagenet_model --model_def=../examples/imagenet/imagenet_deploy.prototxt _temp/cat.txt _temp/cat.h5

I got an error saying:

Traceback (most recent call last):
  File "detector.py", line 29, in <module>
    import caffe
  File "/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net
  File "/caffe/pycaffe.py", line 6, in <module>
    from ._caffe import CaffeNet
ImportError: No module named _caffe

It seems like python is not recognizing the _caffe.cpp file...Would anyone know how to solve this problem?

Thanks tons!

ghost avatar Mar 27 '14 04:03 ghost

Make sure you have done

make pycaffe

and that you have copied the caffe/python/caffe dir into your PYTHONPATH. The wrapper needs to be compiled–just having the cpp isn't enough. Further, just compiling it doesn't do much good if python can't find it, which is why you need to install it somewhere in your python path.

shelhamer avatar Mar 27 '14 05:03 shelhamer

Hi,

Thanks very much for your reply Evan!

I had trouble finding the makefile for pycaffe, the caffe dir is in my pythonpath, but I do not have the makefile there..am I missing some files or steps?

On Thu, Mar 27, 2014 at 1:57 AM, Evan Shelhamer [email protected]:

Make sure you have done

make pycaffe

and that you have copied the caffe/python/caffe dir into your PYTHONPATH. The wrapper needs to be compiled-just having the cpp isn't enough. Further, just compiling it doesn't do much good if python can't find it, which is why you need to install it somewhere in your python path.

Reply to this email directly or view it on GitHubhttps://github.com/BVLC/caffe/issues/263#issuecomment-38771502 .

ghost avatar Mar 27 '14 15:03 ghost

Please take another look at the compilation section of the installation guide, above all the parts about make pycaffe and make distribute. You need to build everything first from Caffe root dir, then once you have made pycaffe, which yields a _caffe.so module, you place the distribute/python dir in your python path.

Good luck.

shelhamer avatar Mar 27 '14 16:03 shelhamer

Maybe #256 will help.

kloudkl avatar Mar 28 '14 12:03 kloudkl

I have done

make all make pycaffe make distribute

my PYTHONPATH looks like this:

/.../caffe_temp/python/caffe:/usr/lib64/python2.6:/.../caffe_temp/distribute/python:/usr/lib64/python2.6/site-packages:/.../caffe_temp/python

I have the _caffe.so module under caffe/python/caffe, but when I run

python pycaffe.py

I still get the error

Traceback (most recent call last): File "pycaffe.py", line 10, in from ._caffe import CaffeNet

Thanks!

ghost avatar Mar 28 '14 17:03 ghost

pycaffe.py is not a script that you can run directly; you should, however, be able to import caffe.

longjon avatar Mar 28 '14 21:03 longjon

Please inspect your PYTHONPATH as it looks wrong. You should give absolute paths to the caffe module's parent directory. For instance, in my configuration I did:

# in caffe root dir
make all
make pycaffe
make distribute
# make dir for custom python modules, install caffe
mkdir ~/python
mv distribute/python/caffe ~/python
# set PYTHONPATH (this should go in your .bashrc or whatever
PYTHONPATH=${HOME}/python:$PYTHONPATH

shelhamer avatar Mar 29 '14 00:03 shelhamer

I created a new directory under home folder and it worked fine, thanks!

Out of curiosity, I wasn't able to find any documentation in terms of how to view the top prediction results for cifar10. Would there be any implementation in caffe that would show the prediction?

ghost avatar Apr 01 '14 05:04 ghost

Closing with resolution of the pycaffe issue. For the network output question, see https://github.com/BVLC/caffe/issues/281#issuecomment-39533131.

shelhamer avatar Apr 04 '14 05:04 shelhamer

Hello, I had the same (or similar) problem! I tried to do one of the examples in the Caffe website: Image Classification [notebook]. I was able to import numpy and matplotlib.pyplot, but when I tried to import caffe, I got the following error:

import caffe Traceback (most recent call last): File "", line 1, in ImportError: No module named caffe

I successfully did: make all make pycaffe make distribute

I also added python/caffe & distribute/python/caffe to my PYTHONPATH.

Could someone please help with this issue? Thanks a lot in advance!

dkkim930122 avatar Jun 02 '14 22:06 dkkim930122

@dkkim930122, I believe you should have caffe/python or caffe/distribute/python in your PYTHONPATH. The caffe folder is itself the module.

longjon avatar Jun 02 '14 22:06 longjon

@longjon I appreciate your help! I think it worked! But now it shows another error message for importing caffe:

import caffe Traceback (most recent call last): File "", line 1, in File "/home/dkkim930122/caffe-master/distribute/python/caffe/init.py", line 1, in from .pycaffe import Net, SGDSolver File "/home/dkkim930122/caffe-master/distribute/python/caffe/pycaffe.py", line 10, in from ._caffe import Net, SGDSolver ImportError: libmkl_rt.so: cannot open shared object file: No such file or directory

I tried: export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda/lib64 But, this didn't work... Thank you again!

dkkim930122 avatar Jun 03 '14 00:06 dkkim930122

I am experiencing the same problem when trying to install scipy, even though /opt/intel/mkl/lib/intel64 is in LD_LIBRARY_PATH.

ghost avatar Aug 03 '14 21:08 ghost

try source /opt/intel/bin/compilervars.sh intel64, then install This command will load the environment setting.

bug-fixed avatar Aug 03 '14 22:08 bug-fixed

Has anyone found a solution to this problem? I am also getting same kind of error as this

import caffe Traceback (most recent call last): File "", line 1, in File "/home/dkkim930122/caffe-master/distribute/python/caffe/init.py", line 1, in from .pycaffe import Net, SGDSolver File "/home/dkkim930122/caffe-master/distribute/python/caffe/pycaffe.py", line 10, in from ._caffe import Net, SGDSolver ImportError: libmkl_rt.so: cannot open shared object file: No such file or directory

my $PYTHONPATH is set to following /home/username/Dropbox/Study/git/caffe/distribute/python: Thanks

sausthapit avatar Aug 21 '14 17:08 sausthapit

I have same problem

I have done: make all make pycaffe make distribute set PYTHONPAYH

It works, but I dont know why after restart computer it shows this error

Traceback (most recent call last):
File "examples/web_demo/app.py", line 16, in
import caffe
ImportError: No module named caffe

have checked command source ~/.bashrc
still dont work

armcortex avatar Oct 04 '14 09:10 armcortex

When I try to make pycaffe, files like _caffe.cpp and _caffe.hpp; have #include "caffe/caffe.hpp" ... The error seems to occur due to unavailability of this module ... Does anyone knows the solution?? Thnks in advance

Prasanna1991 avatar Oct 17 '14 07:10 Prasanna1991

@shelhamer, thanks i solved no module named caffe problem :)

ghost avatar Nov 03 '14 07:11 ghost

I am get the similiar error, but I don't know how to set the PATHONPATH . How to do ?

wguo68 avatar Feb 08 '15 12:02 wguo68

I am also facing a similar problem. I tried doing all of this. I am getting the following error:

ImportError: dlopen(caffe/_caffe.so, 2): Library not loaded: @rpath/libcaffe.so Referenced from: /Users/abhisheksimhadri/Documents/Github/caffe/distribute/python/python/caffe/_caffe.so Reason: image not found

make all make test make runtest make pycaffe make distribute

All work fine ...

What am I doing wrong?

abhisheksimhadri avatar Apr 25 '15 14:04 abhisheksimhadri