ultralytics/yolov5 · error · FileNotFoundError

Source path '{source}' does not exist

Error message

Source path '{source}' does not exist

What it means

detect.py run() raises FileNotFoundError when the inference source is neither a webcam index/stream URL/screenshot keyword nor an existing path or a resolvable glob pattern. The check uses has_magic to allow wildcards, so any non-magic literal path that does not exist on disk is rejected before the model is loaded. This is the first validation in run(), so it fails fast with no GPU work done.

Source

Thrown at detect.py:159

        from detect import run

        # Run inference on an image
        run(source='data/images/example.jpg', weights='yolov5s.pt', device='0')

        # Run inference on a video with specific confidence threshold
        run(source='data/videos/example.mp4', weights='yolov5s.pt', conf_thres=0.4, device='0')
        ```
    """
    source = str(source)
    is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
    is_url = source.lower().startswith(("rtsp://", "rtmp://", "http://", "https://"))
    webcam = source.isnumeric() or source.endswith(".streams") or (is_url and not is_file)
    screenshot = source.lower().startswith("screen")

    if not (webcam or screenshot or is_url) and not (
        Path(source).exists() or (has_magic(source) and glob(source, recursive=True))
    ):
        raise FileNotFoundError(f"Source path '{source}' does not exist")

    save_img = not nosave and not source.endswith(".txt")  # save inference images

    if is_url and is_file:
        source = check_file(source)  # download

    # Directories
    save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)  # increment run
    (save_dir / "labels" if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir

    # Load model
    device = select_device(device)
    model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
    stride, names, pt = model.stride, model.names, model.pt
    imgsz = check_img_size(imgsz, s=stride)  # check image size

    # Dataloader
    bs = 1  # batch_size

View on GitHub (pinned to 20d1d78a08)

Solutions

  1. Verify the path exists from the same cwd: python -c "from pathlib import Path; print(Path('your/source').exists())".
  2. Use an absolute path for the source argument to remove cwd ambiguity.
  3. Use a glob pattern (e.g. source='images/*.jpg') which is expanded by the has_magic branch instead of the exists() check.
  4. Use a supported stream: webcam index ('0'), rtsp://http:// URL, or 'screen' for screenshots.

Example fix

# before
run(source='data/videos/example.mp4', weights='yolov5s.pt')  # file missing

# after
run(source=str(ROOT / 'data/images'), weights='yolov5s.pt')  # bundled demo images
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
from detect import has_magic

def source_ok(source: str) -> bool:
    s = str(source)
    is_file = Path(s).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
    is_url = s.lower().startswith(("rtsp://", "rtmp://", "http://", "https://"))
    webcam = s.isnumeric() or s.endswith(".streams") or (is_url and not is_file)
    screenshot = s.lower().startswith("screen")
    return bool(webcam or screenshot or is_url or Path(s).exists() or (has_magic(s) and glob.glob(s, recursive=True)))

Type guard

def is_usable_source(source: str) -> bool:
    """True if detect.run() will accept this source without FileNotFoundError."""
    from pathlib import Path
    from utils.general import IMG_FORMATS, VID_FORMATS
    s = str(source)
    if s.isnumeric() or s.endswith('.streams') or s.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://', 'screen')):
        return True
    return Path(s).exists() or '*' in s

Prevention

When it happens

Trigger: run(source='data/videos/example.mp4') when that demo file is absent; passing a relative path while the process cwd differs from the repo root; a typo'd image path with no wildcard characters; an NAS/mount path that is not mounted at run time.

Common situations: Running detect.py from outside the repo clone where the default example.mp4 does not exist; Docker containers where the data/ directory was not copied in; CI jobs with missing test fixtures; symlinked paths that are broken.

Related errors


AI-assisted analysis of ultralytics/yolov5@20d1d78a08 (2026-08-15). Data as JSON: /api/errors/e59e2f428b7e493f. Report an issue: GitHub.