安装DGL注意事项
一、首先得确定自己电脑或者服务器环境安装的 python 和 cuda 版本是多少,然后选择对应的dgl进行安装,而不是直接使用下面的代码直接进行安装,否则会报错。
pip install dgl # 这种直接安装方式不妥
DGL backend not selected or invalid. Assuming PyTorch for now.
Setting the default backend to "pytorch". You can change it in the ~/.dgl/config.json file or export the DGLBACKEND environment variable. Valid options are: pytorch, mxnet, tensorflow (all lowercase)
Traceback (most recent call last):File "test.py", line 5, in <module>import GCL.losses as LFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/__init__.py", line 2, in <module>import GCL.augmentorsFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/__init__.py", line 3, in <module>from .rw_sampling import RWSamplingFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/rw_sampling.py", line 2, in <module>from GCL.augmentors.functional import random_walk_subgraphFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/functional.py", line 6, in <module>from GCL.utils import normalizeFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/utils.py", line 4, in <module>import dglFile "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/__init__.py", line 14, in <module>from .backend import backend_name, load_backend # usort: skipFile "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/backend/__init__.py", line 122, in <module>load_backend(get_preferred_backend())File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/backend/__init__.py", line 57, in load_backendmod = importlib.import_module(".%s" % mod_name, __name__)File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/importlib/__init__.py", line 127, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/backend/pytorch/__init__.py", line 2, in <module>from .tensor import *File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/backend/pytorch/tensor.py", line 16, in <module>raise RuntimeError("DGL requires PyTorch >= 1.13.0")
RuntimeError: DGL requires PyTorch >= 1.13.0
二、点击下面链接打开选择合适的dgl版本安装文件进行下载,选择方式举例:比如博主服务器的运行环境是cuda12(对应cu113),然后python是3.8(对应cp38),所以可以选择dgl_cu113-0.9.1-cp38-cp38-manylinux1_x86_64.whl文件进行下载。
dgl各版本安装文件:点击打开链接
三、然后将下载好的dgl安装文件进行安装。
pip install dgl_cu113-0.9.1-cp38-cp38-manylinux1_x86_64.whl
四(另简安装)、直接复制粘贴下面的安装命令进行安装,可以省略上面两步直接一步安装dgl对应版本,但是也需要根据自己电脑或者服务器 python 和 cuda 版本选择好dgl版本,博主举例是dgl_cu113-0.9.1-cp38-cp38-manylinux1_x86_64.whl,相当于下面代码中的dgl_cu113,所以不同dgl版本改动“dgl_cu113”即可。
pip install dgl-cu113 -f https://data.dgl.ai/wheels/repo.html
五、安装可能出现的问题
(1)就是安装了两个版本的dgl,然后就会出现文件指向错误,如:“FileNotFoundError: Cannot find DGL C++ sparse library at /mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/dgl_sparse/libdgl_sparse_pytorch_1.12.1.so
”。
Traceback (most recent call last):File "test.py", line 5, in <module>import GCL.losses as LFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/__init__.py", line 2, in <module>import GCL.augmentorsFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/__init__.py", line 3, in <module>from .rw_sampling import RWSamplingFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/rw_sampling.py", line 2, in <module>from GCL.augmentors.functional import random_walk_subgraphFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/functional.py", line 6, in <module>from GCL.utils import normalizeFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/utils.py", line 4, in <module>import dglFile "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/__init__.py", line 16, in <module>from .backend import load_backend, backend_nameFile "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/backend/__init__.py", line 109, in <module>load_backend(get_preferred_backend())File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/backend/__init__.py", line 48, in load_backendmod = importlib.import_module('.%s' % mod_name, __name__)File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/importlib/__init__.py", line 127, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/backend/pytorch/__init__.py", line 2, in <module>from .sparse import *File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/backend/pytorch/sparse.py", line 4, in <module>from ...sparse import _gspmm, _gspmm_hetero, _gsddmm, _gsddmm_hetero, _segment_reduce, _bwd_segment_cmpFile "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/sparse/__init__.py", line 43, in <module>load_dgl_sparse()File "/mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/sparse/__init__.py", line 35, in load_dgl_sparseraise FileNotFoundError(f"Cannot find DGL C++ sparse library at {path}")
FileNotFoundError: Cannot find DGL C++ sparse library at /mnt/sdb/xier21/anaconda3/envs/nlp/lib/python3.8/site-packages/dgl/dgl_sparse/libdgl_sparse_pytorch_1.12.1.so
(2)那么有的朋友就想着那我直接用下面的命令(注意卸载得指向对应的dgl版本,否则卸载无效,可用conda list
查看)卸载一个dgl不就可以了,其实也不可以,因为两个版本的dgl导致文件之间的指向互相紊乱,所以两个都得卸载。否则会依旧出现文件指向混乱找不到对应的包问题,如AttributeError: module 'dgl' has no attribute 'DGLGraph'
。
pip uninstall dgl==1.1.3
Traceback (most recent call last):File "test.py", line 5, in <module>import GCL.losses as LFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/__init__.py", line 2, in <module>import GCL.augmentorsFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/__init__.py", line 3, in <module>from .rw_sampling import RWSamplingFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/rw_sampling.py", line 2, in <module>from GCL.augmentors.functional import random_walk_subgraphFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/functional.py", line 6, in <module>from GCL.utils import normalizeFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/utils.py", line 60, in <module>def build_dgl_graph(edge_index: torch.Tensor) -> dgl.DGLGraph:
AttributeError: module 'dgl' has no attribute 'DGLGraph'
(nlp) xier21@UVM:~/PyGCL-main/examples$ ^C
(nlp) xier21@UVM:~/PyGCL-main/examples$ python test.py
Traceback (most recent call last):File "test.py", line 9, in <module>from GCL.eval import get_split, SVMEvaluatorFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/__init__.py", line 2, in <module>import GCL.augmentorsFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/__init__.py", line 3, in <module>from .rw_sampling import RWSamplingFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/rw_sampling.py", line 2, in <module>from GCL.augmentors.functional import random_walk_subgraphFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/augmentors/functional.py", line 6, in <module>from GCL.utils import normalizeFile "/mnt/sdb/xier21/PyGCL-main/examples/../GCL/utils.py", line 60, in <module>def build_dgl_graph(edge_index: torch.Tensor) -> dgl.DGLGraph:
AttributeError: module 'dgl' has no attribute 'DGLGraph'
(3)因此需要再将之前安装剩下的dgl版本卸载。
pip uninstall dgl-cu113
(4)最后再重新安装一个你所需要的dgl版本,然后就基本不会出问题了。