Detectron2: cannot import name '_C'

Created on 24 Oct 2019  路  9Comments  路  Source: facebookresearch/detectron2

When I run a demo or a test project, it keeps showing up.
"from detectron2 import _C" Importerror: cannot import name '_C' "
How can I resolve the error?

installation / environment

Most helpful comment

If you install by pip, and run demo or example in git repo detectron in root directory, import detectron may import lib from your git root directory (not pip installation). This won't work (you want to use pip installation).

You may remove detectron directory or change the name, so python will look in pip packages.

All 9 comments

You did not build detectron2. Please install it following INSTALL.md

I have the same problem. However, I have followed the installation steps using "pip3 install build develop".

The errors: Failed to load OpenCL runtime
Traceback (most recent call last):
File "Copy_of_Detectron2_Tutorial custom.py", line 49, in
from detectron2.engine import DefaultPredictor
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/engine/__init__.py", line 11, in
from .hooks import *
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/engine/hooks.py", line 17, in
from detectron2.evaluation.testing import flatten_results_dict
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/evaluation/__init__.py", line 2, in
from .cityscapes_evaluation import CityscapesEvaluator
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/evaluation/cityscapes_evaluation.py", line 10, in
from detectron2.data import MetadataCatalog
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/data/__init__.py", line 4, in
from .build import (
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/data/build.py", line 13, in
from detectron2.structures import BoxMode
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/structures/__init__.py", line 2, in
from .boxes import Boxes, BoxMode, pairwise_iou
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/structures/boxes.py", line 7, in
from detectron2.layers import cat
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/layers/__init__.py", line 3, in
from .deform_conv import DeformConv, ModulatedDeformConv
File "/home/dlsj/Documents/Detectron2/detectron2_repo/detectron2/layers/deform_conv.py", line 10, in
from detectron2 import _C
ImportError: cannot import name '_C'

Environment:
Ubuntu 18.04.03 LTS
Python 3.6.8
GCC 7.4.0
Pytorch 1.3.0
OpenCV 3.2.0

However, I have followed the installation steps using "pip3 install build develop".

The installation instructions are not like this.

Same issue, after build detectron2 with:

python setup.py build develop

and try to run with:

python demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input input1.jpg input2.jpg --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

Got an error:

Traceback (most recent call last):
  File "demo/demo.py", line 11, in <module>
    from detectron2.data.detection_utils import read_image
  File "/home/eko_rudiawan/detectron2/detectron2/data/__init__.py", line 4, in <module>
    from .build import (
  File "/home/eko_rudiawan/detectron2/detectron2/data/build.py", line 13, in <module>
    from detectron2.structures import BoxMode
  File "/home/eko_rudiawan/detectron2/detectron2/structures/__init__.py", line 2, in <module>
    from .boxes import Boxes, BoxMode, pairwise_iou
  File "/home/eko_rudiawan/detectron2/detectron2/structures/boxes.py", line 8, in <module>
    from detectron2.layers import cat
  File "/home/eko_rudiawan/detectron2/detectron2/layers/__init__.py", line 3, in <module>
    from .deform_conv import DeformConv, ModulatedDeformConv
  File "/home/eko_rudiawan/detectron2/detectron2/layers/deform_conv.py", line 10, in <module>
    from detectron2 import _C
ImportError: /home/eko_rudiawan/detectron2/detectron2/_C.cpython-37m-x86_64-linux-gnu.so: undefined symbol: _ZNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEC1Ev

I am using anaconda env
Python: 3.7.3
Torch: 1.3.1
Torchvision: 0.4.2

That's not the same issue because your error does not contain "cannot import name '_C'".

Please see if the information https://github.com/facebookresearch/detectron2/blob/master/INSTALL.md#common-installation-issues resolves your issue. If not, please provide an environment so that your issue can be reproduced by other people.
You can also try conda update libgcc.

@ekorudiawan
I encountered the same error before, i.e.

ImportError: /home/eko_rudiawan/detectron2/detectron2/_C.cpython-37m-x86_64-linux-gnu.so: undefined symbol: _ZNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEC1Ev

The following procedures solve my problems:

  1. uninstall conda env and your detectron2 gcc build by the following command:
$ conda remove --name your_conda_name --all
detectron2$ rm -rf build
detectron2$ rm -rf **/**.so
  1. update base conda:
$ conda update -n base -c defaults conda
  1. update conda's libgcc package:
$ conda update libgcc
  1. reinstall conda environment and build detectron2 again, as instructed in INSTALL.md

P.S. the above steps are actually from INSTALL.md. Hope this could help

I've met the same issue, but I can run import detectron2, so what's the problem?

If you install by pip, and run demo or example in git repo detectron in root directory, import detectron may import lib from your git root directory (not pip installation). This won't work (you want to use pip installation).

You may remove detectron directory or change the name, so python will look in pip packages.

I've met the same issue, but I can run import detectron2, so what's the problem?

yes, I have met the same problem. You should change the floder name "detectron"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AntonBaumannDE picture AntonBaumannDE  路  3Comments

kl720 picture kl720  路  3Comments

limsijie93 picture limsijie93  路  3Comments

marcoippolito picture marcoippolito  路  4Comments

choasup picture choasup  路  3Comments