How to Solving Python pip Error "Preparing metadata (pyproject.toml) did not run successfully"
When installing Python packages using pip
, you might encounter the error message Preparing metadata (pyproject.toml) ... error
, often followed by subprocess-exited-with-error
. This indicates that pip
failed during an early stage of the installation process, specifically while trying to determine the package's build dependencies and metadata defined in its pyproject.toml
file.
This guide explains the common reasons for this build system error and provides systematic solutions to resolve it.
Understanding the Error: pyproject.toml
and Build Metadata
Modern Python packages often use a pyproject.toml
file (defined in PEP 517 and PEP 518) to specify how the package should be built. This includes listing build-time dependencies, i.e. other packages (like setuptools
, wheel
, cython
, etc.) needed just to build the package itself from source, often before its regular runtime dependencies can even be determined.
The "Preparing metadata (pyproject.toml) did not run successfully" error means pip
encountered a problem while executing the build backend specified in pyproject.toml
to get this essential metadata. This usually happens because either the necessary build tools are missing/outdated, or the build process itself failed due to incompatibilities or missing system-level dependencies (like C compilers). It's fundamentally a build system failure, not just a package-not-found issue.
Common Causes
- Outdated
pip
,setuptools
, orwheel
: These core packaging tools might be too old to correctly interpretpyproject.toml
or handle modern build backends. This is the most common cause. - Python Version Incompatibility: The package explicitly requires a different Python version than the one you're using.
- Missing Build Dependencies: The package requires external tools or libraries to build from source (e.g., a C/C++ compiler, Rust compiler, specific development headers like
python3-dev
) that are not installed on your system. - Package-Specific Issues: Some packages have unique, complex build requirements not immediately obvious (e.g., PyQt5 often needs the Qt development libraries installed separately).
- Network Issues: Less common for this specific error stage, but problems downloading build dependencies could theoretically contribute.
- Bugs: A bug in the package's build script or in the build backend itself.
Solution 1: Upgrade pip, setuptools, and wheel (Crucial First Step)
Always ensure you have the latest versions of the fundamental packaging tools, as they frequently receive updates to support new standards like pyproject.toml
.
# Recommended command (targets the active Python environment's pip)
python -m pip install --upgrade pip setuptools wheel
# Alternatives (use pip/pip3 appropriate for your environment)
pip install --upgrade pip setuptools wheel
pip3 install --upgrade pip setuptools wheel
After upgrading these, immediately try installing the problematic package again. This often resolves the issue.
Solution 2: Check Python Version Compatibility
Verify that your current Python version is supported by the package.
- Check Your Python Version:
python --version
# Or: python3 --version - Check Package Requirements: Find the package on PyPI (e.g., search "pypi packagename"). Look at the "Requires: Python" section on the project page (usually in the sidebar or project details).
- Match Versions: If your Python version is outside the required range (e.g., package requires
>=3.7,<3.11
but you have 3.11 or 3.6), you will need to either:- Use a different, compatible Python environment (e.g., create a new virtual environment with a supported Python version using tools like
pyenv
orconda
). - Wait for the package maintainers to release a version supporting your Python version.
- Find an older version of the package that does support your Python version (see Solution 6).
- Use a different, compatible Python environment (e.g., create a new virtual environment with a supported Python version using tools like
Solution 3: Install Missing Build Dependencies (Compilers, Headers)
If the error message or verbose logs (see Section 9) mention missing compilers (like GCC, Clang, MSVC), development headers (...-dev
or ...-devel
packages), or other build tools (like Rust), you need to install them at the system level. This is common for packages with C extensions.
- Debian/Ubuntu:
sudo apt update
sudo apt install build-essential python3-dev # Common requirements
# Add others as needed, e.g., libssl-dev, libffi-dev, rustc - Fedora/CentOS/RHEL:
sudo dnf update # or yum
sudo dnf groupinstall "Development Tools" # or yum groupinstall ...
sudo dnf install python3-devel # Common requirements
# Add others as needed, e.g., openssl-devel, libffi-devel, rust - macOS: Install Xcode Command Line Tools (includes Clang compiler):
xcode-select --install
# May need other tools via Homebrew (brew install rust, etc.) - Windows: Install Microsoft C++ Build Tools (available as part of Visual Studio Installer - select "Desktop development with C++"). Sometimes specific SDKs might be needed.
Consult the specific package's documentation for its exact build dependencies. After installing them, try the pip install
command again.
Solution 4: Address Package-Specific Requirements (e.g., PyQt5/Qt)
Some complex packages have specific system library dependencies beyond standard build tools.
- Example (PyQt5/PyQt6): Often requires the base Qt libraries to be installed.
- On macOS:
brew install qt@5
(orqt@6
) - On Linux:
sudo apt install qtbase5-dev
(or similar package name for your distribution/Qt version) - Sometimes linking is needed:
brew link qt@5 --force
(macOS, use specific Qt version)
- On macOS:
Always check the official installation instructions for the specific package giving you trouble.
Solution 5: Try Pre-Release Versions (--pre
)
If the stable version of the package doesn't support your (perhaps newer) Python version or has a known build bug, a pre-release might contain a fix. Use this cautiously.
# Replace 'packagename' with the actual package
pip install --pre packagename
The --pre
flag allows pip
to consider alpha, beta, or release candidate versions.
Solution 6: Install an Older Package Version
If the latest version is incompatible (e.g., due to Python version support), try installing a specific, older version known to work with your setup.
- Find available versions: Leave the version blank after
==
and press Tab (in some shells) or run the command –pip
will often list available versions if it fails. Or check the "Release history" on PyPI.pip install packagename==
# Example error output might list versions: (from versions: 1.0, 1.1, 2.0, 2.1rc1) - Install a specific version:
pip install packagename==1.1 # Install version 1.1
Debugging with Verbose Output (-vvv
)
To get much more detailed information about exactly where the metadata preparation is failing, use the verbose flag (-v
, -vv
, or -vvv
for maximum detail).
# Replace 'packagename' with the actual package
pip install packagename -vvv
Examine the detailed output carefully. Look for specific compiler errors, missing file errors, or messages indicating incompatibility just before the main error occurs. This often points directly to the root cause (e.g., a missing C header file).
Deprecated Legacy Resolver (Less Common Fix for This Error)
The --use-deprecated=legacy-resolver
flag tells pip
to use its older dependency resolution logic. While sometimes helpful for complex runtime dependency conflicts, it's less likely to fix the "Preparing metadata" build error itself, unless the build failure is indirectly caused by pip
failing to install a build dependency due to resolver issues. It's generally better to fix the root cause identified by other methods. If you must try it:
pip install packagename --use-deprecated=legacy-resolver
Conclusion
The Preparing metadata (pyproject.toml) did not run successfully
error indicates a failure during the package build process, specifically when pip
interacts with the build system defined in pyproject.toml
.
The most effective solutions usually involve:
- Upgrading
pip
,setuptools
, andwheel
: Ensures you have tools that understand modern packaging standards. - Checking Python version compatibility: Make sure your Python version is supported by the package.
- Installing missing system-level build dependencies: Such as C/C++ compilers (
build-essential
, Xcode Tools, MS Build Tools) and development headers (python3-dev
, etc.). - Consulting package-specific documentation for unique requirements (like Qt for PyQt).
- Using verbose output (
-vvv
) to pinpoint the exact failure point during the build.
Addressing these build environment and compatibility issues is key to resolving this pip
installation error.