161

I was wanting to upgrade my python version (to 3.10 in this case) so after installing python3.10 I proceeded to try adding some modules I use e.g. opencv , which ran into:

jeremy@jeremy-Blade:~$ python3.10 -m pip install opencv-python 
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3/dist-packages/pip/__main__.py", line 16, in <module>
    from pip._internal.cli.main import main as _main  # isort:skip # noqa
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/main.py", line 10, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/cmdoptions.py", line 19, in <module>
    from distutils.util import strtobool
ModuleNotFoundError: No module named 'distutils.util'

jeremy@jeremy-Blade:~$ sudo apt-get install python3-distutils
[sudo] password for jeremy: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-distutils is already the newest version (3.8.10-0ubuntu1~20.04).
...
0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.

Since distutils already seems to be installed , I can't grok how to proceed.

6
  • it's related to your python version. Commented Oct 12, 2023 at 17:28
  • 1
    This question is not about the removal of distutils from the standard library in 3.12. It is about the fact that Setuptools patches Distutils, adding a util subpackage that was not already there in the standard library version. Commented Apr 24 at 16:04
  • 1
    (See stackoverflow.com/questions/77233855 for the problem caused by the removal of distutils in 3.12.) Commented May 24 at 3:17
  • @KarlKnechtel distutils.util is there in the stdlib distutils github.com/python/cpython/blob/v3.10.14/Lib/distutils/util.py . setuptools does not patch distutils, afaict, they vendor it.
    – wim
    Commented Aug 21 at 18:22
  • 1
    @KarlKnechtel It's not a patch - that's for a custom loader so import statements will find distutils from setuptools vendoring, not from site-packages directly. When setuptools is installed, you will find this loader in the first element of the sys.meta_path. There is no patching involved here. As for removed pieces of distutils, you can blame the distro for that - see Negative Python user experience on Debian/Ubuntu.
    – wim
    Commented Aug 21 at 22:23

19 Answers 19

248

Install setuptools in your machine

pip install setuptools

This solved my problem

8
  • 22
    For people who has python latest(3.12) this worked. Can someone explain me why ? Thanks Commented Nov 1, 2023 at 20:17
  • 16
    You can check the PR gh-95299 which states: Do not pre-install setuptools in virtual environments created with venv. This means that distutils, setuptools, pkg_resources, and easy_install will no longer available by default; to access these run pip install setuptools in the activated virtual environment. Commented Nov 9, 2023 at 5:39
  • 1
    @AravindReddy check the answer above stackoverflow.com/a/76691103/4245534
    – HaMi
    Commented Jan 24 at 18:09
  • 1
    pip3 install setuptools Commented Feb 7 at 14:26
  • 1
    @AravindReddy This question is about Python 3.10. Installing Setuptools works because it creates a "fully featured" importable distutils package. The one that comes with Python can be missing pieces, depending on how Python was installed (especially if it came with certain Linux distributions) - hence the problem OP encountered. In Python 3.12 the entire thing is removed from the standard library. Commented Apr 24 at 16:08
211

distutils package is removed in python version 3.12

It was deprecated in Python 3.10 by PEP 632 “Deprecate distutils module”. For projects still using distutils and cannot be updated to something else, the setuptools project can be installed: it still provides distutils.

Explanation and workaround:

Python 3.12 Release document mentions :

gh-95299: Do not pre-install setuptools in virtual environments created with venv. This means that distutils, setuptools, pkg_resources, and easy_install will no longer available by default; to access these run pip install setuptools in the activated virtual environment.

https://docs.python.org/3.12/whatsnew/3.12.html


use below command in your virtual environment

pip install setuptools

OR

brew install python-setuptools

OR

sudo apt install python3-setuptools

OR

if it doesn't work, you may need stay on Python < 3.12 until the package is supported.


OR

Sometimes, setuptools is installed but, you still need to upgrade:

pip install --upgrade setuptools

Python 3.12 does not come with a stdlib distutils module (changelog), because distutils was deprecated in 3.10 and removed in 3.12

You can still use distutils on Python 3.12 by installing setuptools.

Full migration advice : https://peps.python.org/pep-0632/#migration-advice

6
  • 2
    Not a replacement as such but, you can install setuptools in your virtual environment for your needs. Commented Jan 9 at 16:58
  • What does it do exactly is it something that can be removed from the project? Commented Apr 24 at 9:14
  • 1
    option 3 I like the best because you don't have to fiddle with virtual environments, and if you just want things to work like the way it was before. (assuming you also have python from brew)
    – 1mike12
    Commented Jul 19 at 23:06
  • The O.P. was using Python 3.10
    – wim
    Commented Aug 21 at 18:18
  • 2
    @wim because this is the first link that pops up on a google search and anyone running python 3.12+ will run into this issue soon. Many things depends on distutils (including NodeJS) as well. In summary, its an answer that addresses the concern for too many people. Commented Sep 21 at 20:22
52

It looks like distutils has versioning , so after

jeremy@jeremy-Blade:~$ sudo apt-get install python3.10-distutils 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
...
Setting up python3.10-lib2to3 (3.10.0-1+focal1) ...
Setting up python3.10-distutils (3.10.0-1+focal1) ...

jeremy@jeremy-Blade:~$ python3.10 -m pip install opencv-python 

seems to be able to proceed.

