how to debug exec('code_string')?

I need to refactor a legacy application (Python3.7).
It contains a dynamic code import mechanism which gets strings to be executed.

Is there a way to debug into such a string? i.e. can I set a breakpoint?

Upload id: 2022_06_13_oUBNUgw7sTQT2QZWKrrfun (file:

# get a string containing python code and execute it:

def other_function_to_call(name):
print(f'other_function_to_call: {name}') # Press Strg+F8 to toggle the breakpoint.
def more_to_execute():
print(f'more_to_execute() ..') # Press Strg+F8 to toggle the breakpoint.

# can I set a breakpoint in this code?
code_string = '''
other_function_to_call('hello from local string')

if __name__ == '__main__':
print(f'text to execute:\n>>>{code_string}<<<')
exec (code_string)

Comment actions Permalink

u1550 to set a breakpoint you simply click the mouse to the left of the line of code in the column and a red dot should appear. Here is a video of what I 'm talking about, Jetbrains has a ton of these lately they have been pumping out more content.

Comment actions Permalink

andrew ,

you may have missed that my code is not in a file, but in a string.

See example above: variable 'code_string' contains the code I want to debug. 



Please sign in to leave a comment.