ModuleNotFoundError: No module named 'groundingdino' usually means the package was never installed, because it is not on PyPI — pip install groundingdino finds nothing useful. It installs from its GitHub repository, and it compiles a CUDA extension along the way, which is where most of the difficulty comes from.
Why pip does not work#
GroundingDINO is a research project distributed as a repository, not a published package. It also contains a C++ and CUDA extension that has to be compiled against your installed PyTorch and CUDA versions, which is why there is no universal wheel.
Some unrelated packages occupy similar names on PyPI. Installing one of those leaves you with something installed and the import still failing — a confusing state worth ruling out first:
python -m pip list | grep -i dino
Installing properly#
python -m venv .venv
source .venv/bin/activate # .venv\Scripts\activate on Windows
# PyTorch first, matched to your CUDA version
python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
git clone https://github.com/IDEA-Research/GroundingDINO.git
cd GroundingDINO
python -m pip install -e .
Order matters. The build reads your installed PyTorch to compile against it, so PyTorch must be present first.
The -e flag installs in editable mode, which points at the cloned folder rather than copying it. That is what the project’s own instructions assume, and it means the configuration files stay where the code expects to find them.
mkdir -p weights
curl -L -o weights/groundingdino_swint_ogc.pth \
https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
The CUDA_HOME error#
NameError: name '_C' is not defined
Or a build failure mentioning CUDA_HOME. The extension did not compile. Check what the build can see:
nvcc --version
echo $CUDA_HOME
python -c "import torch; print(torch.__version__, torch.version.cuda, torch.cuda.is_available())"
Two things must line up. The CUDA toolkit version reported by nvcc should match the one PyTorch was built against, and CUDA_HOME must point at the toolkit:
export CUDA_HOME=/usr/local/cuda-12.1
export PATH="$CUDA_HOME/bin:$PATH"
export LD_LIBRARY_PATH="$CUDA_HOME/lib64:$LD_LIBRARY_PATH"
cd GroundingDINO
python -m pip install -e . --no-build-isolation
--no-build-isolation lets the build see your already-installed PyTorch rather than fetching a fresh one into a temporary environment, which is a common cause of a version mismatch.
Running without a GPU#
The package still imports on a CPU-only machine, though inference will be slow:
python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
cd GroundingDINO
python -m pip install -e .
If the CUDA extension fails to build, the project falls back to a pure PyTorch implementation for some operations. Watch the install output — a warning there explains a lot of later slowness.
Trap one: the import name#
import groundingdino # the package
from groundingdino.util.inference import load_model # the usual entry point
All lowercase, no underscore, no hyphen. The repository folder is GroundingDINO with capitals, which misleads people into writing import GroundingDINO — and module names are case-sensitive.
Trap two: the working directory#
FileNotFoundError: .../GroundingDINO_SwinT_OGC.py
The config path in most examples is relative, so it only resolves when you run from the repository root. Build absolute paths instead:
from pathlib import Path
import groundingdino
ROOT = Path(groundingdino.__file__).resolve().parent
CONFIG = ROOT / "config" / "GroundingDINO_SwinT_OGC.py"
print(CONFIG.exists())
Deriving the path from the installed package’s own location means the script works from anywhere.
A minimal check#
import torch
import groundingdino
from groundingdino.util.inference import load_model, load_image, predict
print("torch:", torch.__version__)
print("cuda available:", torch.cuda.is_available())
print("package at:", groundingdino.__file__)
Run that first. If it prints three lines, the install is correct and every later problem is in your own code or your paths. Separating those two questions saves a great deal of time.
from pathlib import Path
import groundingdino
from groundingdino.util.inference import load_model, load_image, predict
ROOT = Path(groundingdino.__file__).resolve().parent
model = load_model(
str(ROOT / "config" / "GroundingDINO_SwinT_OGC.py"),
"weights/groundingdino_swint_ogc.pth",
)
image_source, image = load_image("example.jpg")
boxes, logits, phrases = predict(
model=model,
image=image,
caption="a dog . a bicycle .",
box_threshold=0.35,
text_threshold=0.25,
)
print(phrases, logits.tolist())
Note the caption format: lowercase phrases separated by full stops. The model was trained that way, and other formats give noticeably worse results.
Docker, if the build keeps failing#
FROM pytorch/pytorch:2.1.0-cuda12.1-cudnn8-devel
ENV CUDA_HOME=/usr/local/cuda
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
WORKDIR /app
RUN git clone https://github.com/IDEA-Research/GroundingDINO.git \
&& cd GroundingDINO \
&& pip install -e . --no-build-isolation
The devel image includes the CUDA toolkit; the runtime variants do not, so the extension cannot compile in them. If you have spent more than an hour on toolkit versions, this is the faster route.
Questions people ask#
Can I install it without CUDA?
Yes. It runs on CPU, considerably more slowly. For anything beyond trying it out, a GPU makes a large difference.
What does the _C undefined error mean?
The compiled extension is missing or was built against a different PyTorch. Reinstall with --no-build-isolation after confirming CUDA_HOME and the toolkit version.
Why does the import work in the repository folder but not elsewhere?
Python finds the local folder rather than an installed package. That means the install did not succeed — check pip show groundingdino and reinstall with -e.
Is there a simpler way to use it?
Several higher-level libraries wrap it with the setup handled for you, and some hosted APIs expose similar models. If you want the capability rather than the codebase, those are worth looking at first.
Where to go next#
- Python virtual environments — isolating a build like this.
- Fixing ModuleNotFoundError — the ordinary version of this error.
- Machine learning with Python — the wider context.