Enter Interactive Mode In Python

后端 未结 7 894
日久生厌
日久生厌 2021-01-30 06:42

I\'m running my Python program and have a point where it would be useful to jump in and see what\'s going on, and then step out again. Sort of like a temporary console mode.

7条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-30 06:56

    I use pdb for this purpose. I realize Emil already mentioned this in his answer, but he did not include an example or elaborate on why it answers your question.

    for thing in set_of_things:
        import pdb; pdb.set_trace()
        do_stuff_to(thing)
    

    You can read and set variables by starting your command with an exclamation point. You can also move up and down the stack (commands u and d), which InteractiveConsole does not have built-in mechanisms to do.

    To have the program continue executing, use the c command. In the above example it will enter the debugger every loop iteration, so you might want to wrap the set_trace() call in an if sentence.

提交回复
热议问题