You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spacy保存训练模型遇Permission Error 13权限拒绝问题求助

PermissionError 13 When Saving spaCy Model to Disk (Even After Changing Directories)

Hey, let's work through this permission issue you're hitting when saving your spaCy NER model. I've run into this exact problem before, so here's what's going on and how to fix it.

First, let's recap your setup and the error you're seeing:

Your Training Code

import random
TRAIN_DATA = [
    ("Uber blew through $1 million a week", {'entities': [(0, 4, 'ORG')]}),
    ("Google rebrands its business apps", {'entities': [(0, 6, "ORG")]})
]
nlp = spacy.blank('en')
optimizer = nlp.begin_training()
for i in range(20):
    random.shuffle(TRAIN_DATA)
    for text, annotations in TRAIN_DATA:
        nlp.update([text], [annotations], sgd=optimizer)
nlp.to_disk('/model')

The Error You're Getting

PermissionError Traceback (most recent call last)
in ()
14 for text, annotations in TRAIN_DATA:
15 nlp.update([text], [annotations], sgd=optimizer)
---> 16 nlp.to_disk('/model')
~/anaconda2/envs/py35/lib/python3.5/site-packages/spacy/language.py in to_disk(self, path, disable)
596 serializers[name] = lambda p, proc=proc: proc.to_disk(p, vocab=False)
597 serializers['vocab'] = lambda p: self.vocab.to_disk(p)
--> 598 util.to_disk(path, serializers, {p: False for p in disable})
599
600 def from_disk(self, path, disable=tuple()):
~/anaconda2/envs/py35/lib/python3.5/site-packages/spacy/util.py in to_disk(path, writers, exclude)
508 path = ensure_path(path)
509 if not path.exists():
--> 510 path.mkdir()
511 for key, writer in writers.items():
512 if key not in exclude:
~/anaconda2/envs/py35/lib/python3.5/pathlib.py in mkdir(self, mode, parents, exist_ok)
1214 self._raise_closed()
1215 try:
-> 1216 self._accessor.mkdir(self, mode)
1217 except FileNotFoundError:
1218 if not parents or self.parent == self:
~/anaconda2/envs/py35/lib/python3.5/pathlib.py in wrapped(pathobj, *args)
369 @functools.wraps(strfunc)
370 def wrapped(pathobj, *args):
--> 371 return strfunc(str(pathobj), *args)
372 return staticmethod(wrapped)
373
PermissionError: [Errno 13] Permission denied: '/model'

What's Causing This?

The core issue here is that you're trying to create a folder at /model—that's the root directory of your system. Regular user accounts don't have permission to write to the root directory by default, which is why you're getting the PermissionError. Even if you changed directories, you might have accidentally picked another path that requires admin access, or made a typo (like forgetting a ./ at the start of a relative path).

Fixes to Try

1. Use a Path You Have Access To (Easiest Fix)

Instead of trying to save to the root directory, use a path in your current working directory or your user home folder. These locations are always writable by your user account.

Modify the last line of your code to use a relative path:

# Save to a folder in your current working directory
nlp.to_disk('./my_spacy_model')

Or save to your user home directory (works on macOS/Linux):

nlp.to_disk('~/my_spacy_model')

2. Verify Directory Permissions (If You Need a Specific Path)

If you must save to a specific directory, first check if you have write permissions for it. Open a terminal and run:

ls -ld /path/to/your/target/directory

If you don't have write access, you can change the directory's owner to your user (requires admin password):

sudo chown your_username:your_username /path/to/your/target/directory

Or adjust the directory permissions to allow writing:

sudo chmod 755 /path/to/your/target/directory

3. Double-Check Your Path

Sometimes typos happen—make sure you're not accidentally pointing back to the root directory. You can print your current working directory to confirm where your script is running:

import os
print(os.getcwd())

Use this output to build a correct relative path for your model.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:33:36