CLion 2021.2 Help

Debug Python extensions

Cross-debugging for mixed Python and C/C++ code is not currently supported in CLion (CPP-5797). However, you can use one of the options described below to workaround this when working with Python extension modules.

In order to debug the C/C++ code of an extension, you need to use the native debugger, GDB or LLDB, and debug the interpreter process that loads your script and runs the application. For that, you can either attach to a running Python process or debug a properly configured Custom Build Application.

Option 1: Attach to a running Python process with the native debugger

  1. Set breakpoints and make sure they are going to be hit after you attach to the process.

  2. Launch the Python script. You can do that from your system terminal, CLion's built-in terminal, or by running a Python configuration.

  3. Call Run | Attach to Process from the main menu or press Ctrl+Alt+F5.

  4. In the list of entries, search for the one that corresponds to the process's PID under the Native node:

    Attaching to a running Python process
  5. After the debugger is attached successfully, you will be able to debug your native code as usual. See the Debugging section for details.

  6. When finished, call Run | Stop or click Suspend on the toolbar or in the Debug tool window to detach from the process.

Option 2: Debug a Custom Build Application configuration

As an alternative to attaching the native debugger, you can launch the Python interpreter under it instead. This option is available if you know the exact interpreter command that invokes your script.

You need to create a custom target that will rebuild the native extension and a Custom Build Application configuration that will call the custom target and execute the Python interpreter with proper arguments.

  1. Go to Run | Edit Configurations, click Icons general add, and select Custom Build Application.

  2. Click Configure Custom Build Targets below the Target field. This will open the Settings / Preferences | Build, Execution, Deployment | Custom Build Targets dialog.

  3. Click Icons general add to add a new target and set the target's name.

  4. Select the toolchain. CLion will use the debugger from the selected toolchain.

  5. Set up the tool for building your project. Click the Browse button next to the Build field and then Icons general add to add a new tool.

    Specify the tool's configuration. The screenshot below illustrates the case of using pip install -e:

    Pip install as an external tool

  6. Save the changes in the Edit Tool and Custom Build Targets dialogs, and get back to setting up the run/debug configuration.

    Custom Build Application

    Specify the following settings:

    • Executable- provide the path to the Python interpreter.

    • Program arguments- specify the script name with additional arguments if required.

    • Working directory- set the path to the project root. You can use macros (Icons general add dark) and path variables here.

  7. After saving the configuration, you can start a debug session for it as usual:

    Debugging a Python extension

Last modified: 28 April 2021