[Python] How to add Alpine scikit-learn to poetry environment on Alpine Docker container?

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have to use an python alpine docker image for my python project. I use poetry for dependency management, and I need to add the scikit-learn package.

    The scikit-learn within poetry is not working on alpine linux images, so I figured out I have to use the official alpine sklearn package: py3-scikit-learn . The install goes through successfully thanks to https://stackoverflow.com/a/76184400/18975804

    But I'm using poetry for the other packages, and when runnig the scripts with poetry, the sklearn package is not recognized: ModuleNotFoundError: No module named 'sklearn'

    All of the other modules are recognized. How can I get the alpine-sklearn recognized in the poetry environment?

    FYI: IT'S NOT AN OPTION TO USE OTHER LINUX DISTRIBUTION in the DOCKER container.

    Dockerfile:

    FROM python:3.13.2-alpine3.21
    RUN apk --update add gcc build-base freetype-dev libpng-dev openblas-dev py3-scikit-learn
    RUN pip install --upgrade pip
    RUN pip install poetry==2.1.1

    ENV POETRY_NO_INTERACTION=1 \
    POETRY_VIRTUALENVS_IN_PROJECT=1 \
    POETRY_VIRTUALENVS_CREATE=1 \
    POETRY_CACHE_DIR=/tmp/poetry_cache

    WORKDIR /app

    COPY pyproject.toml poetry.lock ./

    RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR

    COPY app ./app

    WORKDIR ./app

    ENTRYPOINT ["poetry", "run", "python", "main.py"]


    And I use import sklearn in my main.py python script.

    Continue reading...

Compartilhe esta Página