Scikit-Learn: Error while installing with pip: Preparing metadata (pyproject.toml) did not run successfully
This error occurs when an outdated pip or setuptools version attempts to build Scikit-Learn from source without pre-compiled wheels. Upgrade build tools with python -m pip install --upgrade pip setuptools wheel.
Root Cause Analysis
This error occurs during pip install scikit-learn when pip fails during the build frontend phase (Preparing metadata (pyproject.toml) ... error) because it cannot generate package distribution metadata from source.
Root Cause 1: Outdated pip and setuptools Build Frontends
Modern Python packaging standards (PEP 517 and PEP 518) use pyproject.toml and modern build backends (meson-python or setuptools>=64). Old versions of pip (< 22.0) do not properly support modern pyproject build metadata generation.
Root Cause 2: Missing Pre-Compiled Binary Wheel for New Python Versions
When installing on a newly released Python version (such as Python 3.12/3.13 immediately after release) before official pre-compiled wheels are uploaded to PyPI, pip falls back to compiling from source. If C/C++ compilers (gcc, clang, cl.exe) or Cython/Meson are missing, metadata preparation fails.
Root Cause 3: Missing Meson and Ninja Build Tools for Scikit-Learn 1.3+
Scikit-Learn transitioned its build system to meson-python and ninja. If the isolated build environment cannot build the native extension dependencies, the build backend aborts.
Root Cause 4: Conflicting Python Architecture (32-bit vs 64-bit)
Attempting to install 64-bit Scikit-Learn wheels on a 32-bit Python interpreter triggers source build fallback and metadata failures.
Reproduction Code (MCVE)
# Simulating pip build backend metadata generation failure
class MockPipBuildFrontend:
def prepare_metadata(self, pip_version: str, has_wheel: bool):
if int(pip_version.split(".")[0]) < 22 and not has_wheel:
raise OSError(
"error: subprocess-exited-with-error\n"
" Preparing metadata (pyproject.toml) did not run successfully.\n"
" exit code: 1\n"
" note: This error originates from a subprocess, and is likely not a problem with pip."
)
build_env = MockPipBuildFrontend()
build_env.prepare_metadata(pip_version="20.2.4", has_wheel=False)
Solution 1: Upgrade pip, setuptools, and wheel in Active Interpreter
Upgrade the core packaging tools to the latest versions so pip can fetch official pre-compiled binary wheels.
import sys
# Solution 1: Terminal commands to upgrade packaging toolchain
upgrade_cmd = f'"{sys.executable}" -m pip install --upgrade pip setuptools wheel'
install_cmd = f'"{sys.executable}" -m pip install scikit-learn'
print("Execute the following commands:")
print(f" $ {upgrade_cmd}")
print(f" $ {install_cmd}")
assert "pip install --upgrade" in upgrade_cmd
Solution 2: Use Conda / Mamba Pre-Built Binary Distribution
Install Scikit-Learn via Conda or Miniforge, which provides pre-compiled binaries including BLAS/LAPACK runtimes.
# Solution 2: Conda installation command
conda_cmd = "conda install -c conda-forge scikit-learn --yes"
print("Alternative Conda installation:")
print(f" $ {conda_cmd}")
assert "conda install" in conda_cmd
If upgrading pip still triggers source compilation errors on Windows, verify that you installed 64-bit Python (not 32-bit). Check with python -c "import struct; print(struct.calcsize('P') * 8)" (must output 64).
Note de reproductibilité : Cette erreur dépend des versions des outils de packaging pip/setuptools et de la disponibilité de roues binaires précompilées pour l'OS hôte.
Contrast Preparing metadata (pyproject.toml) error with ModuleNotFoundError: The former is a package installation/build failure; the latter is a runtime import failure.