spacy下载en_core_web_lg, en_core_web_sm等,出现ConnectionError下载失败的情况的解决方法

文章目录

  • 报错信息
  • 原因及解决方法
  • 参考文献

报错信息

raceback (most recent call last):
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/urllib3/connectionpool.py", line 672, in urlopen
    chunked=chunked,
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/urllib3/connectionpool.py", line 376, in _make_request
    self._validate_conn(conn)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/urllib3/connectionpool.py", line 994, in _validate_conn
    conn.connect()
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/urllib3/connection.py", line 300, in connect
    conn = self._new_conn()
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/urllib3/connection.py", line 169, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f6efabdf810>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/urllib3/connectionpool.py", line 720, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/urllib3/util/retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='raw.githubusercontent.com', port=443): Max retries exceeded with url: /explosion/spacy-models/master/shortcuts-v2.json (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/spacy/__main__.py", line 31, in <module>
    plac.call(commands[command], sys.argv[1:])
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/plac_core.py", line 328, in call
    cmd, result = parser.consume(arglist)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/plac_core.py", line 207, in consume
    return cmd, self.func(*(args + varargs + extraopts), **kwargs)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/spacy/cli/download.py", line 31, in download
    shortcuts = get_json(about.__shortcuts__, "available shortcuts")
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/spacy/cli/download.py", line 54, in get_json
    r = requests.get(url)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/home/share/liuyibing/Anaconda3/envs/py3_torch_v1.4/lib/python3.7/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='raw.githubusercontent.com', port=443): Max retries exceeded with url: /explosion/spacy-models/master/shortcuts-v2.json (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))

原因及解决方法

因为无法连接到model的链接,所以我们最根本的解决途径就是预下载好需要的en_core_web_lg等模型,然后让spacy load即可

  • 一种方式,这里可以参考这篇博客:python -m spacy download en 提示服务器连接一直失败安装不上的解决方式。

  • 不过上面的方式略显麻烦,目前anaconda已经提供了spacy model的下载,所以我们只需要:

conda install -c conda-forge spacy-model-en_core_web_lg
或者运行conda install -c conda-forge/label/cf202003 spacy-model-en_core_web_lg

注:上面的en_core_web_lg可以替换为en_core_web_sm、en_core_web_md均可。关于这三种模型的区别,可以参考我的这篇博客

另外如果出现KeyError: ‘PUNCTSIDE_FIN’,可以参考load模型时出现KeyError: 'PUNCTSIDE_FIN’的情况的解决方法

参考文献

  • https://anaconda.org/conda-forge/spacy-model-en_core_web_lg
  • https://blog.csdn.net/WZY31014332886/article/details/104605887/

你可能感兴趣的:(nlp)