执行torch_geometric radius_graph示例遇AttributeError及依赖安装问题
Torch Geometric 常见问题解决:radius_graph 错误与 pyg_lib 安装失败
一、AttributeError: 'NoneType' object has no attribute 'radius_graph'
问题根源
radius_graph 并非 torch_geometric 核心包的函数,它由 torch_cluster 依赖提供。出现该错误要么是导入路径错误,要么是 torch_cluster 未正确安装或版本不兼容。
解决步骤
- 修正导入路径:
替换错误的导入语句,使用正确路径:from torch_cluster import radius_graph - 确保 torch_cluster 版本匹配:
必须安装与你的 PyTorch(1.13/2.0)、Python 3.8、Windows 平台完全匹配的torch_cluster版本,版本不兼容会导致导入后功能不可用。
二、找不到符合要求的 pyg_lib 版本
问题根源
Windows 平台上,PyPI 提供的 pyg_lib 预编译 wheel 版本有限,容易出现版本匹配失败。
解决步骤
- 用 Conda 一键安装(推荐):
Conda 会自动处理所有依赖的版本适配,执行以下命令:
若未安装 Conda,先安装对应版本的 PyTorch,再执行此命令。conda install torch_geometric=2.3.0 pyg_lib torch_cluster torch_scatter torch_sparse -c pyg - 手动下载 wheel 安装:
找到适配 PyTorch 1.13/2.0、Python 3.8、Windows 的 pyg_lib 和 torch_cluster 等依赖的 wheel 文件,本地安装:pip install ./pyg_lib-xxx.whl ./torch_cluster-xxx.whl - 验证安装:
运行以下代码确认依赖可用:import torch import torch_cluster from torch_cluster import radius_graph x = torch.randn(10, 3) edge_index = radius_graph(x, r=1.0) print(edge_index)
关键注意事项
- 始终在干净的虚拟环境中操作:避免多个 PyTorch 版本或旧依赖干扰。
- 安装顺序:先安装 PyTorch(指定版本),再安装 pyg 系列依赖,确保版本链一致。
内容的提问来源于stack exchange,提问作者Niccolò Tiezzi
相关产品推荐
相关产品推荐

