cannot import name ‘container_abcs‘ from ‘torch._six‘错误的解决方法

在升级PyTorch 1.9以后用一些老版本的开源代码的时候会出现这种问题。与此类似的还有:

cannot import name ‘container_abcs‘ from ‘torch._six‘
cannot import name ‘int_classes‘ from ‘torch._six‘

解决方法是定位到相应的import的位置,然后把原来的import改成:

# from torch._six import container_abcs as container_abcs
# from torch._six import int_classes as int_classes
TORCH_MAJOR = int(torch.__version__.split('.')[0])
TORCH_MINOR = int(torch.__version__.split('.')[1])
if TORCH_MAJOR == 1 and TORCH_MINOR < 8:
    from torch._six import container_abcs
    from torch._six import int_classes as int_classes
else:
    import collections.abc as container_abcs
    int_classes = int

DIG包,报错环境信息:

torch 1.11.0
torchvision 0.12.0
torch-sparse 0.6.13
torch-scatter 2.0.9
torch-geometric 1.7.0
Built with Hugo
Theme Stack designed by Jimmy