できない.dev

pip install で wheel のビルドに失敗する

wheel が公開されていない sdist は手元でビルドが走るため、C / Rust 拡張に必要なコンパイラやヘッダが揃っていないと失敗する。
ビルドツールチェインを入れる、wheel が出ている Python に合わせる、setuptools / pip を更新するで切り分ける。

#pip#wheel#build#c-extension#rust

要約

error: command 'gcc' failed Building wheel for X (pyproject.toml) did not run successfully. は、wheel が公開されていない sdist を手元でビルドしようとして 必要なコンパイラ / ヘッダが揃っていない 状態。
OS の開発ツール、Python 開発ヘッダ、Rust ツールチェインのいずれかを入れれば多くが解消する。
先に wheel が出ている Python に切り替えるのが最短のケースも多い。

よくある原因

  1. コンパイラ不在: Linux なら gcc、macOS は Xcode Command Line Tools、Windows は VS Build Tools が無い。
  2. 開発ヘッダ不足: Python.h が見つからない場合は python3-dev(Debian/Ubuntu)や python3-devel(RHEL 系)が必要。
  3. Rust 拡張: 最近の暗号系(cryptography 等)は Rust が必須。error: can't find Rust compiler で気付く。
  4. system ライブラリ不足: libssl-dev / libffi-dev / libxml2-dev などのヘッダが無く、リンク段階で失敗する。
  5. 古い pip / setuptools: PEP 517 の build-system 指定を読めず、build バックエンドの呼び出しで詰まる。

解決策

1. OS の開発ツールを入れる

# Debian / Ubuntu
sudo apt-get install build-essential python3-dev libssl-dev libffi-dev
 
# macOS
xcode-select --install
 
# Windows
# Visual Studio Build Tools の Desktop development with C++ を選んでインストール

2. Rust ツールチェインを入れる

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
. "$HOME/.cargo/env"
pip install cryptography

cryptography / pydantic-core / orjson などは Rust 拡張を含む。
rustup で stable を入れるのが標準的(pip install のドキュメント でも sdist のビルドツール必要性に触れている)。

3. pip と setuptools を更新

python -m pip install -U pip setuptools wheel

古い pip だと pyproject.tomlbuild-system を正しく読めず、backend-path 不在で落ちることがある。

4. wheel が出ている Python に切り替え

pyenv install 3.11.9
pyenv local 3.11.9
pip install pandas

最新 Python(リリース直後)は wheel がまだ揃っておらず、sdist ビルドに回ることが多い。
PyPI のパッケージページで cp311 等の wheel ファイル名が公開されているマイナーに合わせるのが手堅い(sdist 仕様の公式ドキュメント)。

この記事は役立ちましたか?