Skip to content
Latchkey

pip "Cannot import 'setuptools.build_meta'" - Build Backend Errors

pip created an isolated build environment and could not import the build backend your pyproject.toml declares. Usually [build-system].requires is missing the backend, or build isolation can’t reach it.

What this error means

A build fails very early with a message that pip cannot import the build backend (e.g. setuptools.build_meta, poetry.core.masonry.api, hatchling.build). It happens before any compiling, during build-environment setup.

pip output
ERROR: Cannot import 'setuptools.build_meta'
# or
ModuleNotFoundError: No module named 'poetry.core.masonry.api'

Diagnose it: is it the build backend or a missing system library?

Python packaging failures in CI split into build-backend configuration problems and missing system headers. The traceback usually points at the backend even when the real cause is an absent -dev package.

Terminal
python -m pip install --upgrade pip build
python -m build --wheel 2>&1 | tail -40

# a compiler error naming a .h file is a system dependency, not a Python one
#   e.g. "Python.h: No such file" -> python3-dev
#        "openssl/ssl.h"          -> libssl-dev

Common causes

build-system.requires is incomplete

The [build-system] requires list does not include the backend (or a recent enough version), so the isolated build env never installs it.

Build isolation can’t fetch the backend

With --no-build-isolation or a blocked index, the isolated environment cannot install the declared backend, so the import fails.

How to fix it

Declare the backend correctly

Make sure pyproject.toml lists the backend in requires and names it in build-backend.

pyproject.toml
[build-system]
requires = ["setuptools>=64", "wheel"]
build-backend = "setuptools.build_meta"

Fix build isolation

  1. If you pass --no-build-isolation, pre-install the backend yourself (pip install setuptools wheel).
  2. Ensure the build env can reach an index so it can fetch requires.
  3. Upgrade pip so it honors modern pyproject.toml build-system declarations.

How to prevent it

  • Always declare a complete [build-system] table in pyproject.toml.
  • Pre-install the backend when disabling build isolation.
  • Keep pip current so PEP 517 builds behave correctly.

Frequently asked questions

What causes pip "Cannot import 'setuptools.build_meta'"?
There are 2 common causes: build-system.requires is incomplete and build isolation can’t fetch the backend. The [build-system] requires list does not include the backend (or a recent enough version), so the isolated build env never installs it.
How do I fix pip "Cannot import 'setuptools.build_meta'"?
There are 2 fixes depending on which cause you have: declare the backend correctly and fix build isolation. Work through them in order, since the first is the most common.
What does pip "Cannot import 'setuptools.build_meta'" actually mean?
A build fails very early with a message that pip cannot import the build backend (e.g.
How do I stop pip "Cannot import 'setuptools.build_meta'" happening again?
Always declare a complete [build-system] table in pyproject.toml. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card