Fastapi: ModuleNotFoundError: No module named 'app'

Created on 29 Aug 2020  路  5Comments  路  Source: tiangolo/fastapi

The deployment example in this link does not work:

https://fastapi.tiangolo.com/deployment/

It gives the error:

ModuleNotFoundError: No module named 'app'

answered question

Most helpful comment

Get rid of the WORKDIR /app from your Dockerfile and try. If you docker run -it <container> /bin/bash, you'll see your current directory is in the app path.

# cat Dockerfile 
FROM python:3.7 

RUN pip install fastapi uvicorn  

EXPOSE 80

COPY ./app /app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
# docker run $(docker build -q .)
INFO:     Started server process [1]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)

All 5 comments

can you please share your code?

I created a folder "practice" and in that folder i created another folder "app" and within that folder a file main.py:

from typing import Optional

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}

In the folder "practice" I created a Dockerfile:


FROM python:3.7 
#tiangolo/uvicorn-gunicorn-fastapi:python3.6

RUN pip install fastapi uvicorn  

WORKDIR /app/

# The EXPOSE command makes the port 80 accessible to the outside world (our flask service runs on port 80;
# we need this port inside the container to be accessible outside the container).
EXPOSE 80

COPY ./app /app

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

Get rid of the WORKDIR /app from your Dockerfile and try. If you docker run -it <container> /bin/bash, you'll see your current directory is in the app path.

# cat Dockerfile 
FROM python:3.7 

RUN pip install fastapi uvicorn  

EXPOSE 80

COPY ./app /app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
# docker run $(docker build -q .)
INFO:     Started server process [1]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)

Thanks for the help here everyone! :clap: :bow:

If that solves the original problem, then you can close this issue :heavy_check_mark:

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings