no module named distutils....but distutils installed?
Asked Answered
C

13

69

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.

Cassiterite answered 10/11, 2021 at 20:42 Comment(2)
it's related to your python version.Amphictyon
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.Parterre
C
48

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.

Cassiterite answered 10/11, 2021 at 20:45 Comment(2)
Didnt work for me I still get "ModuleNotFoundError: No module named 'distutils.util'"Libertine
sorry I'm not sure what would cause that , can you post the whole errmsg or maybe start a new question?Cassiterite
I
106

Install setuptools in your machine

pip install setuptools

This solved my problem

Inflection answered 25/10, 2023 at 15:2 Comment(6)
For people who has python latest(3.12) this worked. Can someone explain me why ? ThanksKerril
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.Cadel
worked on fedora39 linux. the package is needed to work with gradio packagePalace
@AravindReddy check the answer above https://mcmap.net/q/175160/-no-module-named-distutils-but-distutils-installedPhotoluminescence
pip3 install setuptoolsStew
@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.Parterre
C
89

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

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

Consequently answered 14/7, 2023 at 21:4 Comment(2)
Not a replacement as such but, you can install setuptools in your virtual environment for your needs.Consequently
What does it do exactly is it something that can be removed from the project?Caldarium
C
48

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.

Cassiterite answered 10/11, 2021 at 20:45 Comment(2)
Didnt work for me I still get "ModuleNotFoundError: No module named 'distutils.util'"Libertine
sorry I'm not sure what would cause that , can you post the whole errmsg or maybe start a new question?Cassiterite
A
12

This solved it for me

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

You can also replace 3.7 with your version of python

Agential answered 12/6, 2022 at 13:7 Comment(0)
L
6
sudo apt-get install python3.9-distutils

This solved my problem i am using Python3.9

Loraineloralee answered 11/12, 2022 at 11:27 Comment(0)
S
5

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

Sindhi answered 6/12, 2023 at 9:3 Comment(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.Akins
Yep, experiencing this on a CircleCI Electron app build pipeline right now. Ugh.Fry
I
3

Install distutils for your-python-version

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

For example, to install in 3.8 version

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 distutisl 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: Migragtion Advice

Implicative answered 19/8, 2023 at 14:8 Comment(0)
N
2

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

brew install python-setuptools
N answered 8/4 at 15:50 Comment(0)
P
1

This solved my problem

sudo apt-get install python3.10-distutils

I am using python 3.10

Pontone answered 4/1, 2023 at 7:36 Comment(0)
B
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
Bathysphere answered 27/3 at 14:25 Comment(1)
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'Ranged
R
0

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

Ranged answered 30/4 at 8:3 Comment(0)
M
0

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"
Mesocarp answered 1/5 at 2:17 Comment(0)
S
-1
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.

Stieglitz answered 7/11, 2023 at 0:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.