new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Tutorial · all subjects

debugging

5 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Call uvicorn directly in FastAPI application

Import uvicorn and run it directly in your FastAPI application code. This allows you to call your Python program directly from a debugger. The uvicorn.run() call should be wrapped in an if __name__ == "__main__" block to ensure it only runs when the file is executed directly, not when it is imported.

Purpose of __name__ == '__main__' pattern

The __name__ == "__main__" check ensures that code is executed when a file is called directly (e.g., python myapp.py) but not when the file is imported by another module (e.g., from myapp import app). When a file is run directly, Python automatically sets the __name__ variable to the string "__main__". When imported, __name__ is set to the module name instead.

Debug FastAPI with Visual Studio Code

In Visual Studio Code, open the Debug panel, add a configuration by selecting "Add configuration...", choose "Python", then run the debugger with the option "Python: Current File (Integrated Terminal)". This will start the Uvicorn server with your FastAPI code and allow breakpoints to work.

Debug FastAPI with PyCharm

In PyCharm, open the Run menu, select "Debug...", then a context menu appears. Select the file to debug (such as main.py). This will start the Uvicorn server with your FastAPI code and allow breakpoints to work.

Example: uvicorn.run() in __main__ block

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

Give your agent this brain