uvicorn-gunicorn-fastapi-docker
uvicorn-gunicorn-fastapi-docker copied to clipboard
ModuleNotFoundError: No module named 'app'
Hi, the error as mentioned in the title is thrown when running the container. Below I tried to describe my setup in detail.
I have setup everything exactly as described in the readme. Therefore, this is how my Dockerfile looks like.
FROM python:3.10 as requirements-stage
WORKDIR /tmp
RUN pip install poetry
COPY ./pyproject.toml ./poetry.lock* /tmp/
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.10
COPY --from=requirements-stage /tmp/requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY ./app /app
my directory structure is as follows.
app
├── main.py
├── api
│ └── __init__.py
| └── user.py
└── tests
└── __init__.py
pyproject.toml
Dockerfile
then, in the app/main.py
I am trying to import user
from the api
-directory by doing:
from app.api import user
When running the container, the following error is thrown.
ModuleNotFoundError: No module named 'app'
This is probably because app
is not in the PYTHONPATH. I have tried to add it to the Dockerfile, but this did not help.
I want to keep the structure and the way of importing it as is as it is also needed by my test setup.
How can I fix this? The problem has probably a very trivial solution, but I am currently stuck.
Thanks in advance
I've just had a similar error. I solved it this way.
My source code directory is app. DockerFile "COPY" "./app" "/app" 이라 formed by a structure called /app/app You change Dockerfile.
COPY . /app
OR
COPY ./app /app_code
WORKDIR /app_code
@JaeHyunL nailed it! Thank you, whoever you are!
I am using the Heroku container stack. My Dockerfile looked like this:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11-slim
COPY ./requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY ./app /app
I changed it to this (appended /app
on the COPY line):
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11-slim
COPY ./requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY ./app /app/app
Also, FWIW, my heroku.yml file looks like this:
build:
docker:
web: Dockerfile
run:
web: sh /start.sh