torch: ModuleNotFoundError: No module named 'pkg_resources' (fix)
Old PyTorch releases import pkg_resources, which setuptools 82 removed. On torch 1.10 import torch fails; on 1.11 to 2.1 torch.utils.cpp_extension does. We measured 1.10.2, 2.1.2 and 2.9.0 and tested both fixes.
Which line fails depends on the torch version. We read torch/torch_version.py and torch/utils/cpp_extension.py at each release tag:
- torch 1.10:
torch_version.pyimportspkg_resourcesat the top, so plainimport torchfails. - torch 1.11 to 2.1:
import torchworks (the version import is lazy, and from 1.12 it tries thepackagingpackage first), buttorch.utils.cpp_extensionstill importspkg_resourcesat the top. That module is whatsetup.pyfiles of CUDA/C++ extensions, and many libraries that build or load them, import. - torch 2.2 and newer: no
pkg_resourcesimport in either file.
1. torch 1.10: import torch fails
import torch
print("torch", torch.__version__)
torch 1.10.2, no setuptools in the venv (the default for uv venv):
Traceback (most recent call last):
File "b1.py", line 1, in <module>
import torch
File ".../site-packages/torch/__init__.py", line 29, in <module>
from .torch_version import __version__ as __version__
File ".../site-packages/torch/torch_version.py", line 3, in <module>
from pkg_resources import packaging # type: ignore[attr-defined]
ModuleNotFoundError: No module named 'pkg_resources'
2. torch 2.1: cpp_extension fails
import setuptools, torch
print("torch", torch.__version__, "setuptools", setuptools.__version__)
from torch.utils import cpp_extension
print("cpp_extension OK")
torch 2.1.2 with setuptools 84.2.1 (setuptools 82.0.0 removed pkg_resources):
torch 2.1.2+cpu setuptools 84.2.1
Traceback (most recent call last):
File "b2.py", line 3, in <module>
from torch.utils import cpp_extension
File ".../site-packages/torch/utils/cpp_extension.py", line 28, in <module>
from pkg_resources import packaging # type: ignore[attr-defined]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'pkg_resources'
import torch alone is fine on 2.1.2:
torch 2.1.2+cpu
3. Fix A: upgrade torch to 2.2 or newer
Same script on torch 2.9.0 with setuptools 84.2.1:
torch 2.9.0+cpu setuptools 84.2.1
cpp_extension OK
Upgrade torchvision/torchaudio in the same command, since each needs a matching torch (mismatches give operator torchvision::nms does not exist or libtorchaudio.so undefined symbol).
4. Fix B: keep the old torch, pin setuptools below 81
pip install "setuptools<81", then torch 2.1.2:
torch 2.1.2+cpu setuptools 80.10.2
.../site-packages/torch/utils/cpp_extension.py:28: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
from pkg_resources import packaging # type: ignore[attr-defined]
cpp_extension OK
torch 1.10.2:
.../site-packages/torch/torch_version.py:3: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
from pkg_resources import packaging # type: ignore[attr-defined]
torch 1.10.2+cpu
The warning is harmless. If pip install builds an extension in an isolated build environment, it gets the newest setuptools regardless of what is installed; add --no-build-isolation so the build uses your pinned one.
On Linux with glibc 2.41+ (Debian 13 and others), torch 1.10 then stops at cannot enable executable stack as shared object requires; that is the next fix if you are reviving an old environment.
Related: No module named pkg_resources (all causes) ยท torchvision.transforms has no attribute Scale
Frequently asked questions
Which torch versions are affected?
We checked the source at each tag: 1.10 fails on import torch; 1.11 to 2.1 fail when torch.utils.cpp_extension is imported; 2.2 and later have no pkg_resources import there.
Does pip install setuptools fix it?
Only a version below 82 brings pkg_resources back. A plain pip install setuptools installs the latest, which does not have it.
Written with AI assistance. Measured on 2026-10-24 on Linux: torch 1.10.2+cpu (CPython 3.9), torch 2.1.2+cpu and 2.9.0+cpu (CPython 3.11.17), setuptools 84.2.1 and 80.10.2. Output pasted unedited except shortened paths. PlainTables is our extension; torch and setuptools are free.