How to Resolve Python Error "AttributeError: module 'distutils' has no attribute 'version'"
Encountering the AttributeError: module 'distutils' has no attribute 'version'
in Python often occurs after updating development tools or in environments using newer versions of setuptools
(v60+). This error typically arises because a library you are using (commonly PyTorch or others that relied on older package structures) attempts to access version information through distutils
, which has been changed or deprecated within setuptools
.
This guide explains the cause related to setuptools
updates and provides effective solutions to resolve this attribute error.
Understanding the Error: Changes in setuptools
and distutils
Historically, setuptools
bundled or relied heavily on Python's built-in distutils
module for package building and distribution tasks. Some libraries accessed versioning information through an internal structure like setuptools.distutils.version
.
However, major changes occurred:
distutils
itself became deprecated, with its functionality migrating tosetuptools
and other libraries likepackaging
.- Newer versions of
setuptools
(starting around v60) began to vendor (include a private copy) or remove direct access to the bundleddistutils
, changing the internal structure.
As a result, code (especially in older library versions) that tries to access distutils.version
via the setuptools
namespace might find that the version
attribute no longer exists at that location, leading to the AttributeError
.
The Common Cause: Incompatibility with Newer setuptools
The most frequent reason you see this error is an incompatibility between:
- An older version of a library (like PyTorch, but potentially others) that still uses the old
setuptools.distutils.version
access pattern. - A newer version of
setuptools
(v60 or later) installed in your Python environment, where that internal path is no longer valid.
This often happens after updating pip
or setuptools
itself, or when setting up a new environment that installs the latest setuptools
by default.
Solution 1: Upgrade the Dependent Package (e.g., PyTorch) (Recommended)
The best long-term solution is usually to update the library that is triggering the error internally. Maintainers of popular libraries like PyTorch typically release updates to be compatible with newer versions of setuptools
and Python.
Example: Upgrade PyTorch (check PyTorch website for current install commands)
# Standard pip install (may vary based on your CUDA version etc.)
pip install --upgrade torch torchvision torchaudio
# Or using pip3 / python -m pip
pip3 install --upgrade torch torchvision torchaudio
python -m pip install --upgrade torch torchvision torchaudio
# If using conda
conda update pytorch torchvision torchaudio -c pytorch (or similar)
Replace torch torchvision torchaudio
with the actual package(s) that depend on the older distutils.version
structure if it's not PyTorch. Upgrading often resolves the issue because the newer library version uses corrected import paths or different versioning mechanisms.
Solution 2: Downgrade setuptools
(Workaround)
If you cannot upgrade the primary library (e.g., due to other dependency constraints or if an update isn't available yet), a common workaround is to downgrade setuptools
to a version prior to the breaking change (typically version 59.x or earlier). Version 59.6.0
or 59.5.0
are often cited as safe points.
# Downgrade setuptools to a specific compatible version
pip install "setuptools<=59.6.0"
# Or pin exactly:
pip install "setuptools==59.5.0"
# Or using pip3 / python -m pip
pip3 install "setuptools<=59.6.0"
python -m pip install "setuptools<=59.6.0"
Note on Dependency Conflicts: When you run this, pip
might show errors related to dependency resolution ("ERROR: pip's dependency resolver does not currently take into account..."). Often, for this specific setuptools
downgrade, these warnings can be ignored, and the downgrade still resolves the distutils
AttributeError
. However, be aware that pinning setuptools
might cause issues with other packages that require a newer version.
Solution 3: Check/Correct Import Statements (Less Common for this Error)
While the error usually stems from library code, if your own code was directly trying to import distutils.version
via setuptools
, you should update the import.
Old and Incorrect way that might break with new setuptools
from setuptools import distutils
try:
# ⛔️ AttributeError with setuptools >= 60
version_info = distutils.version.LooseVersion("1.0")
except AttributeError as e:
print(f"Error accessing via setuptools.distutils: {e}")
Correct ways (depending on need)
Option A: Import directly from distutils (if still available/needed)
try:
from distutils.version import LooseVersion # or StrictVersion
print(f"LooseVersion from distutils: {LooseVersion}")
except ImportError:
print("distutils.version not directly available.")
distutils
itself is deprecated, prefer 'packaging'
Option B: Use the modern 'packaging' library (Recommended replacement)
# Needs installation: pip install packaging
try:
from packaging.version import Version
print(f"\nVersion from packaging: {Version}")
v = Version("1.0")
print(f"Parsed version: {v}")
except ImportError:
print("\n'packaging' library not installed. Run: pip install packaging")
Again, this is usually something library maintainers need to fix, not typically the cause of the error for end-users importing libraries like PyTorch.
Debugging: Checking Package Versions
To understand your environment, check the installed versions:
# Check setuptools version
pip show setuptools
# Or:
python -m pip show setuptools
# Check PyTorch version (if relevant)
pip show torch
# Or: python -m pip show torch
If setuptools
is >= 60.0.0 and your dependent library (like PyTorch) is an older version, that's strong evidence for the incompatibility described.
Conclusion
The AttributeError: module 'distutils' has no attribute 'version'
, often encountered with libraries like PyTorch, is typically caused by an incompatibility between an older version of the library and a newer version of setuptools
(>= v60), where internal access to distutils.version
has changed.
The recommended solutions are:
- Upgrade the affected library (e.g.,
pip install --upgrade torch
) to a version compatible with modernsetuptools
. - As a workaround, downgrade
setuptools
to a version before the breaking change (e.g.,pip install "setuptools<=59.6.0"
), being mindful of potential side effects on other dependencies.
Updating the primary library is generally the preferred long-term fix.