Debug with VSCode
1
how to debug in vscode
Debugging in Visual Studio Code (VSCode) is a powerful feature that helps you find and fix issues in your code. Here's a step-by-step guide to get you started:
1. Install Required Extensions
Ensure you have the necessary extensions installed for the language you're working with. For example:
Python: Python extension
JavaScript/TypeScript: JavaScript Debugger (built-in)
C++: C++ extension
2. Open Your Project
Open the folder containing your project in VSCode.
3. Configure Debugging
VSCode uses a launch.json file to configure the debugger for your project.
Open the Debug view by clicking the debug icon in the Activity Bar on the side of the VSCode window.
Click on the gear icon to configure the
launch.jsonfile.Select the appropriate environment (e.g., Python, Node.js) if prompted.
A launch.json file will be created in the .vscode directory. It looks something like this for a Python project:
4. Set Breakpoints
Click in the gutter (the area to the left of the line numbers) to set breakpoints in your code. A red dot will appear, indicating the
2
how to debug fastapi in python
Debugging a FastAPI application in Visual Studio Code (VSCode) is straightforward with the right setup. Here’s how you can do it:
1. Install Required Extensions
Ensure you have the following extensions installed in VSCode:
Python extension
2. Install Dependencies
Make sure you have uvicorn installed as it's the ASGI server commonly used with FastAPI.
3. Open Your FastAPI Project
Open the folder containing your FastAPI project in VSCode.
4. Create a launch.json Configuration
launch.json ConfigurationVSCode uses a launch.json file to configure the debugger. Follow these steps to create it:
Open the Debug view by clicking the debug icon in the Activity Bar on the side of the VSCode window.
Click on the gear icon to create a
launch.jsonfile.Select "Python" as the environment.
Add the following configuration to the launch.json file:
Replace main:app with the appropriate module and app instance name for your FastAPI project. For example, if your main file is app.py and your FastAPI instance is named app, you should use app:app.
5. Set Breakpoints
Set breakpoints in your FastAPI code where you want the debugger to pause. You can do this by clicking in the gutter next to the line numbers in your code.
6. Start Debugging
Open the Debug view by clicking the debug icon in the Activity Bar.
Select the "Python: FastAPI" configuration.
Click the green play button to start debugging.
VSCode will start your FastAPI application using uvicorn, and you will be able to hit the breakpoints you set.
7. Access Your Application
Open your web browser and navigate to http://127.0.0.1:8000. Your FastAPI application should be running, and you can test the endpoints you have created.
Example FastAPI Application (main.py)
main.py)Here’s a simple FastAPI application for reference:
Now, when you navigate to http://127.0.0.1:8000/items/1?q=test in your browser, the debugger in VSCode should hit any breakpoints you have set in your FastAPI code.
This setup allows you to effectively debug your FastAPI application using VSCode, providing you with a robust environment to diagnose and fix issues in your code.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Last updated