215

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

python3.10 -m pip install opencv-python 

Output:

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' 

And

sudo apt-get install python3-distutils 

Output:

[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.

7
  • it's related to your python version. Commented Oct 12, 2023 at 17:28
  • 2
    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, 2024 at 16:04
  • 1
    (See stackoverflow.com/questions/77233855 for the problem caused by the removal of distutils in 3.12.) Commented May 24, 2024 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. Commented Aug 21, 2024 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. Commented Aug 21, 2024 at 22:23

20 Answers 20

319

Install setuptools in your machine

pip install setuptools 

This solved my problem

Sign up to request clarification or add additional context in comments.

9 Comments

For people who has python latest(3.12) this worked. Can someone explain me why ? Thanks
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.
@AravindReddy check the answer above stackoverflow.com/a/76691103/4245534
pip3 install setuptools
@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.
|
267

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

7 Comments

Not a replacement as such but, you can install setuptools in your virtual environment for your needs.
What does it do exactly is it something that can be removed from the project?
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)
@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.
I had found "pip install setuptools" in a few places. But this answer included the "you started with homebrew, you gotta stay with homebrew" missing link. the "brew install" above. Thank you.
|
53

It looks like distutils has versioning, so after the following, it seems to be able to proceed.

sudo apt-get install python3.10-distutils 

Output:

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) ... 

And:

python3.10 -m pip install opencv-python 

3 Comments

Didnt work for me I still get "ModuleNotFoundError: No module named 'distutils.util'"
sorry I'm not sure what would cause that , can you post the whole errmsg or maybe start a new question?
exactly, python 3.10 did the trick for me. :)
14

This solved it for me

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

You can also replace 3.7 with your version of python

Comments

6
sudo apt-get install python3.9-distutils 

This solved my problem i am using Python3.9

Comments

5

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 Comments

Thanks! sudo apt-get install python3.12-distutils worked for me (2024).
hmm: distutils package is removed in python version 3.12 stackoverflow.com/a/76691103/2278511
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 

Comments

4

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

3 Comments

Yes! If you install python3.12-dev on Ubuntu 22.04, it may break packages that rely on distutils such as ament-cmake-python.
Yep, experiencing this on a CircleCI Electron app build pipeline right now. Ugh.
No one should ever change their distro's system python. Python is baked in to so much of the OS and everything expecting that version. Im fairly sure most distros wont even let you change the system python. Instead, use uv. Then you can set up a per app/per folder/per venv python version. It's an easy one line install: curl -LsSf astral.sh/uv/install.sh | sh Want a python 3.10 venv in some folder? uv venv -p 3.10 --seed
3

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

brew install python-setuptools 

Comments

3

On Windows, here are steps worked for me:

  • downgrade the 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"

Comments

3

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

pip install setuptools 

Then run

django-admin startproject projname . 

Comments

2

This solved my problem

sudo apt-get install python3.10-distutils 

I am using python 3.10

Comments

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 Comment

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'
1
  • 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 

Comments

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 

Comments

0

The solution that worked for me:

I installed Visual C++ build tools and restarted my machine.

Comments

0

Inside your venv run this

pip install -U setuptools 

Comments

-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

Comments

-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.

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.