我在 Ubuntu 16.04 上工作,使用 jupyter 笔记本。我刚刚使用以下命令安装了最新版本的 spaCy,因为我的英语模块没有下载
conda install -c conda-forge spacy=2.0.11
但是,在使用上述命令安装 spaCy 时,它说:以下软件包将被删除:anaconda:5.2.0-py36_3
通过以下方式加载英语模块:
import spacy nlp = spacy.load('en')
我得到以下信息:
KeyError Traceback (most recent call last) <ipython-input-15-76abff010c5f> in <module>() ----> 1 nlp = spacy.load('en') ~/anaconda3/lib/python3.6/site-packages/spacy/__init__.py in load(name, vocab, tokenizer, parser, tagger, entity, matcher, serializer, vectors, via) 13 if depr_path not in (True, False, None): 14 deprecation_warning(Warnings.W001.format(path=depr_path)) ---> 15 return util.load_model(name, **overrides) 16 17 ~/anaconda3/lib/python3.6/site-packages/spacy/util.py in get_package_by_name(name, via) 42 """Import and load a Language class. 43 ---> 44 lang (unicode): Two-letter language code, e.g. 'en'. 45 RETURNS (Language): Language class. 46 """ ~/anaconda3/lib/python3.6/site-packages/sputnik/__init__.py in package(app_name, app_version, package_string, data_path) 157 data_path = default_data_path(app_name) 158 --> 159 pool = Pool(app_name, app_version, expand_path(data_path)) 160 return pool.get(package_string) 161 ~/anaconda3/lib/python3.6/site-packages/sputnik/pool.py in __init__(self, app_name, app_version, path, **kwargs) 17 18 def __init__(self, app_name, app_version, path, **kwargs): ---> 19 super(Pool, self).__init__(app_name, app_version, path, **kwargs) 20 self.logger = logging.getLogger(__name__) 21 self.cleanup() ~/anaconda3/lib/python3.6/site-packages/sputnik/package_list.py in __init__(self, app_name, app_version, path, **kwargs) 31 raise InvalidDataPathException(self.data_path) 32 ---> 33 self.load() 34 35 def packages(self): ~/anaconda3/lib/python3.6/site-packages/sputnik/package_list.py in load(self) 49 def load(self): 50 self._packages = {} ---> 51 for package in self.packages(): 52 self._packages[package.ident] = package 53 ~/anaconda3/lib/python3.6/site-packages/sputnik/package_list.py in packages(self) 45 continue 46 ---> 47 yield self.__class__.package_class(path=os.path.join(self.path, path)) 48 49 def load(self): ~/anaconda3/lib/python3.6/site-packages/sputnik/package.py in __init__(self, path) 13 def __init__(self, path): 14 meta = util.json_load(os.path.join(path, default.META_FILENAME)) ---> 15 super(Package, self).__init__(defaults=meta['package']) 16 17 self.logger = logging.getLogger(__name__) KeyError: 'package'
请帮忙!
你的问题主要有两部分:
spaCy
anaconda
KeyError: 'package'
以下是逐步解决问题的办法:
conda install 命令可能破坏了你的 anaconda 环境。以下是修复的步骤: 1. 重新安装 Anaconda: - 下载最新版本的 Anaconda:Anaconda官网 - 重新安装后,将环境初始化: bash conda init
conda install
bash conda init
bash conda create -n spacy_env python=3.6 conda activate spacy_env
安装 spaCy: 确保你在新的环境中安装 spaCy: bash conda install -c conda-forge spacy
bash conda install -c conda-forge spacy
安装英语语言模型: spaCy 2.x 使用单独的命令下载语言模型。执行以下命令安装英语模型: bash python -m spacy download en_core_web_sm
bash python -m spacy download en_core_web_sm
加载语言模型: 确保使用正确的模型名称加载: python import spacy nlp = spacy.load('en_core_web_sm')
python import spacy nlp = spacy.load('en_core_web_sm')
验证 spaCy 是否正确安装: bash python -c "import spacy; print(spacy.__version__)"
bash python -c "import spacy; print(spacy.__version__)"
验证模型是否加载: python import spacy nlp = spacy.load('en_core_web_sm') print("Model loaded successfully!")
python import spacy nlp = spacy.load('en_core_web_sm') print("Model loaded successfully!")
en_core_web_sm
更新至最新的 spaCy 后,这个问题应该解决。
Anaconda 包冲突:
bash conda install -c conda-forge spacy --no-update-deps
尝试以上步骤,应该能解决你的问题。如果仍有问题,可以提供更多日志信息,我会进一步协助!