Search code examples
pythonvisual-studio-codefastapiprogram-entry-pointuvicorn

invalid command name 'main:app' when I execute "uvicorn main:app --reload"


When I try to execute the command uvicorn main:app --reloadfor start my local server, the VSCode terminal throws the error invalid command name 'main:app'. The code below imports a variable from the pipeline of the generative AI model using FastAPI to estabilish a route for the generated responses. I guess the trick is in the main method that imports uvicorn library and uses it to run the app, apparently the code is right but maybe I missed something.

#main.py
from fastapi import FastAPI
import sys
import os

# Adds the root directory to sys.path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from src.inference.pipeline import generate_response
app = FastAPI()

@app.post("/predict")
def predict(input_text: str):
    response = generate_response(input_text)
    return {"response": response}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)

Solution

  • Decouple the main and app logic. Place this part under server.py file in the same directory:

    # server.py
    from fastapi import FastAPI
    import sys
    import os
    
    # Adds the root directory to sys.path
    sys.path.append(os.path.dirname(os.path.abspath(__file__)))
    
    from src.inference.pipeline import generate_response
    app = FastAPI()
    
    @app.post("/predict")
    def predict(input_text: str):
        response = generate_response(input_text)
        return {"response": response}
    

    In the main.py keep only this part:

    if __name__ == "__main__":
        import uvicorn
    
        uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True)
    

    Note that "server:app" indicates "module:app" path.