# 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.

Source: https://livops.dev/en/guides/error-uvicorn-could-not-import-module

By Livops team, the people who build and run Livops. Updated 1 December 2026.

What the log shows, and the process stops right after it:

```text
ERROR:    Error loading ASGI app. Could not import module "main".
```

Its sibling comes from the same place:

```text
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 called `main` at all[1].
- **`Attribute "app" not found in module "main".`** The module was found and ran, but it has no `app`[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, a `SyntaxError` or a `KeyError` for 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:

```text
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]:

```sh
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:

```sh
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:

```python
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]:

```sh
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:

```sh
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()` or `app = Flask(__name__)` in `main.py`, `app.py` or `server.py`, also in a package one or two folders down, or for a `web:` line in a `Procfile`.
- **Or you write it.** In the project's **Settings → Start**, in a `Procfile` at the project root, or as `start` in `livops.json` before the project is created; after that, the start command is changed in the settings. A web app listens on `0.0.0.0` and the port in `PORT`:

  ```text
  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. A `No module named 'X'` there is a missing package or folder: [Python could not find a module](https://livops.dev/en/docs/error-start#py-module).

The general Python case is in [ModuleNotFoundError: No module named](https://livops.dev/en/guides/error-module-not-found-python), and running FastAPI in production in [FastAPI hosting](https://livops.dev/en/hosting/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

1. [Uvicorn source — uvicorn/importer.py](https://raw.githubusercontent.com/encode/uvicorn/master/uvicorn/importer.py). Checked 1 December 2026.
2. [Uvicorn source — uvicorn/config.py](https://raw.githubusercontent.com/encode/uvicorn/master/uvicorn/config.py). Checked 1 December 2026.
3. [Uvicorn source — uvicorn/main.py](https://raw.githubusercontent.com/encode/uvicorn/master/uvicorn/main.py). Checked 1 December 2026.
4. [Uvicorn docs — Settings](https://uvicorn.dev/settings/). Checked 1 December 2026.
5. [FastAPI docs — Run a Server Manually](https://fastapi.tiangolo.com/deployment/manually/). Checked 1 December 2026.
6. [Python docs — The import system](https://docs.python.org/3/reference/import.html). Checked 1 December 2026.
