pythonpython-3.xsetuptoolsdistutils

Windows: ModuleNotFoundError: No module named 'distutils'


Python 3.7.2
Pip 18.1
setuptools 40.7.2
Windows-10-10.0.14393-SP0

I've seen people having the No module named 'distutils issue on Linux/Ubuntu (for example, here and here). The reported solution is installing distutils with apt-get:

apt-get install python3-distutils

Or, people had the issue very long ago (for example, here).

However, I'm getting this error on new Windows (Windows-10-10.0.14393-SP0), new Python (3.7.2) and cannot figure out why distutils is upset and/or how to install distutils.

I can import distutils in Python so I assume it to be installed. But setuptools is unhappy.

File "site-packages\setuptools\__init__.py", line 6, in <module>
ModuleNotFoundError: No module named 'distutils'

Directly from Python, it doesn't complain about distutils.

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils.core
>>> import setuptools
>>>

But, when I try to run a Python script, it complains. Any thoughts?


Solution

  • This error might show up in case if you created a new virtual environment while creating the project.

    You can sort it out by opening the directory where your project is present, and then by searching and deleting the "venv" folder.

    Now when you will re-run the project, the IDE is gonna ask you to setup the python interpreter. At that time instead of selecting the virtual environment, go with the option for using pre-existing interpreter option.

    -- What happens when you select "create virtual environment" is that your IDE creates a new directory named "virenv" and copies all the python files from Python/bin to this folder and then import modules from here. So the module might be already installed in the native python directory but might no be imported to the virenv directory and hence resulting in the ModuleNotFoundError.