我正在构建一个基于 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
要求.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
女服务员.py:
print("Hello World")
其他文件是一些经过训练的 NLP 模型。
注:
提前致谢!
最佳答案
从看similar issues在 GitHub 上,看起来这是在 native 操作系统上而不是在 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"
from: 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
免责声明:我自己没有对此进行过测试,但看到了各种类似的涉及 python 包的 DLL 问题,并且在 venv 中运行是常见的解决方案。
关于python - Windows Docker - 在 python 中安装 Spacy 语言模型返回 ImportError : DLL load failed: The specified module could not be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62167080/