PyCharm virtualenv ModuleNotFoundError: No module named 'distutils.core'

1. Overview

In this tutorial, we'll see how to solve a common pip, Python and PyCharm error:

ModuleNotFoundError: No module named 'distutils.util'

We get this error from pip and Python when we try to install a new package with pip, and that package distutils is missing. Also PyCharm raises the error during creation of a new Project or virtual environment.

The reasons might be different and so the solutions. We will cover most popular solutions related to that error

The full error will looks like:

Traceback (most recent call last):
  File "/tmp/tmpmhuk7uzepycharm-management/setuptools-40.8.0/setup.py", line 11, in <module>
    import setuptools
  File "/tmp/tmpmhuk7uzepycharm-management/setuptools-40.8.0/setuptools/__init__.py", line 6, in <module>
    import distutils.core
ModuleNotFoundError: No module named 'distutils.core'

2. Latest Python and ModuleNotFoundError: No module named 'distutils.util'

Module distutils is not included in the latest versions of Linux Mint 20(Ubuntu 20) which cause errors in pip and PyCharm.

To solve this error we need to install distutils package - for the latest version of Python 3 which is included in the OS:

sudo apt-get update
sudo apt-get install python3-distutils

After this installation the error should disappear. If the error is still present - then we have two options:

  • check the python version of pip and the virtual environment - go to next sections
  • try to reinstall the package if it's broken:
sudo apt-get install --reinstall python3-distutils

Note: If the modules are installed already we will see:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-distutils is already the newest version (3.6.9-1~18.04).
0 upgraded, 0 newly installed, 0 to remove and 65 not upgrade

3. Verify Python version and modules

Next thing to do is to verify the Python installation, version and setuptools modules.

(1) Starting with Python version:

python -V
python3 -V

which can result in:

Python 2.7.17
Python 3.6.9

(2) Now we can verify that you have: pip and setuptools for the targeted version, For example for Python 3:

by listing all python modules:

pip3 list

result

Package    Version
---------- -------
pip        20.0.2
setuptools 40.8.0

or per package:

$ pip3 show pip
Name: pip
Version: 9.0.1
Summary: The PyPA recommended tool for installing Python packages.
Home-page: https://pip.pypa.io/
Author: The pip developers
Author-email: python-virtualenv@groups.google.com
License: MIT
Location: /usr/lib/python3/dist-packages
Requires:
$ pip3 show setuptools
Name: setuptools
Version: 45.2.0
Summary: Easily download, build, install, upgrade, and uninstall Python packages
Home-page: https://github.com/pypa/setuptools
Author: Python Packaging Authority
Author-email: distutils-sig@python.org
License: UNKNOWN
Location: /usr/local/lib/python3.6/dist-packages
Requires:

If the packages are missing for older Python versions you need to install them by following the next step.

4. Install python3-distutils and setuptools for older Python versions

The additional modules required for creation of new virtual environments can be installed by:

sudo apt-get install python3-distutils

expected output:

sudo apt-get install python3-distutils  
...
After this operation, 3143 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
...
Unpacking python3-distutils (3.6.9-1~18.04) ...
Setting up python3-lib2to3 (3.6.9-1~18.04) ...
Setting up python3-distutils (3.6.9-1~18.0

4.2. Install Older Python on Ubuntu

But for the older Python version on latest Linux Mint and Ubuntu we need to add a repository and install older versions of distutils. For Python 3.9 and Ubuntu 20 we can follow the next steps. Add the repo with the older modules:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update

Then install distutils for Python 3.9

sudo apt install python3.9-distutils

For Python 3.7 we can use:

sudo apt install python3.7-distutils

4.2. Install Python 3.6 on Ubuntu 20.04+

By default Ubuntu 20.04 uses Python 3.8 as the main version. So to install Python 3.6 plus distutils we need to run next commands:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt install python3.6 python3.6-dev python3.6-distutils

Once the installation is complete you need to restart PyCharm or reactivate the virtual environment.

5. Verify the setuptools modules in PyCharm

Once the installation is done and PyCharm is restarted you can verify the installation by checking:

  • File
  • Settings
  • Project: ProjectName
  • Python Interpreter
  • Verify that exists
    • pip
    • setuptools
  • Upgrade them if needed - from the triangle

6. Conclusion

To sum up, this article shows how installing a correct modules can solve the "ModuleNotFoundError: No module named 'distutils.util'" error in Ubuntu 20+.

We also check how to fix the error for older Python versions by adding repo and install older versions of distutils.