pip build wheel failed
ERROR: Failed building wheel for package
Traceback
Traceback (most recent call last):
File "main.py", line 2, in <module>
# ERROR: Failed building wheel for psycopg2
ERROR: Failed building wheel for packageWhat causes this error
pip could not compile a package from source. This usually means missing compilers, development headers, or system libraries required by the package's C/C++/Rust extensions.
How to fix it
Install system build dependencies: `sudo apt install build-essential python3-dev`. Install specific library headers (check package docs). Use pre-built binary alternatives when available. Upgrade pip and setuptools.
Code that causes this error
pip install psycopg2 # ERROR: Failed building wheel for psycopg2
Fixed code
# Option 1: Install system dependencies # sudo apt install libpq-dev python3-dev # Option 2: Use the pre-built binary pip install psycopg2-binary # Option 3: Ensure build tools are available pip install --upgrade pip setuptools wheel
About pip build wheel failed
This error occurs when pip cannot build a wheel (binary package) from a source distribution. Packages with C, C++, Rust, or Fortran extensions need a compiler and development headers to build from source. Common causes include missing system libraries (like `libpq-dev` for psycopg2, `libxml2-dev` for lxml), missing compilers (`gcc`, `g++`, `rustc`), and incompatible compiler versions.
Modern pip strongly prefers pre-built wheels from PyPI, but falls back to building from source when no wheel matches your platform. The fix depends on the package: install the required system development libraries, install compilers, or use an alternative package that provides pre-built wheels (e.g., `psycopg2-binary` instead of `psycopg2`). Upgrading pip and setuptools often resolves wheel-building issues with newer packages.
Common scenarios
Running scripts without sufficient file system permissions
Connecting to servers that are not running or are unreachable
Installing Python packages without proper environment setup
Running out of disk space or file descriptors during I/O operations