Enter Interactive Mode In Python

后端 未结 7 812
日久生厌
日久生厌 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 07:05

    You have options -- Python standard library or IPython.

    The Python standard library has a code module which has an InteractiveConsole class whose purpose is to "Closely emulate the behavior of the interactive Python interpreter." This would probably be able to do what you want, but the documentation doesn't have any examples on how to use this, and I don't have any suggestions on where to go.

    IPython, which is a more advanced Python terminal, has the option to embed a console at any point in your program built in. According to their documentation, you can simply do

    from IPython import embed
    
    for thing in set_of_things:
      embed()
      do_stuff_to(thing)
    

提交回复
热议问题