Home > Blockchain >  Deploying my Python (FastAPI) Application with Docker: ModuleNotFoundError: No module named 'Fo
Deploying my Python (FastAPI) Application with Docker: ModuleNotFoundError: No module named 'Fo

Time:08-03

I am trying to deploy my fastAPI applications using Docker. It's part of a bigger system which I am trying to connect with each other using a docker-compose later on. It works fine locally but when I try deploying it, it doesn't found my sub directories. I have __init__.py files in all directories.

This is my project structure:
enter image description here

And this is my Dockerfile:

FROM python:3.8

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN python3.8 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./app /code/app

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

The Dockerfile is taken from the FastAPI Documentation and I've also tried manually copying the main.py and recognition_service directory (and setting a python env) but it didn't change the Error (might have done it wrong though). Building goes fine, no errors, running only works until the import

from recognition_service.json_reader import Json_Reader

in my main.py, which causes the Error

File "/code/./app/main.py", line 3, in <module>
    from recognition_service.json_reader import Json_Reader
ModuleNotFoundError: No module named 'recognition_service'

This has probably some obvious solution I am overseeing since it's the first time I have a "bigger" application with Python, but I am hoping to find some help here.


As a side note, within the json_reader.py there are also imports for files in the spacy subdirectories, each interacting with their respective spacy models.

EDIT: Was asked for the requirements.txt:

fastapi==0.79.0
scipy==1.7.3
pydantic==1.8.2
uvicorn==0.17.6
uvloop==0.16.0

CodePudding user response:

it surely have something to do with the venv and the path, here is an old fastapi dockek combined with your code

FROM python:3.8-slim-buster

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# install system dependencies
RUN apt-get update \
    && apt-get -y install gcc make \
    && rm -rf /var/lib/apt/lists/*s

WORKDIR /code

ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

COPY ./requirements.txt /code/requirements.txt

RUN pip install -r /code/requirements.txt

EXPOSE 8000

COPY ./app /code/app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
  • Related