shap icon indicating copy to clipboard operation
shap copied to clipboard

cannot import name '_cext' !!

Open statgirl101 opened this issue 4 years ago • 10 comments

Hi there, I am really struggling to get SHAP to work and was hoping someone might be able to help me. This has been an incredibly painful process.

  • So, I start by cloning the repository: ! git clone git://github.com/slundberg/shap.git shap1
  • Then, in order to complete the installation, I went: ! python setup.py build How do you know if the build was successful? Mine starts out ok, but then I get the following issue/warning message halfway through:

building 'shap._cext' extension creating build/temp.linux-x86_64-3.5 creating build/temp.linux-x86_64-3.5/shap gcc -pthread -B /usr/local/envs/py3env/compiler_compat -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/envs/py3env/include/python3.5m -I/usr/local/envs/py3env/lib/python3.5/site-packages/numpy/core/include -c shap/_cext.cc -o build/temp.linux-x86_64-3.5/shap/_cext.o cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ g++ -pthread -shared -L/usr/local/envs/py3env/lib -B /usr/local/envs/py3env/compiler_compat -Wl,-rpath=/usr/local/envs/py3env/lib,--no-as-needed build/temp.linux-x86_64-3.5/shap/_cext.o -o build/lib.linux-x86_64-3.5/shap/_cext.cpython-35m-x86_64-linux-gnu.so

Notice the cc1plus warning up there - I have no idea what this means or what is going on. I am not sure if the installation has finished, having seen such a message, but I suspect not.

  • When I try to run shapley.TreeExplainer(XGBmodel) I see this message right at the top (right away): C extension was not built during install! Then I get the error at the bottom cannot import name '_cext' .

I am guessing this all has something to do with the cextension not being built, but I have no idea what is going on and am very frustrated. Any help or insight would be very much appreciated. Thanks!!

statgirl101 avatar Apr 02 '20 17:04 statgirl101

And the build stops after that block of code I pasted above. I see no message about completely installed shap or anything like that. It just stops after the cc1plus error

statgirl101 avatar Apr 02 '20 17:04 statgirl101

Have you tried installing the c compiler? If you are on Ubuntu, try uninstalling shap, running sudo apt update and sudo apt install build-essential and then installing shap again.

alexcoca avatar Apr 08 '20 08:04 alexcoca

I have similar issue, except that i didn't install SHAP library due to permission issues.

When I try to run shap.TreeExplainer it fails to import _cext straight away.

Is there a way to solve this issue without installing anything?

The exact message I get is "cannot import name _cext from shap. Thank you

Justinas365 avatar Jan 23 '21 23:01 Justinas365

Hey, I'm also having the same issue. ImportError: cannot import name '_cext_gpu' Using cuda10.1, nvcc is available in the PATH. I have tried to use the shap package extracting from the .egg. It contains "_cext_gpu.py" file. Still it shows the above error. Any clarity on the gcc/g++ and build tools versions to build the package? Any resolution available for this?

rejinjosek avatar Jun 22 '21 11:06 rejinjosek

Hiyah, I am having the same problem, has anyone found a fix?

e5k avatar Sep 17 '21 10:09 e5k

Sampe problem here, but only with GPUTreeExplainer

mrektor avatar Jan 24 '22 15:01 mrektor

Also have the problem in GPUTreeExplainer - I compiled from source using 'python setup.py install' and it found CUDA... seemed to install fine.

C extension was not built during install! ImportError: cannot import name '_cext' from partially initialized module 'shap' (most likely due to a circular import)

ajalberd avatar Mar 04 '22 17:03 ajalberd

I facing the same problem with GPUTreeExplainer

gaurav-gg avatar Jul 12 '22 12:07 gaurav-gg

Also have the problem in GPUTreeExplainer - I compiled from source using 'python setup.py install' and it found CUDA... seemed to install fine.

C extension was not built during install! ImportError: cannot import name '_cext' from partially initialized module 'shap' (most likely due to a circular import)

Same problem here, did anyone solve it?

mxshen avatar Sep 08 '22 06:09 mxshen

Same problem here. I tried installing with pip install shap and pip install git+https://github.com/slundberg/[email protected]. Either way, when I run the code:

explainer = shap.GPUTreeExplainer(classifier)
shap_values = explainer.shap_values(X)

I get the error:

cannot import name '_cext_gpu' from partially initialized module 'shap' (most likely due to a circular import)

adkinsty avatar Oct 07 '22 19:10 adkinsty

@adkinsty did you solved? Same Issue here

davidmacar avatar Mar 21 '23 11:03 davidmacar

@davidmacar no I did not solve, just moved on

adkinsty avatar Mar 21 '23 16:03 adkinsty

Hey everyone, did anyone get any workaround on this? I have millions of records so I want use GPU version of TreeExplainer to explain shap values. Please help!

ya-stack avatar May 18 '23 18:05 ya-stack

For those encountering this problem, Please try the latest released version of shap, v0.42.1 as of time of writing.

thatlittleboy avatar Jul 18 '23 17:07 thatlittleboy

For those encountering this problem, Please try the latest released version of shap, v0.42.1 as of time of writing.

I'm facing the same issue with SHAP 0.42.1, too. Switching back to Explainer works flawlessly.

GuidoBartoli avatar Jul 24 '23 09:07 GuidoBartoli

Hi @GuidoBartoli , can you provide detailed steps to reproduce the error? and please provide the system os + cpu architecture + python version as well.

thatlittleboy avatar Jul 24 '23 09:07 thatlittleboy

I'll close this for now as the problem is fixed on recent releases. Please feel free to open a new issue (with detailed steps to reproduce the error) if you encounter it again.

connortann avatar Feb 08 '24 13:02 connortann