🐙

Claude Code Sandbox で Node.js イメージに Python を追加する際の PEP668 エラー解決法

に公開

はじめに

https://github.com/anthropics/claude-code/tree/main/.devcontainer

  • Claude Code のレポジトリで公開されている devcontainer を使用して Python 環境を立てて使用したいと思った。
  • ただし、レポジトリに公開されているベースイメージが Node.js のため、Python を別途インストールする必要がある。
  • そのままインストールするとライブラリを pip で別途追加する際に、PEP668由来の error: externally-managed-environment が表示され、インストールできない。

解決方法

Dockerfile
# Python3のインストール
RUN apt-get update && apt-get install -y \
+    python3 \
+    python3-pip \
+    python3-venv \
    && rm -rf /var/lib/apt/lists/*

+ # PEP 668の制限を解除
+ RUN rm -f /usr/lib/python*/EXTERNALLY-MANAGED

  • sandbox 内で追加で pip インストールするには、init-firewall.sh の domain に追加する必要がある
init-firewall.sh
# Resolve and add other allowed domains
for domain in \
    "registry.npmjs.org" \
    "api.anthropic.com" \
    "sentry.io" \
    "statsig.anthropic.com" \
    "statsig.com" \
    "marketplace.visualstudio.com" \
    "vscode.blob.core.windows.net" \
    "update.code.visualstudio.com" \
+    "pypi.org" \
+    "files.pythonhosted.org" \
+    "download.pytorch.org"; do
    echo "Resolving $domain..."
    ips=$(dig +noall +answer A "$domain" | awk '$4 == "A" {print $5}')
    if [ -z "$ips" ]; then
        echo "ERROR: Failed to resolve $domain"
        exit 1
    fi

PEP668 について

https://peps.python.org/pep-0668/

  • PEP 668 は、システムのパッケージマネージャー(apt など)で管理される Python と pip でインストールするパッケージの競合を防ぐための仕様とのこと。

注意点

  • あくまで、devcontainer に環境を立てて使用するものであることに留意する。

Discussion