Python interactive mode input

I am running my Python program and have a point at which it would be useful to jump over and see what happens and then exit again. It looks like a temporary console mode.

In Matlab, I would use the keyboard command, but I'm not sure if the command is in python.

Is there any way to do this?

For example:

 for thing in set_of_things: enter_interactive_mode_here() do_stuff_to(thing) 

When I enter_interactive_mode() , I would like to go there, look around, and then leave and continue the program.

+35
python interactive
Nov 17
source share
7 answers

code.interact() seems to work somehow:

 >>> import code >>> def foo(): ... a = 10 ... code.interact(local=locals()) ... return a ... >>> foo() Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> a 10 

Ctrl + Z returns to the "main" interpreter.

You can read the locals, but changing them doesn't work that way.

+35
Nov 17 '12 at 17:22
source share
 python -i myapp.py 

This will execute myapp.py and put you in an interactive shell. From there, you can perform functions and check their output by loading the entire environment (import, etc.) of myapp.py .

For something more complicated, it would be better to use a debugger like pdb , setting a breakpoint. In addition, most IDEs (PyDev, PyCharm, Komodo ...) have graphical debuggers.

+37
Nov 17 '12 at 17:07
source share

I use pdb for this purpose. I understand that Emil already mentioned this in his answer, but he did not include an example or did not specify why he 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 mark. You can also move up and down the stack ( u and d commands), which the InteractiveConsole does not have built-in mechanisms.

To keep the program running, use the c command. In the above example, it will inject a debugger into each iteration of the loop, so you can wrap the set_trace() call in an if clause.

+14
Nov 17 '12 at 18:02
source share

You have options - the standard Python or IPython library.

The Python standard library has a code module that has an InteractiveConsole whose purpose is to "closely emulate the behavior of the Python interactive interpreter." This will probably be able to do what you want, but the documentation does not have examples of how to use this, and I have no suggestions on where to go.

IPython , which is a more advanced Python terminal, has the ability to embed the console at any time in your firmware. According to their documentation, you can simply do

 from IPython import embed for thing in set_of_things: embed() do_stuff_to(thing) 
+8
Nov 17 '12 at 17:23
source share

The most convenient tool for me is ipdb .

ipdb exports functions for accessing the IPython debugger, which includes tab completion, syntax highlighting, better traces, better introspection with the same interface as the pdb module.

Completion and convenient introspection are especially useful for debugging.

+3
Nov 17 '12 at 19:20
source share

You can use ipdb .

To set breakpoints, add import ipdb; ipdb.set_trace() import ipdb; ipdb.set_trace() , where you want to go to the debugger. When you reach the breakpoint, you will be presented with an interactive shell and a few lines of code around your breakpoint for context.

https://www.safaribooksonline.com/blog/2014/11/18/intro-python-debugger/

+2
Apr 7 '16 at 12:40
source share

Although this is probably not the most suitable option, it is as simple as:

 try: while True: print input() except: pass 

Will cover many cases

0
Nov 17 '12 at 17:27
source share



All Articles