'FastAPI cannot find main.py: `ERROR: Error loading ASGI app. Could not import module "main"

I know usually it is something about project structure that raises this error.

But my project structure is the simplest one:

.
├── blog.db
├── database.py
├── main.py
├── models.py
├── poetry.lock
├── __pycache__
│   ├── database.cpython-39.pyc
│   ├── main.cpython-39.pyc
│   ├── models.cpython-39.pyc
│   ├── schemas.cpython-39.pyc
│   └── services.cpython-39.pyc
├── pyproject.toml
├── requirements.txt
├── schemas.py
└── services.py
 

Yet every time I run

uvicorn main:app --reload

I get

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

So what exactly am I doing wrong if I have all my files in the same folder?



Solution 1:[1]

I have the same problem with running fastAPI. After checking the file path and everything, I figure the solution is very simple. You just need to save your python file (in this case "main.py") before run uvicorn command on cmd. That's it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quang Tu