完整报错

ImportError: Failed to load PyTorch C extensions:
    It appears that PyTorch has loaded the `torch/_C` folder
    of the PyTorch repository rather than the C extensions which
    are expected in the `torch._C` namespace. This can occur when
    using the `install` workflow. e.g.
        $ python setup.py install && python -c "import torch"

    This error can generally be solved using the `develop` workflow
        $ python setup.py develop && python -c "import torch"  # This should succeed
    or by running Python from a different directory.

解决过程

出错可能性

在这里插入图片描述

自检pytorch有没有安装好

检测方法参考这篇大佬的文章(请自行检查自己的cuda版本,我的cuda版本是12.3)
检查下来发现一切正常
在这里插入图片描述

import torch
print("Is CUDA available:", torch.cuda.is_available())
print("CUDA version:", torch.version.cuda)

解决问题

发现用的解释器环境路径和自检时的python版本不一样,之前用的环境是“lunwen(2)”,python版本是3.10
在这里插入图片描述
而刚才自检的时候python版本是3.12
在这里插入图片描述
这个时候才意识到此时的python.exe路径错了
重新加载回正确的conda路径后,报错解决
在这里插入图片描述

问题总结

这次报错不是因为pytorch没安装好,也不是因为安装版本不对,而是使用的python.exe路径不对,导致import torch失败

Logo

2万人民币佣金等你来拿,中德社区发起者X.Lab,联合德国优秀企业对接开发项目,领取项目得佣金!!!

更多推荐