Caffe: python can't import _caffe module

Created on 27 Mar 2014  Â·  55Comments  Â·  Source: BVLC/caffe

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!

downstream problem?

Most helpful comment

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.

All 55 comments

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.

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
.

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.

Maybe #256 will help.

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!

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

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

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?

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

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, I believe you should have caffe/python or caffe/distribute/python in your PYTHONPATH. The caffe folder is itself the module.

@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!

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

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

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

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

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

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

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

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?

And if I give the caffe module path as caffe/python instead of the one in distribute/python
Then the application crashes and I get the following error:

Process: Python [6445]
Path: /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Identifier: Python
Version: 2.7.9 (2.7.9)
Code Type: X86-64 (Native)
Parent Process: bash [6438]
Responsible: Terminal [320]
User ID: 501

Date/Time: 2015-04-25 20:21:57.900 +0530
OS Version: Mac OS X 10.10.2 (14C109)
Report Version: 11
Anonymous UUID: 8D82BD11-4581-1BF6-F12B-DCA9FEDC2491

Time Awake Since Boot: 3300 seconds

Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000000

@longjon
where can i find the PYTHONPATH, I try to use: echo $PYTHONPATH to see the detail, but i got nothing ,3ks

I got the same error as "No module name caffe" and set as @longjon said,also meet error as "ImportError:libcaffe.so: cannot open shared object file:no such file or directory", finally, I do as "http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/classification.ipynb" did and it is solved

did you install caffe rigth?

2015-06-12 10:23 GMT+08:00 luweishuang [email protected]:

I got the same error as "No module name caffe" and set as @longjon
https://github.com/longjon said,also meet error as
"ImportError:libcaffe.so: cannot open shared object file:no such file or
directory", finally, I do as "
http://ouxinyu.github.io/Blogs/example_notebook.html" did and it is solved

—
Reply to this email directly or view it on GitHub
https://github.com/BVLC/caffe/issues/263#issuecomment-111330055.

@luweishuang That link is expired and I'm having the same issue. Can you describe what you did?

Same issue here, anyone could solve it?

I have a similar issue - I did

cmake (flags) ..
make all
make pycaffe

using the cmake procedure and generated _caffe.so, then did an export PYTHONPATH to the right directory (........../caffe/python), but Python (IDLE or IPython) doesn't recognize it. I get the "no module named _caffe" (not "no module named caffe") error.

@rwhchan Download the recent caffe and pip install the module of the python requirements. follow the tourial of the caffe , and export the python_path. I hope you can succeed

Hi everyone, I installed all the requirements on Mac OSX for Caffe, i passed the test when writing ""make runtest"", i installed all the libraries, like boost, snappy... Although it doesn't work when i open python, this is the error message that the shell gave me:

ld: library not found for -lboost_python
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [python/caffe/_caffe.so] Error 1

Please help i feel clueless

Thanks, I followed the tutorial but am not able to get a successful import.

Sent from my Mi phoneOn WH [email protected], 16 Jul 2015 10:15 wrote:@rwhchan Download the recent caffe and pip install the module of the python requirements. follow the tourial of the caffe , and export the python_path. I hope you can succeed

—Reply to this email directly or view it on GitHub.

try changing the /caffe_directory/python/caffe/init.py script. add caffe to all imports such that from .pycaffe import Net changes into from caffe.pycaffe import Net etc.

if this doesn't help, try running help(caffe._caffe) in a python shell and see if the module exists.

i tried to import caffe into a python script with "import caffe" and i get this error:


ImportError Traceback (most recent call last)
in ()
----> 1 import caffe

/home/benedetta/caffe/python/caffe/init.py in ()
----> 1 from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver
2 from ._caffe import set_mode_cpu, set_mode_gpu, set_device, Layer, get_solver, layer_type_list
3 from ._caffe import __version__
4 from .proto.caffe_pb2 import TRAIN, TEST
5 from .classifier import Classifier

/home/benedetta/caffe/python/caffe/pycaffe.py in ()
11 import numpy as np
12
---> 13 from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
14 RMSPropSolver, AdaDeltaSolver, AdamSolver
15 import caffe.io

ImportError: No module named 'caffe._caffe'

i have already set the PYTHONPATH. Infact if i do echo $PYTHONPATH i obtain /home/benedetta/caffe/python:

What also have i to do????
thanks

From your code it seems that you didn't try to change "from .pycaffe import ..." to "from caffe.pycaffe import ..." within the init script.
Try it and see if it works for you.

@ShaharKatz I have a similar problem and I tried changing "from .pycaffe import" to "from caffe.pycaffe import" and it still doesn't work for me.

Can you help me out?

Thanks!

I fixed the problem for me by doing the following (Windows 8.1 with cuda 7.5):

  1. Make sure you have compiled caffe and you have the following file under /Release/pycaffe/caffe you have _caffe.pyd and _caffe.lib
  2. Put the path where _caffe.pyd is located to your PATH environment variable.
    DO NOT PUT IT to PYTHONPATH!
  3. In PYTHONPATH put only your /Release/pycaffe folder and nothing else related to caffe!

this is due to the fact that python pulls the python wrapper dll which is _caffe.pyd on windows and of course it does not know where to look otherwise.

Cheers,

Timos

In my case, I couldn't use caffe with openCV.

When I tried to import caffe on python, it worked.
And, When I tried to import cv2 without importing caffe on python, it worked too.

However, If I tried to import caffe with cv2, it returned error like below.

[libprotobuf ERROR google/protobuf/descriptor_database.cc:57] File already exists in database: caffe.proto
[libprotobuf FATAL google/protobuf/descriptor.cc:954] CHECK failed: generated_database_->Add(encoded_file_descriptor, size):
terminate called after throwing an instance of 'google::protobuf::FatalException'
what(): CHECK failed: generated_database_->Add(encoded_file_descriptor, size):
Aborted (core dumped)

I searched how can I fix these error on the internet. And I followed some answers.
But, I still couldn't fix my error . Please let me know how can I use caffe with openCV.

Seems this helps:
export PYTHONPATH=/home/myuser/Downloads/caffe/python:$PYTHONPATH

@mrgloom
It works for me,Thank you!

Everything built successfully. I even set path in bashrc, but still getting "NO module named caffe" error.
What could be the problem?

i got the same error! anyone has solved this please give us the point. i compiled and set path correctly but still it give me no caffe module found

I fixed my problem! 👍

make pycaffe
make distribute

do not forget to update PYTHONPATH environment variable!!

echo export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH >> ~/.bashrc

I have same problem but I solved it in another way and like to share with you,
after doing:
make all,
make pycaffe, (dont run make distribute!!!!)
nano ~/.bashrc >> in the last line add : export PYTHONPATH=/path/caffe/python
python
import caffe
It should work!

it worked for me. But it shows another errors :(

lastly "source ~/.bashrc" or "source /etc/profile".

Maybe you need to source ~/.bashrc, close the terminal and reopen. Or even make pycaffe again. Make sure you have _caffe.so in the /python/caffe.

Hello everyboby
I have the same problem when i'm trying to make pytest after make all and make runtest.
so i have all dependencies but think i have a path troubles:
Make pytest give me errors like that:
```
cd python; python -m unittest discover -s caffe/test

EEEEEEEEEE

ERROR: test_coord_map (unittest.loader._FailedTest)

ImportError: Failed to import test module: test_coord_map
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/home/abba/.local/install/caffe/python/caffe/test/test_coord_map.py", line 6, in
import caffe
File "/home/abba/.local/install/caffe/python/caffe/__init__.py", line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/home/abba/.local/install/caffe/python/caffe/pycaffe.py", line 13, in
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: dynamic module does not define module export function (PyInit__caffe)
```
Please anybody can help me to fixe it .....thanks

@Abdoulzen Hi may i know how did you fix it?

@WUhailing i just created a new environmet for python2.7 and follow this https://www.youtube.com/watch?v=DnIs4DRjNL4&t=1315s

Thanks @Abdoulzen,I found out that cmake causes this ImportError in python 3, so switch to make and it works damn well.

ok @WUhailing now i'm getting the same errors. Want to know how you fixed it
Thanks

@shelhamer
after I followed your steps:
image

I can't find my caffe folder anymore

In my case, the python path was a relative path after I changed to absolute path such as:

/User/home/project/caffe/python

it started working. So the point is use "Absolute" path.

I face the same problems.When I had down all the steps, I

python
import caffe

the same problems happened
and then I found the version is 2. so I try again

import caffe2

>

NICE!!!!!

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.

Hello,I make caffe with cmake, and someone says that pycaffe interface has been made with cmake. However when I import caffe I got the error (No module named _caffe),and I had set the path .

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sdemyanov picture sdemyanov  Â·  3Comments

erogol picture erogol  Â·  3Comments

kelvinxu picture kelvinxu  Â·  3Comments

shiorioxy picture shiorioxy  Â·  3Comments

inferrna picture inferrna  Â·  3Comments