Search code examples
debuggingpycharm

Is there a flag I can check in my code to see if PyCharm's debugger is running?


I'd like to take some actions in my code, conditioned on whether the PyCharm debugger is attached and running — e.g., I've launched my code using the IDE's "Debug" command; something like

if pycharm_debugger_is_running:
    do_something()
else:
    do_another_thing()

Is there a way to do that?


Solution

  • Since Pycharm debugger has been merged with Pydev's, you may like this answer.

    Edit

    In order to determine whether the script was launched by Pycharm, I can think of manually adding your own environment variable in the Run/Debug Configurations

    environ pycharm

    And then check for it:

    if 'PYCHARM' in os.environ:
        print("running in Pycharm")
    

    Actually the most convenient way would probably be to directly edit the default run configuration to automatically integrate that flag in any new run.

    default run config pycharm