**问题描述:*仅仅使用 import dgl,就报错ModuleNotFoundError: No module named ‘setuptools.extern’、ModuleNotFoundError: No module named ‘packaging’。
安装后还会报错:FileNotFoundError: Cannot find DGL C++ graphbolt library at D:\Software\anaconda3\envs\dgl_dgl\Lib\site-packages\dgl\graphbolt*graphbolt_pytorch_2.5.1.dll,原因是没有这个文件
**问题根源:**版本不匹配。
我原来的环境是安装的12.4的pytorch(关系较大),12.4版本的cuda(关系较大),3.12的python(关系不大),dgl安装的是2.2.1。
解决方法:
查找图片里的对应版本,另新建了个环境,注意torch版本和第一张图片里的版本匹配!!!
conda install pytorch==2.3.0 torchvision==0.18.0 torchaudio==2.3.0 pytorch-cuda=12.1 -c pytorch -c nvidia
#####测试代码
import networkx as nx
import dgl
import torch
import numpy as np
# 创建一个简单的异构图,包含论文、作者和期刊节点
G = nx.MultiDiGraph()
# 添加论文、作者和期刊节点
G.add_node('P1', type='paper')
G.add_node('P2', type='paper')
G.add_node('P3', type='paper')
G.add_node('A1', type='author')
G.add_node('A2', type='author')
G.add_node('J1', type='journal')
# 定义边的源节点和目标节点索引(针对每种边类型分别定义)
writes_src = torch.tensor([0, 1]) # 写作关系中源节点(论文)的索引,对应P1和P2
writes_tgt = torch.tensor([0, 1]) # 写作关系中目标节点(作者)的索引,对应A1和A2
cites_src = torch.tensor([0, 1]) # 引用关系中源节点(论文)的索引
cites_tgt = torch.tensor([1, 2]) # 引用关系中目标节点(论文)的索引
belongs_src = torch.tensor([1, 2]) # 属于关系中源节点(论文)的索引
belongs_tgt = torch.tensor([0, 0]) # 属于关系中目标节点(期刊)的索引
# 使用 DGL 构造异构图
# 创建一个简单的异构图(具有多个类型的节点和边)
graph_data = {
('paper', 'writes', 'author'): (writes_src, writes_tgt), # 写作关系:论文 -> 作者,以源节点和目标节点索引元组形式传入
('paper', 'cites', 'paper'): (cites_src, cites_tgt), # 引用关系:论文 -> 论文
('paper', 'belongs', 'journal'): (belongs_src, belongs_tgt) # 属于关系:论文 -> 期刊
}
hg = dgl.heterograph(graph_data)
# 显示图的结构
print("异构图结构:")
print(hg)