Error loading ASGI app. Could not import module "main": how to fix Uvicorn
Uvicorn prints Error loading ASGI app. Could not import module "main" when Python cannot find the module named before the colon in uvicorn main:app, searching from the directory Uvicorn started in. Make the import string match your files (app/main.py is app.main:app), start from the folder above the package or pass --app-dir, and put the app object's real name after the colon.
By Livops team, the people who build and run LivopsUpdated
What the log shows, and the process stops right after it:
ERROR: Error loading ASGI app. Could not import module "main".
Its sibling comes from the same place:
ERROR: Error loading ASGI app. Attribute "app" not found in module "main".
Uvicorn prints these when it cannot turn the text you gave it, such as main:app, into an application object, and then exits with status 3[2]. This page explains how Uvicorn reads that text, lists the causes from the most common, and shows how to check the command before you deploy.
What does the error mean?
Uvicorn’s first argument is the application to run, in the format <module>:<attribute>[4]. FastAPI’s docs spell it out: in uvicorn main:app, main is the file main.py, the Python module, and app is the object created in it with app = FastAPI(); the command is equivalent to from main import app[5].
Uvicorn imports the part before the colon, then looks up the part after it[1]. Three different messages can come out of that:
Could not import module "main".Python did not find a module calledmainat all[1].Attribute "app" not found in module "main".The module was found and ran, but it has noapp[1].- A plain Python traceback, such as
ModuleNotFoundError: No module named 'routers'. Uvicorn shows its own message only when the missing module is the one you named. If the module it is importing fails to import something else, Uvicorn raises Python’s original error instead[1], and any other exception inside your file, aSyntaxErroror aKeyErrorfor a missing variable, is not caught there either.
Where does Python look? --app-dir decides: Uvicorn puts that directory first on the import path, and its default is the current working directory[3][4]. So the same command works or fails depending on the folder you start it from.
Causes, most common first
1. The app is inside a package folder
The most common layout after a tutorial grows:
project/
├── requirements.txt
└── app/
├── __init__.py
├── main.py # app = FastAPI()
└── routers/
uvicorn main:app started in project/ finds no main.py there. The module is app.main, a dotted path to a submodule, which Python imports by importing app first, then app.main[6]:
uvicorn app.main:app --host 0.0.0.0 --port $PORT
Run it from project/, the folder that contains app/. If main.py uses relative imports, such as from .routers import items, it has to be imported as part of its package[6], which app.main is and a bare main is not.
2. Uvicorn starts in another directory
The import string is right, but the process starts somewhere else: a Dockerfile’s WORKDIR, a systemd unit’s working directory, a monorepo where the server starts in the repository root while the code is in backend/. The default --app-dir is the working directory[4], so main is looked up in the wrong place.
Note one trap: with app.main:app and no app folder in the working directory, the missing module is app, not app.main. Python stops at the first part of the dotted name[6], and since that is not the name Uvicorn was given, you see Python’s ModuleNotFoundError: No module named 'app' instead of Uvicorn’s message[1].
Either start in the right folder, or tell Uvicorn where the module is:
uvicorn main:app --app-dir backend --host 0.0.0.0 --port $PORT
--app-dir adds the folder to the import path[4]. It suits a single main.py in a subfolder. For a package with relative imports, prefer cause 1’s fix: start from the folder above it and use the dotted name.
3. The command names a file, not a module
uvicorn main.py:app and uvicorn app/main.py:app look reasonable and are wrong: the part before the colon is a module name[4], dotted, without .py. Python reads main.py as a submodule called py inside a package called main[6]. Write main:app or app.main:app.
fastapi run app/main.py, by contrast, takes a file path and works out the import string itself; it prints the one it chose, for example Using import string: main:app[5].
4. The app object has another name
Attribute "app" not found means main.py was imported and has no top-level app[1]. Look at the line that creates it:
api = FastAPI() # then the command is: uvicorn main:api
The same message appears when app is created inside a function, or under if __name__ == "__main__":, because that block does not run when Uvicorn imports the file.
5. The app comes from a factory
If main.py has def create_app(): ... and no module-level app, point Uvicorn at the function and say it is a factory[4]:
uvicorn main:create_app --factory --host 0.0.0.0 --port $PORT
Without --factory, current Uvicorn still calls a function it is given and uses the result, with the warning ASGI app factory detected. Using it, but please consider setting the --factory flag explicitly.[2]
6. What about __init__.py?
A missing __init__.py does not cause this error on its own. A directory with __init__.py is a regular package, and one without it can still be imported as a namespace package[6], so app.main imports either way. Keep an __init__.py in your package folders anyway: it makes the layout explicit.
How to confirm the fix locally
Reproduce what the server does, from the folder the server starts in, with the exact start command:
cd project
python -c "from app.main import app; print(app)" # the import Uvicorn will do
uvicorn app.main:app --host 0.0.0.0 --port 8000
If the python -c line fails, Uvicorn will fail the same way, and the traceback is more detailed. In a container, run the same two lines with docker run and the image’s own working directory.
On Livops
-
The start command is found for you when it can be. For a Python web app, Livops looks for
app = FastAPI()orapp = Flask(__name__)inmain.py,app.pyorserver.py, also in a package one or two folders down, or for aweb:line in aProcfile. -
Or you write it. In the project’s Settings → Start, in a
Procfileat the project root, or asstartinlivops.jsonbefore the project is created; after that, the start command is changed in the settings. A web app listens on0.0.0.0and the port inPORT:web: uvicorn app.main:app --host 0.0.0.0 --port $PORT -
A monorepo. When the code lives in a subfolder, name that folder as the project’s folder when you connect the repository, for example
backend. -
When it fails. Uvicorn exits, the deploy ends as
start_failed, and the previous version keeps running. The Logs page shows the app’s last words, the line above among them, often with a Tip: line under it. ANo module named 'X'there is a missing package or folder: Python could not find a module.
The general Python case is in ModuleNotFoundError: No module named, and running FastAPI in production in FastAPI hosting.
Questions
What does uvicorn main:app actually mean?
main is the Python module, the file main.py, and app is the object inside it created with app = FastAPI(). It is the same as from main import app, run from the directory Uvicorn starts in.
My app is in app/main.py. What is the import string?
app.main:app, started from the folder that contains the app folder. A path with slashes or a .py ending is not a module name, so app/main.py:app and main.py:app both fail.
Do I need an __init__.py in the app folder?
Not for this error. A folder without __init__.py can still be imported as a namespace package, so app.main imports without it. Adding one is harmless and makes the folder a regular package.
What is the difference between Could not import module and Attribute not found?
Could not import module means Python did not find the file at all. Attribute app not found in module main means it found and ran main.py, but there is no object called app at its top level.
I see No module named 'routers' instead. Is that the same error?
No. Uvicorn found your main.py, but a module that main.py imports is missing, and Uvicorn shows Python's own error for it. Fix that import or add the package to requirements.txt.
Should I use --app-dir or change directory?
Prefer starting from the folder above your package with app.main:app, so imports inside the package keep working. --app-dir puts one folder on the import path, which suits a single main.py in a subfolder.
Where do I set the start command on Livops?
In the project's Settings, under Start, or in the start field of livops.json before the project is created. A Procfile web line works too.
Sources
- Uvicorn source — uvicorn/importer.py (raw.githubusercontent.com). Checked .
- Uvicorn source — uvicorn/config.py (raw.githubusercontent.com). Checked .
- Uvicorn source — uvicorn/main.py (raw.githubusercontent.com). Checked .
- Uvicorn docs — Settings (uvicorn.dev). Checked .
- FastAPI docs — Run a Server Manually (fastapi.tiangolo.com). Checked .
- Python docs — The import system (docs.python.org). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your FastAPI appRelated
- FastAPI hosting in 2026: how to deploy a FastAPI app, and whereA FastAPI app in production runs on Uvicorn without --reload, listens on 0.0.0.0 and the platform's port, trusts the proxy's headers and runs its migrations before it starts. On Livops a repository with main.py and requirements.txt starts on Uvicorn from a push, for 39 credits a month (39,000 so'm). Here is how Render, Railway, Fly.io and a VPS compare.
- ModuleNotFoundError: No module named — after a deployPython searched sys.path and could not find the module you import. After a deploy that almost always means the package is not in requirements.txt, is listed under a different name than the one you import, or the start command runs a Python that the packages were not installed into.
- App not listening on $PORT: why the health check failed after deployThe platform started your app, tried the port it gave it, and nothing answered in time. Almost always the app listens on its own fixed port instead of the PORT variable, listens on localhost (127.0.0.1) instead of 0.0.0.0, or crashed or was still starting when the check gave up. Listen on 0.0.0.0 and the port in PORT, and it passes.
- 502 Bad Gateway after deploying: what it means and how to fix itThe proxy in front of your app reached for it and got no valid answer: nothing was listening on the port it tried, or the process closed the connection mid-request. After a deploy that is almost always one of five things: the app listens on the wrong port or on 127.0.0.1, it crashed on start, it was still starting, it ran out of memory, or the proxy points at the wrong port. The app's own log says which.
- Gunicorn "Worker failed to boot": what it means and how to fix itGunicorn's worker could not load your application, so the master process shut down. The cause is the Python traceback printed just above the message: most often a missing package or variable, or a wrong module:variable path in the gunicorn command. Fix what the traceback names and the workers boot.
- PydanticImportError: BaseSettings has been moved to the pydantic-settings package — how to fix itPydanticImportError: BaseSettings has been moved to the pydantic-settings package means code written for Pydantic 1 is running on Pydantic 2, which moved BaseSettings into a separate package. Add pydantic-settings to requirements.txt, import it with from pydantic_settings import BaseSettings, and move class Config to model_config = SettingsConfigDict(...). Pinning pydantic<2 also gets the app running, but only as a stopgap.
- ImportError: attempted relative import with no known parent package: how to fix itImportError: attempted relative import with no known parent package means Python ran the file as a script, so it has no package for an import like from .config import X to start from. Run it as a module from the project root with python -m mybot.main, or import absolutely from a start file at the root. Its sibling, attempted relative import beyond top-level package, means the dots climb above the top package, usually because Python was started inside the package folder.