Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run fastapi project in debug mode auto reload in vscode

Started with fastapi python.

this is how I am string my server

class Server:
    def __init__(self):
        self.app = FastAPI()

    def runServer(self, host: str, port: int,is_dev:bool):
        uvicorn.run(self.app, host=host, port=port,debug=is_dev)


if __name__ == "__main__":
    server = Server()
    # read the environment variables
    host: str = os.environ['host']
    port: int = int(os.environ['port'])
    is_dev: bool = bool(os.environ['dev'])

    server.runServer(host, port, is_dev)

this ups the server but not running in auto reload mode if i make any changes.

even I tried

uvicorn.run(self.app, host=host, port=port, reload=is_dev)

reload is i guess not an option, thus causing the server to break.

i tried passing --reload args in launch.json even but still not working

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: FastAPI",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/main.py",
            "jinja": true,
            "args": ["--reload"],
            "env": {
                "host": "127.0.0.1",
                "port": "5555",
                "dev": "true"
            }
        }
    ]
}

any idea? am i missing something?

like image 597
Sunil Garg Avatar asked Feb 17 '21 08:02

Sunil Garg


People also ask

How do I run FastAPI in Vscode?

Hit Ctrl + Shift + D to open the Run and Debug window. Find the dropdown and choose the value that has value Python: FastAPI we provided for the key name in our debug configuration in launch. json file. Select Python: FastAPI and click on the green button.

How do I run a Python script in debug mode in Terminal?

To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

How do I enable debugging in Visual Studio code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do I debug a code in Visual Studio python?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.


1 Answers

uvicorn will start in the reload mode only if the app argument is a string in the format <module>:<app_instance> and reload or debug argument are true. Like this:

if __name__ == "__main__":
    uvicorn.run("example:app", host="127.0.0.1", port=5000, reload=True)

Excerpt from the documentation:

Note that the application instance itself can be passed instead of the app import string.

uvicorn.run(app, host="127.0.0.1", port=5000, log_level="info")

However, this style only works if you are not using multiprocessing (workers=NUM) or reloading (reload=True), so we recommend using the import string style.

like image 121
alex_noname Avatar answered Oct 21 '22 16:10

alex_noname