一、问题

本来windows+pycharm+gpu版本pytorch好好的,结果不知道用pip装了什么奇怪的包或者做了什么奇怪操作(感觉很大概率是重装pytorch时出错,用的是pytorch官网的指令,

conda install pytorch torchvision torchaudio pytorch-cuda=10.2 -c pytorch -c nvidia

,也有可能是我用pip install安装下载好的.whl文件时出错),导致import torch出错,具体报错的描述可以参考下述链接,但我真正问题出错处与其不同:

caffe2_detectron_ops_gpu.dll makes import torch occur OSerror, [WinError 193] %1 is not a valid Win32 application · Issue #56884 · pytorch/pytorch · GitHub🐛 Bug To Reproduce Steps to reproduce the behavior: create conda virtual environment install pytorch via conda : "conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge" open jupyter notebook & "import to...https://github.com/pytorch/pytorch/issues/56884

二、原因

具体看报错的输出(因为已经删除所以无法复现,当时也没有截图保存,以后一定要及时记录),发现此处import torch为系统自身的python3.8中的pytorch(我明明是在conda activate的虚拟环境中进行的操作Import torch),之前重装的pytorch也被装进了里面,具体路径为C:\Users\HP\AppData\Roaming\Python3.8,然后上网搜pip install装到系统的python环境中怎么办,发现了这篇链接,解决方法与原因与我一致:

conda虚拟环境路径包含系统python路径问题_conda python路径_幸福回头的博客-CSDN博客趁着618阿里云搞活动,买了一个阿里云最低配版本的ECS服务器,想着给一个内部的服务器做一个内网穿透,这样可以在任何地方连接到服务器进行操作。、_conda python路径https://blog.csdn.net/zt1091574181/article/details/117748084

三、操作

1.首先激活虚拟环境,用指令python -m site -help找到site.py的具体路径,如图:

2.用pycharm打开site.py,修改USER_SITE,如图:

 

 3. 再次输入指令python -m site,可以看到路径中已经没有系统自身的python路径,如图:

或许USER_BASE也可以一起改了?

 4. 再次Import torch,这下没问题啦~

 四、感想

感觉windows配环境经常出两个问题,一个是用户权限问题,一个是路径与环境变量问题,呜呜呜,真浪费时间~

希望能帮助到大家!

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