我有一个多级 Docker 映像,如下所示。
# syntax=docker/dockerfile:1
FROM python:3.11 as DEP_BUILDER
ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
POETRY_HOME='/usr/local'
ARG POETRY_VERSION=1.8.3
WORKDIR /venv
RUN apt-get update && apt-get install -y curl
RUN curl -sSL https://install.python-poetry.org | python3 -
COPY pyproject.toml /venv/
RUN python -m venv .venv
RUN . .venv/bin/activate && \
poetry config virtualenvs.in-project true && \
poetry install --no-interaction --no-ansi --with dev
FROM python:3.11
WORKDIR /server
COPY /app /server/app
COPY /tests /server/tests
COPY --from=DEP_BUILDER /venv/.venv ./.venv
ENV PATH="/server/.venv/bin:$PATH"
CMD ./.venv/bin/python3 -m pytest tests
虽然在我的本地计算机上运行
pytest
工作正常,但在 Docker 中执行时出现以下错误:
Attaching to app-1
app-1 | ImportError while loading conftest '/server/tests/conftest.py'.
app-1 | tests/conftest.py:10: in <module>
app-1 | from test.support.os_helper import EnvironmentVarGuard
app-1 | E ModuleNotFoundError: No module named 'test'
app-1 exited with code 4
有人能好心解释一下为什么从图像内部无法访问一个应该是Python内置的模块吗?
非常感谢
你能检查一下这里是否应该进行测试吗? 从“test”.support.os_helper导入EnvironmentVarGuard
或 你可以尝试包括这一行吗
RUN apt-get update && apt-get install -y python3-testresources
在此之前
FROM python:3.11
WORKDIR /server
COPY /app /server/app
COPY /tests /server/tests
COPY --from=DEP_BUILDER /venv/.venv ./.venv
ENV PATH="/server/.venv/bin:$PATH"
CMD ./.venv/bin/python3 -m pytest tests