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
taliax opened this issue Mar 27, 2014 · 55 comments

Comments

@taliax
Copy link

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

This comment has been minimized.

Copy link
Member

commented Mar 27, 2014

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

@taliax

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Member

commented Mar 27, 2014

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

This comment has been minimized.

Copy link
Contributor

commented Mar 28, 2014

Maybe #256 will help.

@taliax

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Contributor

commented Mar 28, 2014

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

@shelhamer

This comment has been minimized.

Copy link
Member

commented Mar 29, 2014

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

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Member

commented Apr 4, 2014

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

@shelhamer shelhamer closed this Apr 4, 2014

@dkkim930122

This comment has been minimized.

Copy link

commented Jun 2, 2014

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Jun 3, 2014

@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

This comment has been minimized.

Copy link

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.

@zszhong

This comment has been minimized.

Copy link
Contributor

commented Aug 3, 2014

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

@sausthapit

This comment has been minimized.

Copy link

commented Aug 21, 2014

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

This comment has been minimized.

Copy link

commented Oct 4, 2014

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

This comment has been minimized.

Copy link

commented Oct 17, 2014

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

@ghost

This comment has been minimized.

Copy link

commented Nov 3, 2014

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

@wguo68

This comment has been minimized.

Copy link

commented Feb 8, 2015

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

@abhisheksimhadri

This comment has been minimized.

Copy link

commented Apr 25, 2015

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

This comment has been minimized.

Copy link

commented Apr 25, 2015

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

This comment has been minimized.

Copy link

commented May 1, 2015

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

@luweishuang

This comment has been minimized.

Copy link

commented Jun 12, 2015

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

This comment has been minimized.

Copy link

commented Jun 12, 2015

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

This comment has been minimized.

Copy link

commented Jul 8, 2015

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

@alviur

This comment has been minimized.

Copy link

commented Jul 12, 2015

Same issue here, anyone could solve it?

@rwhchan

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Feb 20, 2016

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.

@Benedetta89

This comment has been minimized.

Copy link

commented Mar 9, 2016

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

@ShaharKatz

This comment has been minimized.

Copy link

commented Mar 9, 2016

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

This comment has been minimized.

Copy link

commented Jun 15, 2016

@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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Oct 26, 2016

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

This comment has been minimized.

Copy link

commented Dec 1, 2016

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

@yiran-THU

This comment has been minimized.

Copy link

commented Dec 16, 2016

@mrgloom
It works for me,Thank you!

@HARSHITAGUPTA123

This comment has been minimized.

Copy link

commented Jan 31, 2017

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

@sayadyaghoobi

This comment has been minimized.

Copy link

commented Mar 15, 2017

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

@Jeonguuz

This comment has been minimized.

Copy link

commented Aug 7, 2017

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

@mangwaier

This comment has been minimized.

Copy link

commented Dec 20, 2017

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

@niuqun

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Mar 22, 2018

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

@Abdoulzen

This comment has been minimized.

Copy link

commented Mar 26, 2018

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

@WUhailing

This comment has been minimized.

Copy link

commented Mar 26, 2018

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

@Abdoulzen

This comment has been minimized.

Copy link

commented Mar 26, 2018

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

@Lusia731

This comment has been minimized.

Copy link

commented Apr 24, 2018

@shelhamer
after I followed your steps:
image

I can't find my caffe folder anymore

@fdchiu

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Dec 25, 2018

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

This comment has been minimized.

Copy link

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 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.