0

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"
Matheus
  • 139
  • 4
  • 13
  • 2
    Your project Python paths begin with `api`; for example, you tell uvicorn that the main module is `api.main`. In your code you need to `import api.routers` and so on. (I'd expect you'd see the same problem in a non-Docker Python virtual environment, and that setup might be easier to debug.) – David Maze Aug 06 '22 at 00:01
  • I'he tried this, but gave's me another error. When i run ```docker-compose pytest api```. https://stackoverflow.com/questions/73242525/get-error-modulenotfounderror-no-module-named-api?noredirect=1#comment129351708_73242525 – Matheus Aug 06 '22 at 03:37

1 Answers1

1

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.