You need to enable JavaScript to run this app. Anaconda运行PyTorch代码下载MNIST时报WinError 5权限拒绝错误
优惠活动
大模型
产品
解决方案
定价
更多

Anaconda运行PyTorch代码下载MNIST时报WinError 5权限拒绝错误

问题概述

Anaconda环境下运行PyTorch实现的对抗神经网络代码时,在执行MNIST测试集加载、创建数据加载器的步骤触发权限类报错,无清晰的报错定位提示。

  • 触发报错的原始实现代码:
# MNIST Test dataset and dataloader declaration
test_loader = torch.utils.data.DataLoader(datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([
            transforms.ToTensor(),])),batch_size=1, shuffle=True)
完整报错堆栈信息
PermissionError                           Traceback (most recent call last)
<ipython-input-4-59310f6a37f8> in <module>
41 
42 # MNIST Test dataset and dataloader declaration
43 test_loader = torch.utils.data.DataLoader(datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([transforms.ToTensor(),])),batch_size=1, shuffle=True)
44 
45 # Define what device we are using

~\anaconda3\lib\site-packages\torchvision\datasets\mnist.py in __init__(self, root, train, transform, target_transform, download)
77 
78         if download:
79             self.download()
80 
81         if not self._check_exists():

~\anaconda3\lib\site-packages\torchvision\datasets\mnist.py in download(self)
138             return
139 
140         os.makedirs(self.raw_folder, exist_ok=True)
141         os.makedirs(self.processed_folder, exist_ok=True)
142 

~\anaconda3\lib\os.py in makedirs(name, mode, exist_ok)
211     if head and tail and not path.exists(head):
212         try:
213             makedirs(head, exist_ok=exist_ok)
214         except FileExistsError:
215             # Defeats race condition when another thread created the path

~\anaconda3\lib\os.py in makedirs(name, mode, exist_ok)
211     if head and tail and not path.exists(head):
212         try:
213             makedirs(head, exist_ok=exist_ok)
214         except FileExistsError:
215             # Defeats race condition when another thread created the path

~\anaconda3\lib\os.py in makedirs(name, mode, exist_ok)
221             return
222     try:
223         mkdir(name, mode)
224     except OSError:
225         # Cannot rely on checking for EEXIST, since the operating system

PermissionError: [WinError 5] Access is denied: '../data' .
报错核心原因

报错为Windows系统权限拦截触发:torchvision在执行MNIST数据集自动下载流程时,需要递归创建../data对应的目录结构,但当前运行代码的Anaconda进程,对该路径指向的上级目录没有写入、新建文件夹的权限,最终触发拒绝访问的报错。

可落地解决步骤
  • 替换数据集存储路径:将代码中MNIST类的第一个传入参数从相对路径'../data',修改为当前Windows用户具备完全读写权限的绝对路径,例如r'C:\Users\你的系统用户名\pytorch_dataset',避免相对路径跨级跳转时匹配到受保护的系统目录。
  • 调整进程运行权限:右键点击Anaconda Prompt或Jupyter Notebook的启动图标,选择以管理员身份启动,再重新运行代码,临时获得目录写入权限。
  • 预处理目录权限:提前在目标存储路径下手动创建data文件夹,检查文件夹属性,取消「只读」权限设置,避免torchvision自动创建目录时触发权限拦截。

内容的提问来源于stack exchange,提问作者Joshua Bessley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.28 01:06:04