Last updated: Apr 10, 2024
Reading time·2 min

The "AttributeError: module 'distutils' has no attribute 'version'" occurs
because of a change in setuptools that caused a broken import in PyTorch.
To solve the error, upgrade PyTorch to the latest version or pin your
setuptools version to 59.5.0.

The first thing you should try is to upgrade PyTorch to the latest version.
Alternatively, you can pin your
setuptools version to 59.5.0.
Open your terminal and run the following command.
pip install setuptools==59.5.0 pip3 install setuptools==59.5.0 python -m pip install setuptools==59.5.0 python3 -m pip install setuptools==59.5.0 py -m pip install setuptools==59.5.0

When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".
Pinning setuptools to version 59.5.0 should resolve the error.
Older versions of PyTorch used to access the version attribute on
setuptools.distutils.
# ⛔️ Incorrect import from setuptools import distutils print(distutils.version)
The correct import would be to import LooseVersion from distutils.version.
# ✅ Correct import from distutils.version import LooseVersion print(LooseVersion)
However, if you pin your setuptools version to 59.5.0, the old import still
works.
# 👇️ Works if your `setuptools` version is <= 59.5.0 from setuptools import distutils print(distutils.version)

You can use the pip show setuptools command to check your version of the
setuptools module.
pip show setuptools

You can learn more about the related topics by checking out the following tutorials: