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

时间:2020-06-03 07:04:54

标签: python windows docker importerror spacy

我正在构建一个基于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模型。

注意:

预先感谢!

1 个答案:

答案 0 :(得分:0)

从GitHub上的similar issues看,这似乎是在本机OS而非virtualenv中运行spacy时引起的。推荐的方法是从容器上的vitrualenv安装并运行依赖项。

为此,您需要通过在dockerfile中设置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"

来自:source

所以您的dockerfile应该看起来像这样:

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中运行是常见的解决方案。