完整报错
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失败