No module named 'distutils' despite setuptools installed

505 Views Asked by At

I get

$ pip install -e .
...
ModuleNotFoundError: No module named 'distutils'

with Python 3.12 despite

$ conda install setuptools
...
All requested packages already installed.
$ pip install setuptools
Requirement already satisfied: setuptools in c:\users\...\appdata\local\miniconda3\envs\c312\
lib\site-packages (69.0.3)

My pyproject.toml is

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "zzz"
authors = [
    ...
]
description = "zzz"
readme = "README.md"
version = "36"
dependencies = [
    "numpy",
    "pandas",
]

[project.optional-dependencies]
test = [
    "coverage",
]
speed = [
    "python-rapidjson",
]

[project.urls]
Homepage = "..."

[tool.setuptools.packages.find]
exclude = ["tests"]

[tool.pylint.'MESSAGES CONTROL']
max-bool-expr = 10

My setup with miniconda3:

$ conda create --name c312 python=3.12 pandas matplotlib scipy scikit-learn ...
...
$ conda activate c312
$ which pip
/c/Users/.../AppData/Local/miniconda3/envs/c312/Scripts/pip
$ pip --version
pip 23.3.2 from C:\Users\...\AppData\Local\miniconda3\envs\c312\Lib\site-packages\pip (python 3.12)
$ conda --version
conda 23.11.0
$ which python
/c/Users/.../AppData/Local/miniconda3/envs/c312/python
$ python
Python 3.12.1 | packaged by conda-forge | (main, Dec 23 2023, 07:53:56) [MSC v.1937 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import setuptools
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ssteingold\AppData\Local\miniconda3\envs\c312\Lib\site-packages\setuptools\__init__
.py", line 8, in <module>
    import distutils.core
ModuleNotFoundError: No module named 'distutils'
>>> import distutils
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'distutils'
>>>
$ conda list '^(python|setuptools)$'
# packages in environment at C:\Users\...\AppData\Local\miniconda3\envs\c312:
#
# Name                    Version                   Build  Channel
python                    3.12.1          h2628c8c_1_cpython    conda-forge
setuptools                69.0.3             pyhd8ed1ab_0    conda-forge

what am I doing wrong?

PS. The following questions

basically say "install setuptools" - but I already have it (and shouldn't it be auto-installed because I require it in pyproject?)

The question Unable to import a module that is definitely installed has nothing to do with mine.

ANSWER

Unset SETUPTOOLS_USE_DISTUTILS (it was set to stdlib for the sake of broken msys/python).

2

There are 2 best solutions below

0
Charles Duffy On BEST ANSWER

This problem is taking place because you have SETUPTOOLS_USE_DISTUTILS=stdlib set in your environment, while Python 3.12 no longer provides any standard-library distutils version at all. There is no longer any "stdlib" distutils, so it cannot be selected.


To go into the mechanism a bit:

setuptools provides a module _distutils_hack, which when loaded replaces sys.modules['distutils'] to point to setuptools._distutils; however, it only does this when SETUPTOOLS_USE_DISTUTILS is either unset or has the value 'local'.

4
Daniel Perez Efremova On

Problem

Seems a version problem. pip and conda are using different binaries.

Run

pip --version

to confirm that it points to the virtual env you expected to. You should see something like

pip 23.3.1 from <your path>\envs\<env name>\Lib\site-packages\pip (python 3.12)

Solution

Create a Python 3.12 env, activate it and run the installation. I tested it and it does not have any dependencies problem.

conda create env -n py312 python=3.12
conda activate py312 
python -m pip install -e .