import yolov5: No module named 'pkg_resources' (fix)
The yolov5 package on PyPI (7.0.14, the latest) does import pkg_resources in yolov5/utils/general.py, so import yolov5 fails with ModuleNotFoundError: No module named 'pkg_resources' on setuptools 82+. Tested fix: pip install "setuptools<81".
1. The error: import yolov5 on a fresh install
This is about the yolov5 package on PyPI (pip install yolov5), version 7.0.14 (2024-11-11), the latest. It requires setuptools>=65.5.1 with no upper limit, so a fresh Python 3.11 venv got setuptools 84.2.1 (with torch 2.14.1 CPU). Then the import itself fails:
$ python -c "import yolov5"
Traceback (most recent call last):
...
File "yolov5/utils/general.py", line 34, in <module>
import pkg_resources as pkg
ModuleNotFoundError: No module named 'pkg_resources'
yolov5/utils/general.py line 34 is import pkg_resources as pkg, and setuptools 82.0.0 removed pkg_resources. The Ultralytics GitHub repository (ultralytics/yolov5, used by torch.hub.load and git clone) no longer has this import on master; only the PyPI package does.
2. Fix: pin setuptools below 81 (tested)
$ pip install "setuptools<81" # gave 80.10.2
$ python -c "import yolov5; print(yolov5.__version__)"
...
7.0.14
The import now works, with a harmless pkg_resources is deprecated warning. To hide it, set PYTHONWARNINGS="ignore:pkg_resources is deprecated as an API".
3. Next wall on torch 2.6+: Weights only load failed
Loading a model with this script still failed on torch 2.14.1:
import numpy as np
import yolov5
model = yolov5.load("yolov5n.pt")
img = np.zeros((320, 320, 3), dtype=np.uint8)
results = model(img, size=320)
print(type(results).__name__, len(results.pred[0]), "detections")
$ python b3.py
Traceback (most recent call last):
...
_pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint.
...
WeightsUnpickler error: Unsupported global: GLOBAL models.yolo.Model was not an allowed global by default. Please use `torch.serialization.add_safe_globals([models.yolo.Model])` or the `torch.serialization.safe_globals([models.yolo.Model])` context manager to allowlist this global if you trust this class/function.
torch 2.6 changed torch.load to weights_only=True by default, and the official yolov5n.pt file stores a full model object. torch's override variable made it work:
$ TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1 python b3.py
...
Detections 0 detections
(Zero detections is expected on a black test image.) Only use this override with checkpoints you trust, such as the official Ultralytics release files: loading with weights_only=False can run code stored in the file. The other route is pip install "torch<2.6", which we did not test here. More options: torch.load Weights only load failed.
Related: No module named 'pkg_resources' (general fix) · openmim (same cause) · TensorBoard.
Frequently asked questions
Does torch.hub.load ultralytics/yolov5 have the same problem?
Not from this import: the ultralytics/yolov5 master branch no longer imports pkg_resources. Only the yolov5 package on PyPI (7.0.14) does.
Why does yolov5.load fail after the setuptools fix?
On torch 2.6 and newer, torch.load defaults to weights_only=True, which rejects the full-model .pt files. TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1 worked in our test; use it only with trusted checkpoints.
Written with AI assistance. Measured on 2026-10-24 on Linux (CPU) with yolov5 7.0.14 from PyPI, torch 2.14.1+cpu, CPython 3.11.17, setuptools 80.10.2 and 84.2.1. Output pasted unedited except shortened paths and cut tracebacks. PlainTables is our extension; YOLOv5 is free.