Windows Docker - 在python中安装Spacy语言模型返回ImportError。DLL加载失败。找不到指定的模块

问题描述 投票:1回答:1

我正在构建一个基于Windows的Docker镜像来运行Flask应用。为此,我需要安装SpaCy语言模型。但是,我一次又一次地遇到了以下问题,而且到目前为止还没有找到任何强大的解决方案。

运行时。 Windows容器(Docker

错误跟踪。

Step 6/9 : RUN python -m spacy download en_core_web_sm
 ---> Running in 6f8f33207c8f
        Traceback (most recent call last):
      File "C:\Python\lib\runpy.py", line 183, in _run_module_as_main
        mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
      File "C:\Python\lib\runpy.py", line 142, in _get_module_details
        return _get_module_details(pkg_main_name, error)
      File "C:\Python\lib\runpy.py", line 109, in _get_module_details
        __import__(pkg_name)
      File "C:\Python\lib\site-packages\spacy\__init__.py", line 12, in <module>
        from . import pipeline
      File "C:\Python\lib\site-packages\spacy\pipeline\__init__.py", line 4, in <module>
        from .pipes import Tagger, DependencyParser, EntityRecognizer, EntityLinker
      File "pipes.pyx", line 1, in init spacy.pipeline.pipes
    ImportError: DLL load failed: The specified module could not be found.

Dockerfile:

FROM winamd64/python:3.7-windowsservercore
COPY requirements.txt .
COPY models/* ./models/
RUN pip install --no-cache-dir -r requirements.txt
RUN python -m nltk.downloader stopwords
RUN python -m spacy download en_core_web_sm
COPY . .
EXPOSE 5000
CMD python waitress_server.py

requirements.txt:

Flask==1.1.1
future==0.17.1
httplib2==0.13.1
nltk==3.4.5
numpy==1.18.2
pandas
pandocfilters==1.4.2
pickleshare==0.7.5
regex==2019.8.19
requests>=2.13.0
requests-oauthlib
requests-toolbelt
scikit-learn==0.22.1
scipy==1.3.1
simplejson==3.16.0
urllib3==1.24.3
xlrd==1.2.0
zipp==0.6.0
lightgbm
sner
flask-bcrypt
waitress==1.4.4
spacy

waitress_server.py:

print("Hello World")

其他文件是一些训练好的NLP模型。

注意

先谢谢你

python windows docker importerror spacy
1个回答
0
投票

从看 类似问题 在GitHub上,看来这是在本地操作系统上而不是在virtualenv中运行spacy时造成的。推荐的方法是在容器上安装并运行virrualenv中的依赖项。

要做到这一点,你需要通过在docker文件中设置VIRTUAL_ENV和PATH环境变量来手动激活虚拟环境。

# Set up and activate virtual environment
ENV VIRTUAL_ENV "/venv"
RUN python -m venv $VIRTUAL_ENV
ENV PATH "$VIRTUAL_ENV/bin:$PATH"

从。

所以你的docker文件应该是这样的。

FROM winamd64/python:3.7-windowsservercore

ENV VIRTUAL_ENV "/venv"
RUN python -m venv $VIRTUAL_ENV
ENV PATH "$VIRTUAL_ENV/bin:$PATH"

# Set up and activate virtual environment
COPY requirements.txt .
COPY models/* ./models/
RUN pip install --no-cache-dir -r requirements.txt
RUN python -m nltk.downloader stopwords
RUN python -m spacy download en_core_web_sm
COPY . .
EXPOSE 5000
CMD python waitress_server.py

免责声明: 我没有亲自测试过这个,但看到过各种类似的DLL问题,涉及到python包,而在venv中运行是常见的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.