3
  • 5
    Didnt work for me I still get "ModuleNotFoundError: No module named 'distutils.util'"
    – Definity
    Commented Nov 11, 2021 at 16:32
  • 1
    sorry I'm not sure what would cause that , can you post the whole errmsg or maybe start a new question? Commented Nov 11, 2021 at 20:53
  • exactly, python 3.10 did the trick for me. :)
    – sumonst21
    Commented Aug 11 at 14:05
13

This solved it for me

sudo apt-get install --reinstall python3.7-distutils

You can also replace 3.7 with your version of python

6
sudo apt-get install python3.9-distutils

This solved my problem i am using Python3.9

0
4

If it is possible, you can try to downgrade your python version below 3.12 i.e. 3.11 or lower

2
  • 2
    Yes! If you install python3.12-dev on Ubuntu 22.04, it may break packages that rely on distutils such as ament-cmake-python. Commented Feb 3 at 4:58
  • Yep, experiencing this on a CircleCI Electron app build pipeline right now. Ugh. Commented Feb 20 at 3:50
4

I'm not a Python developer, but I needed to install a Node.js package that required node-gyp, so I didn't want to install pip just for that. Instead, I just installed setuptools as follows on Ubuntu 24.04:

sudo apt install python3-setuptools
4

Install distutils for your-python-version:

!sudo apt-get install python<your-python-version>-distutils

For example, to install in Python version 3.8:

sudo apt-get install python3.8-distutils

Specification: In Python 3.10 and 3.11, distutils will be formally marked as deprecated. All known issues will be closed at this time.

Note: You could still use distutils with warning in 3.10 and 3.11 and any issues that arises will not be fixed. But, Code that imports distutils will no longer work from Python 3.12.

For more info: Migration Advice

2
2

On windows, here are steps worked for me:

  • downgrade python version 3.12.2 to 3.11.9
  • cleared the cache "pip cache purge"
  • removed all the versions numbers from the requirements.txt file.
  • run "pip install -r .\requirements2.txt"
1

This solved my problem

sudo apt-get install python3.10-distutils

I am using python 3.10

1

I faced with that problem on Windows and other answers didn't help. That's because I have two versions of Python3 (3.8 and 3.12) on my machine. In this case call:

pip3 install setuptools

just installs setuptools for Python 3.8:

$ pip3 install setuptools
Requirement already satisfied: setuptools in  
  c:\users\username\appdata\local\programs\python\python38\lib\site-packages (69.2.0)

Meanwhile python3 refers to another version:

$ python3 --version
Python 3.12.2

To correctly install setuptools I have to run pip in a different way:

python3 -m pip install setuptools
1
  • 2
    I tried all three variants of the command (pip, pip3, python3 -m) with Python 3.12 on Windows cmd and I get this error: ...from distutils.cmd import Command as DistutilsCommand ModuleNotFoundError: No module named 'distutils'
    – A. L
    Commented Apr 30 at 6:49
1

If your python or python3 is installed via Homebrew, you can use

brew install python-setuptools
0
0

I needed to do sudo apt install python3.10-venv.

$ python3 -m venv venv1
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.10-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: /home/mono/Sachen/CodeLibrary/Python/2024October5/XOrithm Take Home Assignment/venv1/bin/python3

$ sudo apt install python3.10-venv
0

The solution that worked for me: I installed visual c++ build tools

and restart my machine.

0

These steps solved my problem while installing Django version (2.1) on windows

  • pip install setuptools

then run

  • django-admin startproject projname .
0
  • Environment: Raspberry Pi 4 (RPI), armv7l
  • OS: Raspbian GNU/Linux 10 (buster)
  • Default python version: Python v3.7

Installed python 3.12 from sources, started getting "ModuleNotFoundError: No module named 'distutils'" when executing pip3.

The following worked for virtualenv / particular project that uses poetry:

pip3 install virtualenv

cd /home/user/projects/abcd

# Notice --pip 24.2 argument - this is very important, by default virtualenv will use pip3 from python3.7 and everything is broken
virtualenv /home/user/projects/abcd/.venv --python=/usr/local/bin/python3.12 --pip 24.2

# all good, before this was failing with "No module named 'distutils'" error
pip3 install poetry

-1

On Windows CMD and using a virtual environment, the fix for me was to delete and recreate the virtualenv by explicitly using the new Python version with the full path.

C:\myvirtualenvFolder> mkvirtualenv -p C:\Users\myUser\AppData\Local\Programs\Python\Python312\python.exe myVirtualEnvName

(note, that this command without full path didn´t work, even though it created the virtual environment successfully: C:\myvirtualenvFolder> mkvirtualenv -p python3.12 myVirtualEnvName)

After that, I activated my virtualenv:

workon myVirtualEnvName (or use activate.bat Script)

Eventually also pip worked without the annyoing "ModuleNotFoundError: No module named 'distutils'" error:

pip install -r requirements.txt

-1
sudo apt-get install python3.12-venv

It told me that setuptools, distutils, pip, and wheel were all installed already. When I tried python3 -m ensurepip it gave me "No Module Found 'ensurepip'" Not sure what broke it, but this fixed it.

-2
py -m ensurepip --upgrade

my problem was i couldn't use pip or sudo to install anything so the above command reinstalled pip and fixed the issue. hope that helps anyone else looking for a solution.

Not the answer you're looking for? Browse other questions tagged or ask your own question.