看了一篇关于FGSM文献,于是网上找了代码来复现,开心的打开Pycharm,然后将需要的包粘贴进去,运行的时候出现了一句话:
No module named 'torch'
自己仔细一看就发现是缺少了相关的包,然后网上找先关资料,结果就踩下了下面这篇文章的坑,这里我把地址放到下面
win10+Python37环境pip安装pytorch及pycharm中导入pytorch遇到的问题
按照他的方法我个人是没有尝试成功,于是在知乎上看到了一篇文章解决了我的问题,解决办法如下所示:
在Pycharm终端输入下面这句话:
pip install torch===1.2.0 torchvision===0.4.0 -f https://download.pytorch.org/whl/torch_stable.html
然后回车,等待一段时间,安装完成之后出现如下所示就说明你成功了:
可能我自己之前用了其他方法安装了出现如下所示(出现下面这些话不影响我们,我们直接忽略就可以):
Exception:
Traceback (most recent call last):
File "D:\Python\Anaconda3\lib\shutil.py", line 393, in _rmtree_unsafe
os.rmdir(path)
OSError: [WinError 145] 目录不是空的。: 'C:\\Users\\AppData\\Local\\Temp\\pip-install-cx_d5000\\torch\\torch\\lib'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python\Anaconda3\lib\site-packages\pip\_internal\cli\base_command.py", line 143, in main
status = self.run(options, args)
File "D:\Python\Anaconda3\lib\site-packages\pip\_internal\commands\install.py", line 410, in run
wheel_cache.cleanup()
File "D:\Python\Anaconda3\lib\site-packages\pip\_internal\utils\temp_dir.py", line 58, in __exit__
self.cleanup()
File "D:\Python\Anaconda3\lib\site-packages\pip\_internal\utils\temp_dir.py", line 81, in cleanup
rmtree(self.path)
File "D:\Python\Anaconda3\lib\site-packages\pip\_vendor\retrying.py", line 49, in wrapped_f
return Retrying(*dargs, **dkw).call(f, *args, **kw)
File "D:\Python\Anaconda3\lib\site-packages\pip\_vendor\retrying.py", line 212, in call
raise attempt.get()
File "D:\Python\Anaconda3\lib\site-packages\pip\_vendor\retrying.py", line 247, in get
six.reraise(self.value[0], self.value[1], self.value[2])
File "D:\Python\Anaconda3\lib\site-packages\pip\_vendor\six.py", line 693, in reraise
raise value
File "D:\Python\Anaconda3\lib\site-packages\pip\_vendor\retrying.py", line 200, in call
attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
File "D:\Python\Anaconda3\lib\site-packages\pip\_internal\utils\misc.py", line 111, in rmtree
onerror=rmtree_errorhandler)
File "D:\Python\Anaconda3\lib\shutil.py", line 507, in rmtree
return _rmtree_unsafe(path, onerror)
File "D:\Python\Anaconda3\lib\shutil.py", line 386, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "D:\Python\Anaconda3\lib\shutil.py", line 386, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "D:\Python\Anaconda3\lib\shutil.py", line 386, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "D:\Python\Anaconda3\lib\shutil.py", line 395, in _rmtree_unsafe
onerror(os.rmdir, path, sys.exc_info())
File "D:\Python\Anaconda3\lib\site-packages\pip\_internal\utils\misc.py", line 123, in rmtree_errorhandler
func(path)
PS:对了这里补充一句,如果你的Pycharm使用的是3.8版本的这句话可能不适合你,你可以点击File--->Settings--->Project Interpreter
点击+,然后在输入框里面输入:pytorch
等待一下就可以安裝完成,至于安装 torchvision,我们需要再次回到终端安装,输入下面这句话:
pip install torchvision===0.4.0 -f https://download.pytorch.org/whl/torch_stable.html
这里可能会提示版本太低,你可以根据提示将torchvision===0.4.0改为torchvision===0.5.0 (大于0.4.0的都可以)
等待一下就可以安装完成了,亲测有效。