[Python] ModuleNotFoundError: No module named 'x' where x is a module suppose to be a shared...

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I have a project with three modules - one, two and three. one and two are stand-alone services and three is the common lib that will be used by one and two. When I tried to run docker-compose up --build at the root folder, both the module one and two containers failed to start with error log

    ModuleNotFoundError: No module named 'three'


    Below is the structure

    /root-folder
    │
    ├── one
    │ ├── Dockerfile
    │ ├── requirements.txt
    │ ├── __init__.py
    │ └── main.py
    │
    ├── two
    │ ├── Dockerfile
    │ ├── requirements.txt
    │ ├── __init__.py
    │ └── main.py
    │
    ├── three
    │ ├── Dockerfile
    │ ├── requirements.txt
    │ ├── __init__.py
    │ └── setup.py
    │
    └── docker-compose.yaml


    The docker-compose.yaml file is as below

    version: '3.8'

    services:
    one:
    container_name: container-one
    build:
    context: .
    dockerfile: ./one/Dockerfile
    image: one:latest
    ports:
    - "8881:8881"
    environment:
    - ENVIRONMENT=development

    two:
    container_name: container-two
    build:
    context: .
    dockerfile: ./two/Dockerfile
    image: two:latest
    ports:
    - "8882:8882"
    environment:
    - ENVIRONMENT=development


    And Dockerfile for one and two are

    FROM python:3.12-slim AS builder

    WORKDIR /build

    COPY /one/requirements.txt /build/ # ("COPY /two/requirements.txt /build/" for Dockerfile of module two)

    RUN python -m venv /opt/venv \
    && /opt/venv/bin/pip install --upgrade pip setuptools wheel \
    && /opt/venv/bin/pip install -r requirements.txt

    FROM python:3.12-slim

    WORKDIR /app

    COPY --from=builder /opt/venv /opt/venv

    ENV PATH="/opt/venv/bin:$PATH"

    COPY /one /app # ("COPY /two /app" for Dockerfile of module two)
    COPY /three /three

    RUN pip install /three && pip install -r requirements.txt

    CMD ["python", "main.py"]


    For three is

    FROM python:3.12-slim AS builder

    WORKDIR /build

    COPY requirements.txt /build/

    RUN python -m venv /opt/venv \
    && /opt/venv/bin/pip install --upgrade pip setuptools wheel \
    && /opt/venv/bin/pip install -r requirements.txt

    FROM python:3.12-slim

    WORKDIR /app

    COPY --from=builder /opt/venv /opt/venv

    ENV PATH="/opt/venv/bin:$PATH"

    COPY . .

    RUN pip install -e .


    and the setup.py for module three is

    from setuptools import setup, find_packages

    setup(
    name='three',
    version='0.1',
    packages=find_packages(include=['*']),
    install_requires=[
    'avro-python3==1.10.2',
    ],
    zip_safe=False,
    )


    Please help.

    Continue reading...

Compartilhe esta Página