Greenfire
Greenfire

Reputation: 103

Python - Pause program and send commands possible?

Is there a way to pause a Python program and be able to send commands from the prompt during the pause before resuming the code ?

The only way I found to do a pause is to ask for a keyboard entry (like press enter for example) but you can't type commands while it's waiting for you to press enter. Is there a way to really pause the code? have access to all variables and possibly modify them in the prompt before resuming?

Upvotes: 3

Views: 2472

Answers (1)

Christian Aichinger
Christian Aichinger

Reputation: 7247

If you want to use this for debugging, check out pdb, the Python debugger. You can start you script under pdb, set a breakpoint on the line you want, and then run your script.

python -m pdb script.py
b 15                       # <-- Set breakpoint on line 15
c                          # "continue" -> run your program
# will break on line 15

You can then inspect your variables and call functions. Since Python 3.2, you can also use the interact command inside pdb to get a regular Python shell at the current execution point!

You can also include the following line directly in your program to stop at that line end drop into pdb:

import pdb; pdb.set_trace()

Then you won't even have to use -m pdb on the commandline.

Upvotes: 3

Related Questions