PyCharm Debugger (pydevd-pycharm)

261.23567.35 · active · verified Sat Apr 11

pydevd-pycharm is the Python debugger backend used by JetBrains PyCharm and IntelliJ IDEA. It facilitates debugging Python applications, including remote debugging, stepping through code, and inspecting variables. The library is actively maintained and updated frequently, typically in sync with PyCharm and IDEA releases, with versions like '261.23567.35' reflecting internal build numbers.

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up remote debugging with `pydevd-pycharm`. It instructs the Python process to connect to a PyCharm debugger server running on a specified host and port. This is typically used when debugging code running in a separate environment (e.g., a Docker container, remote server) from your PyCharm IDE. Ensure your PyCharm IDE is configured to listen for remote debug connections on the specified host and port.

import os
from pydevd_pycharm.pydevd import settrace

# Configure PyCharm for remote debugging first (Run -> Edit Configurations -> Python Remote Debug).
# Get the host and port from your PyCharm setup.
# The host should be the IP address of the machine running PyCharm (e.g., your local machine's IP).
# The port is what PyCharm is listening on (e.g., 51235).

PYCHARM_DEBUG_HOST = os.environ.get('PYCHARM_DEBUG_HOST', 'localhost')
PYCHARM_DEBUG_PORT = int(os.environ.get('PYCHARM_DEBUG_PORT', '51235')) # Check PyCharm config for actual port

print(f"Attempting to connect to PyCharm debugger at {PYCHARM_DEBUG_HOST}:{PYCHARM_DEBUG_PORT}...")

try:
    # suspend=False means the program will not wait for the debugger connection at this point.
    # settrace() will attempt to connect and continue execution if it fails.
    settrace(
        host=PYCHARM_DEBUG_HOST,
        port=PYCHARM_DEBUG_PORT,
        suspend=False, # Set to True if you want execution to pause until debugger connects
        stdoutToServer=True,
        stderrToServer=True
    )
    print("Debugger connection established (or attempted).")
except Exception as e:
    print(f"Failed to connect to debugger: {e}")

# Your application logic continues here
def my_function():
    message = "This line can be a breakpoint in PyCharm."
    print(message)
    return message

if __name__ == '__main__':
    my_function()

view raw JSON →