Fastapi: ModuleNotFoundError: No module named 'app'

Created on 12 Jun 2020  路  4Comments  路  Source: tiangolo/fastapi

````
FROM
USER root
RUN dnf -y update &&
dnf -y install gcc &&
dnf -y groupinstall "Development Tools" &&
dnf -y install python3 &&
dnf -y install python3-devel &&
pip3 install fastapi &&
pip3 install uvicorn

EXPOSE 8080
COPY ./app /app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
````

````
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: str = None):
return {"item_id": item_id, "q": q}
````

exception container log:

Traceback (most recent call last): File "/usr/local/bin/uvicorn", line 11, in <module> sys.exit(main()) File "/usr/local/lib/python3.6/site-packages/click/core.py", line 829, in __call__ return self.main(*args, **kwargs) File "/usr/local/lib/python3.6/site-packages/click/core.py", line 782, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1066, in invoke return ctx.invoke(self.callback, **ctx.params) File "/usr/local/lib/python3.6/site-packages/click/core.py", line 610, in invoke return callback(*args, **kwargs) File "/usr/local/lib/python3.6/site-packages/uvicorn/main.py", line 331, in main run(**kwargs) File "/usr/local/lib/python3.6/site-packages/uvicorn/main.py", line 354, in run server.run() File "/usr/local/lib/python3.6/site-packages/uvicorn/main.py", line 382, in run loop.run_until_complete(self.serve(sockets=sockets)) File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete File "/usr/local/lib/python3.6/site-packages/uvicorn/main.py", line 389, in serve config.load() File "/usr/local/lib/python3.6/site-packages/uvicorn/config.py", line 288, in load self.loaded_app = import_from_string(self.app) File "/usr/local/lib/python3.6/site-packages/uvicorn/importer.py", line 23, in import_from_string raise exc from None File "/usr/local/lib/python3.6/site-packages/uvicorn/importer.py", line 20, in import_from_string module = importlib.import_module(module_str) File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked ModuleNotFoundError: No module named 'app'

answered question

Most helpful comment

I get the same error.
Make /app your working directory in the Dockerfile does not change this.

All 4 comments

Hey @anravi , please follow the template, review all the checks, etc.

Anyway, you would probably have to make /app your working directory in your Dockerfile:

WORKDIR /app/

You could probably also use the official Docker image: https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker

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

I get the same error.
Make /app your working directory in the Dockerfile does not change this.

@jhaggle that might be related to a lot of different ways your Python is not finding the app module.

For example, if the directory /app already exists in the image, then doing:

COPY ./app /app

will actually copy ./app into the container at /app/app instead of /app.

But again, that will depend on each case and each Dockerfile.

You can probably follow the instructions of the official Docker images and/or copy or replicate what they do.

Otherwise, if you have a minimal, self-contained example that reproduces your error, please create a new issue following all the steps in the issue template.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

updatatoday picture updatatoday  路  3Comments

scheung38 picture scheung38  路  3Comments

Wytamma picture Wytamma  路  3Comments

vnwarrior picture vnwarrior  路  3Comments

zero0nee picture zero0nee  路  3Comments