Error loading ASGI app. Could not import module "src.main"
See original GitHub issueFirst 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:
- Created 2 years ago
- Comments:22 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Have exact same problem. Here’s my code:
Command line:
All file are in the same directory.