question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error loading ASGI app. Could not import module "src.main"

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.security.oauth2 import OAuth2PasswordRequestForm
from . import models
from . import services
from .database import engine, get_db
from passlib.context import CryptContext
from sqlalchemy.orm import Session
from datetime import datetime, timedelta

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl='login')

pwd_context = CryptContext(schemes=['bcrypt'], depracated='auto')

models.Base.metadata.create_all(bind=engine)

SECRET_KEY = '[SECRET KEY]'
ALGORITHM = 'HS256'
ACCESS_TOKEN_EXPIRE_MINUTES = 30



@app.get("/")
async def root():
    return {"message": "Hello World"}
    
@app.get("/test/")
async def test(token:str = Depends(oauth2_scheme)):
    return {'token', token}

@app.post('/login/{method}')
async def login(method: str, form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db), ):
    user = services.authenticate_user(form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = services.create_access_token(
        data={"username": user.username, "email": user.email}, expires_delta=access_token_expires
    )
    return {"access_token": access_token, "token_type": "bearer"}

Description

I saw the solution is changing ‘main’ -> ‘src.main’ so I tried that, but the same problem is occuring Even if I use ‘cd src’ to move to src and run ‘uvicorn main:app --reload’, errors continue. What do I need?

Operating System

Linux

Operating System Details

WSL 2

FastAPI Version

0.68.1

Python Version

3.8.10

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
filipinjocommented, Feb 18, 2022

The error message will appear if there is circular import error. Unfortunately, instead of circular import error the “Error loading ASGI app. Could not import module ….” is reported.

4reactions
peterbb148commented, Nov 4, 2021

Have exact same problem. Here’s my code:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

Command line:

uvicorn fastapi:app --reload --port 8001
INFO:     Will watch for changes in these directories: ['/Users/peter/Git/peterbb148/Python']
INFO:     Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
INFO:     Started reloader process [3942] using statreload
ERROR:    Error loading ASGI app. Could not import module "fastapi".

All file are in the same directory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI throws an error (Error loading ASGI app. Could not ...
Could not import module "main". The answer is so simple, add the folder name in front of your filename uvicorn src.main:app --reload.
Read more >
FastAPI Error loading ASGI app. Could not import module 'main'
The FastAPI Error loading ASGI app. Could not import module 'main' occurs when your terminal is located in a directory that doesn't contain...
Read more >
Error loading ASGI app. Could not import module "main"
Install the software on Ubuntu, accidentally upgrade PIP, resulting in the use Times as follows: Later, it was found that this problem was...
Read more >
loading ASGI app. Could not import module main - YouTube
HOPEFULLY I CAN SAVE YOU TIME FROM SCOURING FOR HOURS ON GOOGLE or STACKOVERFLOW. The solution to FASTAPI WARNING:" ERROR : Error loading...
Read more >
tiangolo/fastapi - Gitter
py' extension, I have the next error: ERROR: Error loading ASGI app. Could not import module "main". Is there any way to run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found