---
title: "Debugging"
description: "Learn how to debug a backtest in different editor supported by Lean CLI."
canonical: https://www.quantconnect.com/docs/v2/lean-cli/backtesting/debugging
markdown: https://www.quantconnect.com/docs/v2/lean-cli/backtesting/debugging.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/05%20Lean%20CLI/08%20Backtesting/02%20Debugging
modified: 2026-10-28
index: https://www.quantconnect.com/llms.txt
---

# Debugging

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Lean CLI](https://www.quantconnect.com/docs/v2/lean-cli.md) > Backtesting > Debugging

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

Debugging is an important part of writing any algorithm. The CLI makes it easy to use the builtin debugger of the most popular editors to debug LEAN algorithms. This page explains how to start local debugging for Python and C# with all editors supported by the CLI.

To use the CLI, you must be a member in an [organization](https://www.quantconnect.com/docs/v2/cloud-platform/organizations/tier-features.md) on a paid tier.

## Python and PyCharm

Local debugging for Python in PyCharm requires PyCharm's remote debugging functionality, which is only available in PyCharm Professional. After making sure you are running the Professional edition, follow these steps to start local debugging for Python in PyCharm:

1. Open a terminal in your [organization workspace](https://www.quantconnect.com/docs/v2/lean-cli/initialization/organization-workspaces.md) and run `lean library add "<projectName>" pydevd-pycharm --version <pydevdVersion>` to add the debugger package that matches your PyCharm version.

   ```
   $ lean library add "My Project" pydevd-pycharm --version 223.8836.43
   Adding package pydevd-pycharm to project <workspace>\My Project
   Checking compatibility of pydevd-pycharm 223.8836.43 with the Python version used in the Docker images
   Adding pydevd-pycharm 223.8836.43 to 'My Project\requirements.txt'
   Installing pydevd-pycharm 223.8836.43 in local Python environment to provide local autocomplete
   ```

   When PyCharm opens the project, it prompts you to install `pydevd-pycharm` into the environment. Accept the prompt to proceed.
2. Follow the [How to set up local autocomplete for Python in PyCharm](https://www.quantconnect.com/docs/v2/lean-cli/projects/autocomplete.md#02-Python-and-PyCharm) tutorial.
3. Open the directory containing the `main.py` file in a new PyCharm window. It is important that you open the project directory itself, not your [organization workspace](https://www.quantconnect.com/docs/v2/lean-cli/initialization/organization-workspaces.md).
4. Start debugging using the **Debug with Lean CLI** run configuration (this configuration is created when you create a new project with the CLI). ![PyCharm debug button](https://cdn.quantconnect.com/i/tu/debug-python-pycharm.png)
5. Open a terminal in your organization workspace and run `lean backtest "<projectName>" --debug pycharm`.

   ```
   $ lean backtest "My Project" --debug pycharm
   20210322 18:58:23.355 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit)
   20210322 18:58:23.360 TRACE:: Engine.Main(): Started 6:58 PM
   ```
6. Terminate the debugger in PyCharm once LEAN has exited.

After finishing Python debugging with PyCharm, you will see a message saying "Connection to Python debugger failed". You can safely ignore this message.

## Python and VS Code

Follow these steps to start local debugging for Python in VS Code:

1. Follow the [How to set up local autocomplete for Python in VS Code](https://www.quantconnect.com/docs/v2/lean-cli/projects/autocomplete.md#03-Python-and-VS-Code) tutorial.
2. Open the directory containing the `main.py` file in a new VS Code window. It is important that you open the project directory itself, not your [organization workspace](https://www.quantconnect.com/docs/v2/lean-cli/initialization/organization-workspaces.md).
3. Open a terminal in your organization workspace, run `lean backtest "<projectName>" --debug debugpy`, and then wait until the CLI tells you to attach to the debugger.

   ```
   $ lean backtest "My Project" --debug debugpy
   20240507 20:35:54.970 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit)
   20240507 20:35:55.272 TRACE:: Engine.Main(): Started 8:35 PM
   20240507 20:36:01.104 TRACE:: DebuggerHelper.Initialize(): debugpy waiting for attach at port 5678...
   ```
4. In VS Code, open the **Run** tab and run the configuration called **Debug with Lean CLI** (this configuration is created when you create a new project with the CLI). ![VS Code debug button](https://cdn.quantconnect.com/i/tu/debug-vscode-launch-config.png)

## C# and Visual Studio

Follow these steps to start local debugging for C# in Visual Studio:

1. Follow the [How to set up local autocomplete for C# in Visual Studio](https://www.quantconnect.com/docs/v2/lean-cli/projects/autocomplete.md#04-C-and-Visual-Studio) tutorial.
2. Open the project containing the `Main.cs` file in a new Visual Studio window. It is important that you open the project directory itself, not your [organization workspace](https://www.quantconnect.com/docs/v2/lean-cli/initialization/organization-workspaces.md).
3. Open a terminal in your organization workspace, run `lean backtest "<projectName>" --debug vsdbg`, and wait until the CLI tells you to attach to the debugger.

   ```
   $ lean backtest "My Project" --debug vsdbg
   20210423 13:50:54.634 TRACE:: DebuggerHelper.Initialize(): waiting for debugger to attach...
   ```
4. In Visual Studio, open the process selector using **Debug > Attach to Process...**.
5. Select **Docker (Linux Container)** as the connection type.
6. Select **lean_cli_vsdbg** as connection target.
7. Double-click on the process named **dotnet**.
8. Tick the checkbox in front of **Managed (.NET Core for Unix)** and click **OK** to start debugging.

After finishing C# debugging with Visual Studio you will see a message saying "The debug adapter exited unexpectedly.". You can safely ignore this message.

## C# and Rider

Follow these steps to start local debugging for C# in Rider:

1. Follow the [How to set up local autocomplete for C# in Rider](https://www.quantconnect.com/docs/v2/lean-cli/projects/autocomplete.md#05-C-and-Rider) tutorial.
2. Open the project containing the `Main.cs` file in a new Rider window. It is important that you open the project directory itself, not your [organization workspace](https://www.quantconnect.com/docs/v2/lean-cli/initialization/organization-workspaces.md).
3. Open a terminal in your organization workspace, run `lean backtest "<projectName>" --debug rider`, and wait until the CLI tells you to attach to the debugger.

   ```
   $ lean backtest "My Project" --debug rider
   20210423 13:50:54.634 TRACE:: DebuggerHelper.Initialize(): waiting for debugger to attach...
   ```
4. In Rider, select **Run > Attach To Remote Process...**.
5. In the pop-up that opens, select the target named **root@localhost:2222**.
6. Wait for Rider to connect and select the process named **dotnet QuantConnect.Lean.Launcher.dll** when a selector pops up to start debugging. You may have to select **Remote debugger tools are not loaded to the remote host. Click to load** first.

## C# and VS Code

Follow these steps to start local debugging for C# in VS Code:

1. Follow the [How to set up local autocomplete for C# in VS Code](https://www.quantconnect.com/docs/v2/lean-cli/projects/autocomplete.md#06-C-and-VS-Code) tutorial.
2. Open the directory containing the `Main.cs` file in a new VS Code window. It is important that you open the project directory itself, not your [organization workspace](https://www.quantconnect.com/docs/v2/lean-cli/initialization/organization-workspaces.md).
3. Open a terminal in your organization workspace, run `lean backtest "<projectName>" --debug vsdbg`, and wait until the CLI tells you to attach to the debugger.

   ```
   $ lean backtest "My Project" --debug vsdbg
   20210423 13:50:54.634 TRACE:: DebuggerHelper.Initialize(): waiting for debugger to attach...
   ```
4. In VS Code, open the **Run** tab and run the configuration called **Debug with Lean CLI** (this configuration is created when you create a new project with the CLI). ![VS Code debug button](https://cdn.quantconnect.com/i/tu/debug-vscode-launch-config.png)

After finishing C# debugging with VS Code you will see a message saying "The pipe program 'docker' exited unexpectedly with code 137.". You can safely ignore this message.
