Windows: ModuleNotFoundError: No module named 'distutils' [duplicate]
Asked Answered
E

3

14
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?

Experienced answered 1/2, 2019 at 20:8 Comment(2)
What is the exception context? E.g. do you run any specific command, are you in a virtual environment etc. What are the steps to reproduce the error?Arianaariane
maybe this is a linux rookie hint: But if you get asked whether you're root... try sudo apt-get install python3-distutils.Kibbutznik
H
4

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.

Hump answered 6/6, 2019 at 21:17 Comment(2)
Nothing wrong answer as such, but it would help if you structured it in paragraphs for easier reading.Monosyllable
Even though it's not exactly right, this answer helped me figure out the issue so I accepted it. In a virtual env, distutils is not an actual package but a skeleton pointing to the non-virtual Python install distutils. I'm not sure how many packages get this treatment. To get a "real" distutils, you have to go outside the virtual env.Experienced
D
8

Activate your venv and make sure Pip and Setuptools are installed and up to date:

python -m ensurepip --upgrade

python -m pip install --upgrade setuptools

This should make Distutils available for use. To check, use:

python -m distutils.core --help

You should see the help text for Distutils, which means that it is now installed and ready to use.

Ducktail answered 23/2, 2024 at 22:56 Comment(2)
This is the right answer. If a virtualenv is in use, it's probably for a good reason. Simply deleting it is missing the point.Whalebone
I get RuntimeWarning: 'distutils.core' found in sys.modules after import of package 'distutils', but prior to execution of 'distutils.core'; this may result in unpredictable behaviourEstrange
H
4

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.

Hump answered 6/6, 2019 at 21:17 Comment(2)
Nothing wrong answer as such, but it would help if you structured it in paragraphs for easier reading.Monosyllable
Even though it's not exactly right, this answer helped me figure out the issue so I accepted it. In a virtual env, distutils is not an actual package but a skeleton pointing to the non-virtual Python install distutils. I'm not sure how many packages get this treatment. To get a "real" distutils, you have to go outside the virtual env.Experienced
A
0

Ok, I know, my solution may be the worst, but it worked for me, just copy the distutils folder in my virtual environment in the same location to distutils, and the problem was solved

Amine answered 23/10, 2019 at 21:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.