Colab安装PyTorch相关包陷入无限循环问题求助
Colab安装PyTorch Geometric相关包无限循环问题的成因及解决方法
可能成因
- Colab自带的PyTorch版本带有额外后缀(比如
2.0.0+cu118),直接用torch.__version__拼接PyG的whl路径时会导致路径无效,pip找不到匹配的预编译包,只能反复尝试源码编译,而Colab缺少编译依赖,最终卡住不动。 - PyG的git源码安装过程中,网络波动或者Colab的资源限制会导致克隆仓库、编译代码的过程停滞,看起来像无限循环。
- 不同PyG依赖包之间版本不兼容,pip在尝试自动解析依赖时陷入死循环。
解决方法
1. 修复PyTorch版本号格式
先提取不带后缀的纯净版本号,再用来安装:
import torch # 提取核心版本号,去掉+后面的后缀 torch_version = torch.__version__.split('+')[0]
然后执行安装命令:
# 先卸载旧包 !pip uninstall torch-scatter torch-sparse torch-geometric torch-cluster -y # 安装匹配的依赖包 !pip install torch-scatter torch-sparse torch-cluster -f https://data.pyg.org/whl/torch-{torch_version}.html --no-cache-dir # 安装torch-geometric !pip install torch-geometric
2. 使用PyG官方一键安装命令
直接用官方整合的安装命令,自动适配环境:
!pip uninstall torch-scatter torch-sparse torch-geometric torch-cluster torch-spline-conv -y !pip install pyg-lib torch-scatter torch-sparse torch-cluster torch-spline-conv torch-geometric -f https://data.pyg.org/whl/torch-{torch.__version__.split('+')[0]}.html
3. 手动指定兼容版本
如果自动匹配还是有问题,手动指定已知兼容的版本(以Colab常用的PyTorch 2.0.0为例):
!pip uninstall torch-scatter torch-sparse torch-geometric torch-cluster -y !pip install torch-scatter==2.1.0 torch-sparse==0.6.17 torch-cluster==1.6.0 -f https://data.pyg.org/whl/torch-2.0.0.html !pip install torch-geometric==2.3.1
4. 切换到GPU运行时
如果用的是CPU运行时,切换到GPU加速环境(菜单栏「运行时」→「更改运行时类型」→ 选择GPU),避免CPU资源不足导致编译卡住。
内容的提问来源于stack exchange,提问作者Zehra Moğulkoç
相关产品推荐
相关产品推荐

