[Python] Error importing cv2 "ModuleNotFoundError: No module named 'cv2' in Docker image

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

  1. Stack

    Stack Membro Participativo

    I tried to build docker image of python application with this dockerfile:

    FROM python:3.10-slim

    WORKDIR /app

    # Install system dependencies Git
    RUN apt-get update && apt-get install -y --no-install-recommends git curl unzip \
    && rm -rf /var/lib/apt/lists/* \
    && curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
    && unzip awscliv2.zip \
    && ./aws/install \
    && rm -rf aws awscliv2.zip

    # Copy and install Python dependencies
    COPY sai/requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt \
    && pip uninstall -y opencv-python \
    && pip install --no-cache-dir opencv-python-headless==4.11.0.86 \
    && rm -rf /root/.cache/pip

    # Copy only necessary libraries
    COPY libs/python /app/libs/python
    RUN pip install --no-cache-dir /app/libs/python/s3_py \
    && pip install --no-cache-dir /app/libs/python/kafka_py


    I do these particular line because i notice in my requirements there's an AI model such as Real-ESRGAN and several other things listed opencv-python as their dependencies in their metadata.

    && pip uninstall -y opencv-python \
    && pip install --no-cache-dir opencv-python-headless==4.11.0.86 \


    However, eventhough i've uninstalled the opencv-python first, and proceed to install the headless version, the module cv2 was nowhere to be found, even after i list my packages using

    python -c "import pkgutil; print([m.name for m in pkgutil.iter_modules()])".

    is there any workaround to solve this?

    Continue reading...

Compartilhe esta Página