环境配置要是出现兼容问题还是挺繁琐的。所以这里记录下成功的配置情况。
conda create --name Test python=3.9 # 构建一个虚拟环境
conda init bash && source /root/.bashrc # 更新bashrc中的环境变量
conda activate Test # 切换到该虚拟环境
pip install torch==2.2.0 torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 # 安装torch
conda install -c dglteam/label/cu118 dgl # 安装dgl, torch 2.2.0和cu118的dgl兼容
如果conda报错了
你可以修改源,也可以换成pip安装
pip install dgl -f https://data.dgl.ai/wheels/cu118/repo.html
pip install dglgo -f https://data.dgl.ai/wheels-test/repo.html
如果报错ClobberError: The package ‘defaults/linux-64::numpy-base-1.26.4-py39hb5e798b_0’ cannot be installed due to a
path collision for ‘lib/python3.9/site-packages/numpy/typing/tests/data/reveal/ufuncs.pyi’.
This path already exists in the target prefix, and it won’t be removed
by an uninstall action in this transaction. The path is one that conda
doesn’t recognize. It may have been created by another package manager.
尝试运行conda clean --all
,然后再运行安装命令
测试代码test.py
# 测试环境安装结果及版本
import dgl
import torch
if torch.cuda.is_available():
print("CUDA is available. GPU support is enabled.")
print("Number of GPUs available:", torch.cuda.device_count())
for i in range(torch.cuda.device_count()):
print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
# 显示PyTorch使用的CUDA版本
print("CUDA Version:", torch.version.cuda)
# 检查cuDNN是否启用
print("cuDNN enabled:", torch.backends.cudnn.enabled)
# 打印cuDNN版本
print("cuDNN version:", torch.backends.cudnn.version())
else:
print("CUDA is not available. GPU support is not enabled.")
print("PyTorch Version:", torch.__version__)
print("DGL Version:", dgl.__version__)
安装torch-scatter、 torch-cluster、torch-sparse,可能你要想像我一样用到torch稀疏矩阵、随机游走之类的,如果你的torch和cuda版本不一样,直接把后面的网址对应部分改了就行,这里我还是顺着上面的,上面的torch版本就是2.2.0,cuda版本是11.8,对应下面的cu118。
pip install --no-index torch-scatter -f https://pytorch-geometric.com/whl/torch-2.2.0+cu118.html
pip install --no-index torch-cluster -f https://pytorch-geometric.com/whl/torch-2.2.0+cu118.html
pip install --no-index torch-sparse -f https://pytorch-geometric.com/whl/torch-2.2.0+cu118.html
报错:No module named ‘torch_geometric
解决方法:接着上面的torch和cuda版本,先安装下面四个前置包:
pip install --no-index torch-scatter -f https://pytorch-geometric.com/whl/torch-2.2.0+cu118.html
pip install --no-index torch-cluster -f https://pytorch-geometric.com/whl/torch-2.2.0+cu118.html
pip install --no-index torch-sparse -f https://pytorch-geometric.com/whl/torch-2.2.0+cu118.html
pip install --no-index torch-spline-conv -f https://pytorch-geometric.com/whl/torch-2.2.0+cu118.html
然后再安装torch_geometric即可:
pip install torch-geometric
补充一下,我自己安包的时候发现经常会pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
就是读超时,那我建议直接换源。
换源安包代码如下:
pip install torch-geometric -i https://pypi.tuna.tsinghua.edu.cn/simple