python spacy TypeError: unpackb() got an unexpected keyword argument 'raw'

python spacy TypeError: unpackb() got an unexpected keyword argument 'raw'

我在 jupyter notebook 中使用 spacy 进行自然语言处理,代码如下。

# Import spacy
import spacy

# Instantiate the English model: nlp
nlp = spacy.load('en')

出现以下错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-3370ec3212e0> in <module>()
      3 
      4 # Instantiate the English model: nlp
----> 5 nlp = spacy.load('en')
      6 

~\Anaconda3\lib\site-packages\spacy\__init__.py in load(name, **overrides)
     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\site-packages\spacy\util.py in load_model(name, **overrides)
    110     if isinstance(name, basestring_):  # in data dir / shortcut
    111         if name in set([d.name for d in data_path.iterdir()]):
--> 112             return load_model_from_link(name, **overrides)
    113         if is_package(name):  # installed as package
    114             return load_model_from_package(name, **overrides)

~\Anaconda3\lib\site-packages\spacy\util.py in load_model_from_link(name, **overrides)
    127     except AttributeError:
    128         raise IOError(Errors.E051.format(name=name))
--> 129     return cls.load(**overrides)
    130 
    131 

~\Anaconda3\lib\site-packages\spacy\data\en\__init__.py in load(**overrides)
     10 
     11 def load(**overrides):
---> 12     return load_model_from_init_py(__file__, **overrides)

~\Anaconda3\lib\site-packages\spacy\util.py in load_model_from_init_py(init_file, **overrides)
    171     if not model_path.exists():
    172         raise IOError(Errors.E052.format(path=path2str(data_path)))
--> 173     return load_model_from_path(data_path, meta, **overrides)
    174 
    175 

~\Anaconda3\lib\site-packages\spacy\util.py in load_model_from_path(model_path, meta, **overrides)
    154             component = nlp.create_pipe(name, config=config)
    155             nlp.add_pipe(component, name=name)
--> 156     return nlp.from_disk(model_path)
    157 
    158 

~\Anaconda3\lib\site-packages\spacy\language.py in from_disk(self, path, disable)
    651         if not (path / 'vocab').exists():
    652             exclude['vocab'] = True
--> 653         util.from_disk(path, deserializers, exclude)
    654         self._path = path
    655         return self

~\Anaconda3\lib\site-packages\spacy\util.py in from_disk(path, readers, exclude)
    509     for key, reader in readers.items():
    510         if key not in exclude:
--> 511             reader(path / key)
    512     return path
    513 

~\Anaconda3\lib\site-packages\spacy\language.py in <lambda>(p)
    640             ('vocab', lambda p: (
    641                 self.vocab.from_disk(p) and _fix_pretrained_vectors_name(self))),
--> 642             ('tokenizer', lambda p: self.tokenizer.from_disk(p, vocab=False)),
    643         ))
    644         for name, proc in self.pipeline:

tokenizer.pyx in spacy.tokenizer.Tokenizer.from_disk()

tokenizer.pyx in spacy.tokenizer.Tokenizer.from_bytes()

~\Anaconda3\lib\site-packages\spacy\util.py in from_bytes(bytes_data, setters, exclude)
    488 
    489 def from_bytes(bytes_data, setters, exclude):
--> 490     msg = msgpack.loads(bytes_data, raw=False)
    491     for key, setter in setters.items():
    492         if key not in exclude and key in msg:

~\Anaconda3\lib\site-packages\msgpack_numpy.py in unpackb(packed, **kwargs)
    182     object_hook = kwargs.get('object_hook')
    183     kwargs['object_hook'] = functools.partial(decode, chain=object_hook)
--> 184     return _unpackb(packed, **kwargs)
    185 
    186 load = unpack

msgpack/_unpacker.pyx in msgpack._unpacker.unpackb()

TypeError: unpackb() got an unexpected keyword argument 'raw'

总结起来就是

TypeError: unpackb() got an unexpected keyword argument 'raw'

请注意: 我已经以管理员 python -m spacy download en 的身份在 anaconda 提示符下正确安装 link spacy.load('en')。 这是 anaconda 提示中的结果

(base) C:\WINDOWS\system32>python -m spacy download en
Requirement already satisfied: en_core_web_sm==2.0.0 from https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.0.0/en_core_web_sm-2.0.0.tar.gz#egg=en_core_web_sm==2.0.0 in c:\users\winsu\anaconda3\lib\site-packages (2.0.0)

    Linking successful
    C:\Users\winsu\Anaconda3\lib\site-packages\en_core_web_sm -->
    C:\Users\winsu\Anaconda3\lib\site-packages\spacy\data\en

    You can now load the model via spacy.load('en')
pip uninstall msgpack-python
pip install msgpack

请注意:我的提示恰好强调了 "regex version(2018.7.11) is incompatible with spacy which wants regex==2017.4.5" 但是我的 spacy 现在仍然可以正常工作!

顺便说一句,非常感谢@Ines Montani

我在 Windows 盒子上使用 Conda Python 3。安装 spacy 安装了 msgpack:

Local\Continuum\anaconda3>conda list
# packages in environment at C:\Users\akmm94\AppData\Local\Continuum\anaconda3:
#
# Name                    Version                   Build  Channel
msgpack-python            0.5.1            py36he980bc4_0

我在执行以下操作时遇到了同样的错误:

import spacy
nlp = spacy.load('en')

错误:

TypeError: unpackb() got an unexpected keyword argument 'raw'

修复:重新安装 msgpack-python 使用:

conda install -c anaconda msgpack-python

此更新的 msgpack-python:

msgpack-python:  0.5.1-py36he980bc4_0             --> 0.5.6-py36he980bc4_1 anaconda

错误得到修复!