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.

Conclusion

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

Leave a Comment