ImportError: cannot import name '_C' from 'detectron2'

See original GitHub issue

I had no problem using Densepose. However when I was trying to use PointRend I had this issue. I tried reinstalling but the same thing happened

If you do not know the root cause of the problem / bug, and wish someone to help you, please post according to this template:

Instructions To Reproduce the Issue:

  1. what code you wrote
# You may need to restart your runtime prior to this, to let your installation take effect
# Some basic setup:
# Setup detectron2 logger
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()

# import some common libraries
import numpy as np
import cv2
import torch


# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer, ColorMode
from detectron2.data import MetadataCatalog
coco_metadata = MetadataCatalog.get("coco_2017_val")

# import PointRend project
import sys; sys.path.insert(1, "detectron2_repo/projects/PointRend")
import point_rend

im = cv2.imread("frank_front.jpg")
cv2.imshow(im)
  1. what exact command you run: I ran the code
  2. what you observed (including the full logs):
Traceback (most recent call last):
  File "/home/elysium/detectron2-master/projects/practice.py", line 15, in <module>
    from detectron2 import model_zoo
  File "/home/elysium/detectron2-master/detectron2/model_zoo/__init__.py", line 7, in <module>
    from .model_zoo import get, get_config_file, get_checkpoint_url
  File "/home/elysium/detectron2-master/detectron2/model_zoo/model_zoo.py", line 8, in <module>
    from detectron2.modeling import build_model
  File "/home/elysium/detectron2-master/detectron2/modeling/__init__.py", line 4, in <module>
    from detectron2.layers import ShapeSpec
  File "/home/elysium/detectron2-master/detectron2/layers/__init__.py", line 3, in <module>
    from .deform_conv import DeformConv, ModulatedDeformConv
  File "/home/elysium/detectron2-master/detectron2/layers/deform_conv.py", line 10, in <module>
    from detectron2 import _C
ImportError: cannot import name '_C' from 'detectron2' (/home/elysium/detectron2-master/detectron2/__init__.py)

  1. please also simplify the steps as much as possible so they do not require additional resources to run, such as a private dataset.

Expected behavior:

I expected no error

Environment:

Run python -m detectron2.utils.collect_env in the environment where you observerd the issue, and paste the output.


sys.platform linux Python 3.7.6 (default, Jan 8 2020, 19:59:22) [GCC 7.3.0] numpy 1.18.1 detectron2._C failed to import DETECTRON2_ENV_MODULE <not set> PyTorch 1.4.0+cu100 @/home/elysium/anaconda3/envs/detectron2-master/lib/python3.7/site-packages/torch PyTorch debug build False CUDA available True GPU 0 GeForce GTX 1070 Ti CUDA_HOME /usr NVCC Cuda compilation tools, release 7.5, V7.5.17 Pillow 7.0.0 torchvision 0.5.0+cu100 @/home/elysium/anaconda3/envs/detectron2-master/lib/python3.7/site-packages/torchvision torchvision arch flags sm_35, sm_50, sm_60, sm_70, sm_75 cv2 4.2.0


PyTorch built with:

  • GCC 7.3
  • Intel® Math Kernel Library Version 2020.0.0 Product Build 20191122 for Intel® 64 architecture applications
  • Intel® MKL-DNN v0.21.1 (Git Hash 7d2fd500bc78936d1d648ca713b901012f470dbc)
  • OpenMP 201511 (a.k.a. OpenMP 4.5)
  • NNPACK is enabled
  • CUDA Runtime 10.0
  • NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_37,code=compute_37
  • CuDNN 7.6.3
  • Magma 2.5.1
  • Build settings: BLAS=MKL, BUILD_NAMEDTENSOR=OFF, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Wno-stringop-overflow, DISABLE_NUMA=1, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF,

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15

github_iconTop GitHub Comments

9reactions
maisonhai3commented, Apr 14, 2021

I got the exact same problem.

Just an action of rename will help our soul.

Suppose we are in experiment folder, after git clone detectron2, we have:

experiment
|— detectron2
|—|— configs
|—|— datasets
|—|— detectron2

The why: Suppose we want to run /experiment/detectron2/demo/demo.py, The error of cannot import name '_C' will appear. As demo.py try to import detectron2 from the folder of detectron2/detectron2, Instead of importing from this folder instead of our Detectron2 installation somewhere else.

Take a look at detectron2/detectron2, there is a __init.py__, which causes this mistake.

Solution: Rename detectron2/detectron2 to example detectron2/detectron2_you_make_me_trouble

8reactions
ppwwyyxxcommented, Mar 10, 2020

Then you probably did not install detectron2. See https://github.com/facebookresearch/detectron2/blob/master/INSTALL.md

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot import name '_C' #157 - facebookresearch/detectron2
When I run a demo or a test project, it keeps showing up. "from detectron2 import _C" Importerror: cannot import name '_C' "...
Read more >
python - ImportError about Detectron2 - Stack Overflow
It seems there might be some problems in my environment. Does anyone know how to fix it? ImportError: cannot import name 'is_fx_tracing' from...
Read more >
Source code for detectron2.config.lazy
Examples: :: from detectron2.config import instantiate, LazyCall layer_cfg ... + relative_import_err) else: raise ImportError( f"Cannot import name ...
Read more >
解决ImportError: cannot import name '_C' from 'detectron2'
解决ImportError: cannot import name '_C' from 'detectron2'根据link.detectron2安装无误的前提下,将detectron2目录改个名字或者删掉,这样python ...
Read more >
cannot import name 'metanet_pb2' from partially initialized ...
检查caffe2的gpu版本是否安装成功的时候,终端下运行python后输出from caffe2.python import workspace出现from caffe2.proto import caffe2_pb2 ImportError: cannot ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found