AttributeError: Module 'distutils' has no attribute 'version'

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
2 min

banner

# AttributeError: Module 'distutils' has no attribute 'version'

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.

attributeerror module setuptools distutils has no attribute version

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.

shell
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

pin setuptools version

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.".

However, even though the error is shown, the package is installed successfully.

Pinning setuptools to version 59.5.0 should resolve the error.

# Use the correct import statement

Older versions of PyTorch used to access the version attribute on setuptools.distutils.

main.py
# ⛔️ Incorrect import from setuptools import distutils print(distutils.version)

The correct import would be to import LooseVersion from distutils.version.

main.py
# ✅ 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.

main.py
# 👇️ Works if your `setuptools` version is <= 59.5.0 from setuptools import distutils print(distutils.version)

use correct import statement

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

shell
pip show setuptools

get setuptools version

# Additional Resources

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.