PyCharm Debugger (pydevd-pycharm)
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
- gotcha Most users do not need to install `pydevd-pycharm` manually via pip. PyCharm bundles and manages its own version internally. Manual installation is primarily for remote debugging scenarios where the target environment (e.g., a server, Docker container) does not have PyCharm installed.
- breaking The internal communication protocol and API parameters for `settrace` can change between major PyCharm/IDEA releases. An older `pydevd-pycharm` client might not be fully compatible with a newer PyCharm IDE, and vice-versa, leading to debugging issues or failures to connect.
- gotcha Remote debugging requires careful network configuration. Firewall rules, network access control lists (ACLs), or incorrect IP addresses/ports can prevent the debugger client from connecting to the PyCharm IDE. Common issues include 'Connection refused' or 'Timeout' errors.
- gotcha Running code under a debugger, especially with `stdoutToServer=True` or `stderrToServer=True`, can introduce noticeable performance overhead. This is generally expected for debugging tools but can be significant in performance-critical applications.
Install
-
pip install pydevd-pycharm
Imports
- settrace
from pydevd_pycharm.pydevd import settrace
Quickstart
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()