attribute error: module ‘distutils’ has no attribute ‘version’

In this tutorial, we will discuss the following solutions to resolve the error attribute error: module distutils has no attribute version.

What is distutils in Python?

The distutils is a standard module in Python which gives support for building and distributing Python modules.

This is used to compile and install Python packages.

It should be used to distribute your own Python code as packages that other people can install and use.

Also read: attributeerror: ‘str’ object has no attribute ‘read’ [SOLVED]

Usage of disutils

  • It is used to create distribution packages.
  • It is used for building and packaging source code.
  • It is used for generating binary distributions in different platforms.
  • It is used to install packages on a local system.

Why AttributeError: module ‘distutils’ has no attribute ‘version’ occurs?

This error occurs when you are trying to access the attribute which does not exist in the ‘distutils‘ module.

Precisely, you are trying to access the ‘version’ attribute, which does not available in the ‘distutils’ packages.

On the other hand, the error might occur because you are using the out-of-date version of Python which is not support the version attribute in distutils.

For example, we will run the following code:

from setuptools import distutils

print(distutils.version)

Output:

import-attribute-error-module-distutils-has-no-attribute-version

The image above display an error because of the changes in setuptools version 59.6.0. This is to break the call to a version attribute.

Furthermore, the version attribute seems to be removed from the distutils module in the latest version.

You can check this through executing the dir() function:

from setuptools import distutils

print(distutils.__version__, dir(distutils))

Output:

3.9.10 [‘builtins‘, ‘cached‘, ‘doc‘, ‘file‘, ‘loader‘, ‘name‘, ‘package‘, ‘path‘, ‘spec‘, ‘version‘, ‘_msvccompiler’, ‘archive_util’, ‘ccompiler’, ‘cmd’, ‘command’, ‘config’, ‘core’, ‘debug’, ‘dep_util’, ‘dir_util’, ‘dist’, ‘errors’, ‘extension’, ‘fancy_getopt’, ‘file_util’, ‘filelist’, ‘importlib’, ‘log’, ‘spawn’, ‘sys’, ‘util’]

As you can see in the above output, there’s only the __version__ attribute display in the output.

This error occurs if you are importing or installing the PyTorch module, that was fixed in PyTorch version 1.11.0.

How to solved the attribute error: module distutils has no attribute version?

Time needed: 3 minutes

Here are the solutions to solve the solved the attribute error: module distutils has no attribute version.

  • Step 1: Install using PIP

    When you are using a PyTorch module, you can solve this error through upgrading the torch module. You can use the following commands to upgrade the torch module:

    For PIP:
    pip install --upgrade torch

    For PIP3:
    pip3 install --upgrade torch

    For CONDA:
    conda update pytorch

  • Step 2: testing PyTorch version 1.11.0

    Here is the following command to test the PyTorch version 1.11.0.

    If you are using the PIP:
    pip install torch==1.11.0

    If you are using the PIP3:
    pip3 install torch==1.11.0

    If you are using the CONDA:
    conda install pytorch=1.11.0

    If you already upgraded the PyTorch version, the error you encounter will disappear.

    When you are using a different Python package which is the error still persists, then you can install the setuptools version to 59.5.0 so that distutils.version still works.

    Proceed to the next solutions

  • Step 3: Install setuptools

    The following commands to install the setuptools module version:

    For PIP:
    pip install setuptools==59.5.0

    For PIP3:
    pip3 install setuptools==59.5.0

    For CONDA:
    conda install setuptools=59.5.0

    When the conda will show an error message PackagesNotFoundError, then you should install pip in the conda environment and you can use this command to install setuptools:

    Install pip in conda environment:
    conda install pip

    Then you should install setuptools:
    pip install setuptools==59.5.0

    The distutils.version attribute will now be accessible.

    However, you can also adjust the import statement to import distutils.version.LooseVersion when you had an access to the source code:

    From this import:
    from setuptools import distutils

    To this import:
    from distutils.version import LooseVersion

    When you are importing distutils from setuptools will cause an error, importing the LooseVersion class works just fine.

Frequently Asked Questions

What is Python AttributeError and what causes it?

AttributeError is raised when you access an attribute or method that doesn’t exist on the object. Most common cause: calling a method on None (NoneType has no attribute X). Other causes: typo in method name, wrong object type (str when you expected list), or using a feature removed in a newer library version. The error names exactly which type and which missing attribute.

How do I fix ‘NoneType object has no attribute’?

The variable you’re accessing is None, but you expected an object. Trace back to where it was assigned: a function returning None instead of an object (forgot to return), a database query returning no rows (Model.objects.first() returns None when empty), or an API call that failed silently. Safe pattern: if obj is not None: obj.method() OR use the walrus operator: if (obj := get_obj()): obj.method().

How do I check if an attribute exists before accessing it?

Use hasattr(obj, ‘attr_name’) for runtime check, or getattr(obj, ‘attr_name’, default) to get-with-default. For frequent attribute checks, consider type hints + mypy/pyright which catch most AttributeErrors at static-analysis time before runtime.

How do I prevent AttributeError from None values?

Three patterns: (1) Always validate function returns (if result is None: raise). (2) Use type hints with Optional[X] to make None-ability explicit. (3) Use the walrus operator + early return: if (val := get_val()) is None: return default; use val. Defensive coding around None-able returns prevents 90% of AttributeError in production.

Where can I find more AttributeError fixes?

Browse the AttributeError reference hub for 170+ specific fixes (NoneType, pandas, NumPy, sklearn, Selenium). For related errors see TypeError. For Python debugging fundamentals see Python Tutorial hub.

Conclusion

That’s it, I hope the above steps could resolve the attribute error: module ‘distutils’ has no attribute ‘version’ in your python project.

Adones Evangelista

Programmer & Technical Writer at PIES IT Solution

Adones Evangelista is a programmer and writer at PIES IT Solution, author of over 900 tutorials and error-fix guides at itsourcecode.com. Specializes in JavaScript, Django, Laravel, and Python error debugging covering ValueError, TypeError, AttributeError, ModuleNotFoundError, and RuntimeError, plus C/C++ and PHP capstone projects for BSIT students.

Expertise: JavaScript · Python · Django · Laravel · Error Debugging · C/C++  · View all posts by Adones Evangelista →

Leave a Comment