Search code examples
pythoncondasetuptoolsdistutilspython-3.12

No module named 'distutils' despite setuptools installed


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 Miniconda 3:

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

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

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


Solution

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