The Python extension in Visual Studio Code (VS Code) offers an array of debugging features to streamline your app development experience. This includes support for various types of Python applications, such as scripts, web apps, and remote processes. If you're new to Python debugging, this article provides a comprehensive walkthrough of the process.

Initialize Configurations

To begin with, you'll need to initialize debug configurations in VS Code. These configurations drive the behavior of your debugging sessions and are defined in a launch.json file stored in the .vscode folder within your workspace. Note that your code must be stored in a folder for this process to work.

To generate a launch.json file with Python configurations, follow these steps:

  • Open the Run view in the sidebar by clicking on the Run button.
  • If you don't have any configurations defined yet, you'll see a button to Run and Debug and a link to create a configuration file (launch.json).
  • Select the create a launch.json file link or use the Run > Open configurations menu command.
  • In the configuration menu, select Python Debugger from the debugger options list.
  • A pre-defined configuration based on your selection will be created and opened in the launch.json file.

Additional Configurations

By default, VS Code shows only the most common configurations provided by the Python Debugger extension. However, you can include other configurations in the launch.json file using the Add Configuration command. This allows you to add custom configurations or modify existing ones.

When debugging, the Status Bar displays the current configuration and interpreter. You can select a different configuration from this list or use the python value in the launch.json file to specify a different interpreter for debugging purposes.

Basic Debugging

To start debugging your Python script, you can simply select the down-arrow next to the run button on the editor and choose Python Debugger: Debug Python File. If you're looking to debug a web application using Flask, Django, or FastAPI, the Python Debugger extension provides dynamically created debug configurations based on your project structure.

Alternatively, you can start the debugger through the Run view by clicking on the Run and Debug button. When no configuration has been set, you'll be given a list of debugging options. You can select the appropriate option to quickly debug your code.

Command Line Debugging

The Python Debugger extension also allows for command line debugging. To start debugging from the command line, ensure that debugpy is installed in your Python environment. You can install debugpy using the following command: python -m pip install --upgrade debugpy.

Tip: Using a virtual environment is not required but recommended best practice. You can create a virtual environment in VS Code by opening the Command Palette and running the Python: Create Virtual Environment command.

The command line syntax for debugging is as follows:

`

python -m debugpy

--listen | --connect [:]

[--wait-for-client]

`

By leveraging the Python Debugger extension in VS Code, you can streamline your app development experience and focus on writing high-quality code. With its robust set of features and configurations, this extension is an essential tool for any Swift app developer working with Python.