Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python can't import _caffe module #263

Closed
ghost opened this issue Mar 27, 2014 · 56 comments
Closed

python can't import _caffe module #263

ghost opened this issue Mar 27, 2014 · 56 comments

Comments

@ghost
Copy link

ghost commented Mar 27, 2014

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!

@shelhamer
Copy link
Member

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 shelhamer changed the title error running pretrained net? python can't import _caffe module Mar 27, 2014
@ghost
Copy link
Author

ghost commented Mar 27, 2014

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 notifications@github.comwrote:

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//issues/263#issuecomment-38771502
.

@shelhamer
Copy link
Member

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.

@kloudkl
Copy link
Contributor

kloudkl commented Mar 28, 2014

Maybe #256 will help.

@ghost
Copy link
Author

ghost commented Mar 28, 2014

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!

@longjon
Copy link
Contributor

longjon commented Mar 28, 2014

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

@shelhamer
Copy link
Member

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

@ghost
Copy link
Author

ghost commented Apr 1, 2014

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?

@shelhamer
Copy link
Member

Closing with resolution of the pycaffe issue. For the network output question, see #281 (comment).

@dkkim930122
Copy link

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!

@longjon
Copy link
Contributor

longjon commented Jun 2, 2014

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

@dkkim930122
Copy link

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

@ghost
Copy link

ghost commented Aug 3, 2014

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

@bug-fixed
Copy link
Contributor

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

@sausthapit
Copy link

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

@armcortex
Copy link

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

@Prasanna1991
Copy link

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

@object0814
Copy link

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

@wguo68
Copy link

wguo68 commented Feb 8, 2015

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

@abhisheksimhadri
Copy link

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
Copy link

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

@PeterPan1990
Copy link

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

@luweishuang
Copy link

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

@wanghao2020
Copy link

did you install caffe rigth?

2015-06-12 10:23 GMT+08:00 luweishuang notifications@github.com:

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
#263 (comment).

@Shaderboy
Copy link

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

@alviur
Copy link

alviur commented Jul 12, 2015

Same issue here, anyone could solve it?

@rwhchan
Copy link

rwhchan commented Jul 13, 2015

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.

@ShaharKatz
Copy link

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.

@marmikcfc
Copy link

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

@timsamart
Copy link

timsamart commented Aug 31, 2016

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

@Sanggeun-Ahn
Copy link

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.

@mrgloom
Copy link

mrgloom commented Dec 1, 2016

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

@yiran-THU
Copy link

@mrgloom
It works for me,Thank you!

@HARSHITAGUPTA123
Copy link

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

@sayadyaghoobi
Copy link

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

@mammadjv
Copy link

mammadjv commented Mar 16, 2017

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

@MinaRe
Copy link

MinaRe commented Mar 27, 2017

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!

@deepankverma
Copy link

@vokhidovhusan
Copy link

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

@mangwaier
Copy link

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

@niuqun
Copy link

niuqun commented Jan 18, 2018

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.

@Abdoulzen
Copy link

Abdoulzen commented Mar 6, 2018

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 <module>
   import caffe
 File "/home/abba/.local/install/caffe/python/caffe/__init__.py", line 1, in <module>
   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 <module>
   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

@WUhailing
Copy link

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

@Abdoulzen
Copy link

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

@WUhailing
Copy link

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

@Abdoulzen
Copy link

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

@Lusia731
Copy link

@shelhamer
after I followed your steps:
image

I can't find my caffe folder anymore

@fdchiu
Copy link

fdchiu commented Jul 31, 2018

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.

@DongyueHao
Copy link

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

@Hiker01
Copy link

Hiker01 commented Mar 14, 2019

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 .

@surendrallam
Copy link

surendrallam commented Dec 1, 2022

Hai,

Faced the same issue, while importing Caffe after installing Caffe in windows with GPU, could fix it by copying <CAFFE installation>/caffe/python/caffe/ to <Python Directory>/Lib/site-packages

Hope this will help, all the best..!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests