为什么我在 SSH 服务器上安装 SpaCy en_core_web_sm 时遇到 PermissionError

Why am I facing PermissionError while installing SpaCy en_core_web_sm on SSH server

我正在做一个项目,并将我的环境从本地 windows 转移到 linux 服务器(通过 SSH)。我只有有限的访问权限,因为主机服务器来自我的大学,我已经安装了许多没有问题的软件包(有和没有 virtualenv)。我正在开发 Python 3.6.9.

我能够安装 spacy 并导入它,但我需要使用必须使用命令 python3 -m spacy download en_core_web_sm 额外安装的 en_core_web_sm 包。但是,我一直面临 PermissionError,如下面的日志所示。

为什么我会遇到这个错误?是不是因为我没有 /usr 级别的管理员访问权限(请参阅日志的最后一行)?如果是,为什么只有这个包特别需要更高级别的访问权限?如果没有,我是否有任何解决方法来安装该软件包,或者我是否需要联系服务器管理员?

jiayi@cdas1:~/adverse_drug_event_extraction/app/preprocessing$ python3 -m spacy download en_core_web_sm
Collecting en-core-web-sm==3.1.0 from https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.1.0/en_core_web_sm-3.1.0-py3-none-any.whl#egg=en_core_web_sm==3.1.0
  Downloading https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.1.0/en_core_web_sm-3.1.0-py3-none-any.whl (13.6MB)
    100% |████████████████████████████████| 13.6MB 117kB/s 
Requirement already satisfied: spacy<3.2.0,>=3.1.0 in /home/jiayi/.local/lib/python3.6/site-packages (from en-core-web-sm==3.1.0)
Requirement already satisfied: pathy>=0.3.5 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: blis<0.8.0,>=0.4.0 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: typer<0.4.0,>=0.3.0 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: jinja2 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: srsly<3.0.0,>=2.4.1 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: murmurhash<1.1.0,>=0.28.0 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: spacy-legacy<3.1.0,>=3.0.7 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: requests<3.0.0,>=2.13.0 in /usr/lib/python3/dist-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: pydantic!=1.8,!=1.8.1,<1.9.0,>=1.7.4 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: setuptools in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: catalogue<2.1.0,>=2.0.4 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: cymem<2.1.0,>=2.0.2 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: packaging>=20.0 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: tqdm<5.0.0,>=4.38.0 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: typing-extensions<4.0.0.0,>=3.7.4; python_version < "3.8" in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: preshed<3.1.0,>=3.0.2 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: wasabi<1.1.0,>=0.8.1 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: thinc<8.1.0,>=8.0.8 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: numpy>=1.15.0 in /home/jiayi/.local/lib/python3.6/site-packages (from spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: smart-open<6.0.0,>=5.0.0 in /home/jiayi/.local/lib/python3.6/site-packages (from pathy>=0.3.5->spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: dataclasses<1.0,>=0.6; python_version < "3.7" in /home/jiayi/.local/lib/python3.6/site-packages (from pathy>=0.3.5->spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: click<7.2.0,>=7.1.1 in /home/jiayi/.local/lib/python3.6/site-packages (from typer<0.4.0,>=0.3.0->spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: MarkupSafe>=2.0 in /home/jiayi/.local/lib/python3.6/site-packages (from jinja2->spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: zipp>=0.5; python_version < "3.8" in /home/jiayi/.local/lib/python3.6/site-packages (from catalogue<2.1.0,>=2.0.4->spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: pyparsing>=2.0.2 in /home/jiayi/.local/lib/python3.6/site-packages (from packaging>=20.0->spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: contextvars<3,>=2.4; python_version < "3.7" in /home/jiayi/.local/lib/python3.6/site-packages (from thinc<8.1.0,>=8.0.8->spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Requirement already satisfied: immutables>=0.9 in /home/jiayi/.local/lib/python3.6/site-packages (from contextvars<3,>=2.4; python_version < "3.7"->thinc<8.1.0,>=8.0.8->spacy<3.2.0,>=3.1.0->en-core-web-sm==3.1.0)
Installing collected packages: en-core-web-sm
Exception:
Traceback (most recent call last):
  File "/home/jiayi/.local/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/home/jiayi/.local/lib/python3.6/site-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/home/jiayi/.local/lib/python3.6/site-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/home/jiayi/.local/lib/python3.6/site-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/home/jiayi/.local/lib/python3.6/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/home/jiayi/.local/lib/python3.6/site-packages/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/home/jiayi/.local/lib/python3.6/site-packages/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/home/jiayi/.local/lib/python3.6/site-packages/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/usr/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/dist-packages/en_core_web_sm-3.1.0.dist-info'

似乎所有其他软件包都安装在您的用户 /home/jiayi/.local/python3.6/lib 下,而这个软件包试图在 /usr/local/lib/python3.6/ 中全局安装自己,不知道为什么。我想你可以给它安装文件夹什么的。 看这里Where does spacy language model download?

如您所料,发生这种情况是因为您没有安装到 /usr 的权限。通常 pip 会安装在那里,但看起来你的 pip 基本上是 运行 --user,它将安装到你的用户主目录。

spaCy模型不能直接通过pip安装,因为它们是大数据文件,所以不能像普通代码一样托管在PyPI上。一个不幸的副作用是围绕 pip 配置的一些选项被忽略了。

您可以通过将额外的参数附加到您的命令来将额外的参数传递给 pip install 命令,因此在您的情况下您可以这样做:

python3 -m spacy download en_core_web_sm --user

一切都应该正常。

也就是说,我强烈建议您学习如何使用 virtualenv 工具使用虚拟环境,这将使 Python 项目的工作变得更容易,并让您避免这个问题,因为任何事情您的安装只会进入本地 virtualenv 而不是全局 pip 安装。