Friday, April 04, 2025

Debugging Python Projects in VS Code: A Step-by-Step Guide

 Debugging is an essential part of any software development process. For Python developers using VS Code, the integrated debugger offers powerful tools to identify and resolve issues efficiently. This guide will walk you through setting up and using the VS Code debugger for your Python projects.

Step 1: Python Installation

Before diving into VS Code, ensure Python is installed on your system. For Windows users, download the latest stable version from the official Python website:

Step 2: Verify Python Installation and PATH Configuration

After installation, confirm that Python is correctly installed and accessible via the command line. Open your terminal and run:

Bash

1
python --version

This should display the installed Python version, for example:

1
Python 3.13.2

Also, verify that the Python installation directory is added to your system’s PATH environment variable. During installation, you’ll be prompted to add Python to PATH. If you skipped this step, manually add it to your environment variables.

To test Python functionality, run the Python interpreter directly:

Bash

1
python

Then, execute a simple print statement:

Python

1
2
3
>>> print("hello")
hello
>>> exit()

Step 3: VS Code Setup and Extensions

If you haven’t already, install VS Code. Then, install the necessary Python extensions:

  • Ensure you have the official Microsoft Python extension installed. This extension provides excellent support for Python development, including debugging capabilities.

Step 4: Setting up a Flask Project and Virtual Environment

Let’s create a simple Flask application for demonstration:

  1. Create flaskrest.py:

Python

1
2
3
4
5
6
7
8
9
10
from flask import Flask, jsonify, request
 
app = Flask(__name__)
 
@app.route('/api/data', methods=['GET'])
def get_data():
    return jsonify({'data': 'Hello, World!'})
 
if __name__ == '__main__':
    app.run(debug=True)
  1. Create a Virtual Environment:

It’s best practice to use virtual environments to isolate project dependencies. In your project directory, run:

Bash

1
python -m venv env
  1. Activate the Virtual Environment:

Bash

1
.\env\Scripts\activate

Your terminal prompt should now indicate the active environment: (env) C:\vscode-python-workspace>.

  1. Install Flask:

Run the following command to install the Flask library:

Bash

1
pip install Flask
  1. Upgrade pip:

It is a good practice to use the latest version of pip. So run this command.

Bash

1
python.exe -m pip install --upgrade pip
  1. Run the Flask Application:

Bash

1
python flaskrest.py

You should see the Flask development server start, indicating the API is running at http://127.0.0.1:5000.

  1. Access the API:

Open your web browser and navigate to http://127.0.0.1:5000/api/data. You should see the JSON response: {"data": "Hello, World!"}.

Step 5: Debugging with VS Code

  1. Create launch.json:

To configure the debugger, create a launch.json file. In VS Code, go to the “Run and Debug” view (Ctrl+Shift+D) and click “create a launch.json file”. Select “Python File” as the debugging configuration.

  1. Set a Breakpoint:

In flaskrest.py, set a breakpoint on the return jsonify({'data': 'Hello, World!'}) line.

  1. Start Debugging:

In the “Run and Debug” view, click the green “Start Debugging” button (F5).

  1. Trigger the Breakpoint:

Access the API endpoint in your browser again (http://127.0.0.1:5000/api/data). The execution should pause at the breakpoint in VS Code.

  1. Inspect Variables and Step Through Code:

Use the VS Code debugger controls to inspect variables, step through the code, and analyze the program’s state.

  1. Adding Configurations:

If you need to add other debug configurations, you can do so by editing the launch.json file manually or by clicking the “Add Configuration” button.

By following these steps, you can effectively debug your Python projects using VS Code, streamlining your development process and resolving issues efficiently.


No comments: