简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM