ModuleNotFoundError: No module named 'routers'

Question:

I’ve got this error :
from routers.patients import router as PatientsRouter
ModuleNotFoundError: No module named ‘routers’

When i try to run docker-compose up api with my project.

My file structure:

enter image description here

My Dockerfile:

FROM python:3.9-slim-buster

WORKDIR /project

COPY ./requirements.txt .

RUN pip install -r requirements.txt

COPY ./api /project/api/

CMD ["uvicorn", "api.main:app", "--host=0.0.0.0", "--port=8000"]

EXPOSE 8000

My docker-compose:

version: "3.8"
services:
  api:
    build: .
    command: ["uvicorn", "api.main:app", "--host=0.0.0.0", "--port=8000"]
    volumes:
      - ./api:/project/api
    ports:
      - "8000:8000"
Asked By: Matheus

||

Answers:

In order to import from routers, python needs the __init__.py file in that same directory to treat it as a module.

It looks like your init file in the api directory has an extra underscore at the beginning.

Answered By: dreamwalkeranderson