Open1

Kaggle NotebookでGitHubにあるPythonプロジェクトをwheelにして高速にインストールする

colum2131colum2131

オフライン環境のKaggle Notebookにおいて新しくライブラリを追加する場合は、wheelを作成してインストールする方法やDependency Managerを使う方法がある。今回は前者である。

Pythonの多くのライブラリはPyPIにて公開されている。一方でGitHub上で公開されて、GItHubリポジトリを指定してbuildが必要な場合がある。

pipでもuvでもsetup.pypyproject.tomlにbuild用の設定がされていたら、URL指定でインストールすることができる。例えば、VGGTを以下のコマンドでuvでインストールできる。uv add <URL>でも同様に実行できる。

uv pip install https://github.com/facebookresearch/vggt.git

KaggleにてNotebook提出のコンペの場合は、通常オフラインで実行できるようにするためにwheelを別のNotebookで作成したりDatasetにアップロードしたりする必要がある。

その場合は以下のようにpip wheelでwheelをbuildするといい。-wwheels/ディレクトリに依存するパッケージとそのbuild対象のパッケージのwheelを作成する。

pip wheel git+https://github.com/facebookresearch/vggt.git -w wheels/
wheels/
├── certifi-2025.7.9-py3-none-any.whl
├── charset_normalizer-3.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
├── einops-0.8.1-py3-none-any.whl
├── filelock-3.18.0-py3-none-any.whl
├── fsspec-2025.5.1-py3-none-any.whl
├── hf_xet-1.1.5-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
├── huggingface_hub-0.33.4-py3-none-any.whl
├── idna-3.10-py3-none-any.whl
├── numpy-1.26.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
├── opencv_python-4.11.0.86-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
├── packaging-25.0-py3-none-any.whl
├── pillow-11.3.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
├── PyYAML-6.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
├── requests-2.32.4-py3-none-any.whl
├── safetensors-0.5.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
├── tqdm-4.67.1-py3-none-any.whl
├── typing_extensions-4.14.1-py3-none-any.whl
├── urllib3-2.5.0-py3-none-any.whl
└── vggt-0.0.1-py3-none-any.whl

Command 'pip' not foundが出力されたらuvx pip wheel ...として実行する。uvにはpip wheelというコマンドがないため、uvxpipを呼び出してpip wheelを実行します。なんか不思議な感じですね。

uvx pip wheel git+https://github.com/facebookresearch/vggt.git -w wheels/

依存するパッケージのwheelが必要なければ--no-depsを付けて実行する。

pip wheel git+https://github.com/facebookresearch/vggt.git --no-deps -w wheels/
wheels/
└── vggt-0.0.1-py3-none-any.whl

buildしたwheelはpip installでインストールすることができる。uvの場合は以下:

uv pip install wheels/vggt-0.0.1-py3-none-any.whl

もし依存関係があるライブラリを人力で解決できる場合は、必要なwheelを全て作成して、--no-depsをつけることでそのwheelだけをインストールすることができる。これは依存関係の解決をスキップするためインストール自体が高速になる(実行時に必要なパッケージが足りないことに注意が必要)。

uv pip install wheels/* --no-deps