'Pausing the Python console

I am running some Python code in the console, inside a very long for loop. I would like to pause the execution of the script, and then be able to resume where I left. Is it possible to pause the Python console?

Note: I don't want to use time.sleep; I want to be able to externally pause the console in the middle of a loop.



Solution 1:[1]

If you are using Unix you can always Ctrl+Z, to go back to the command prompt and then 'fg' to get back to the python console. On Windows use the Pause button

On Unix you can also do:

To stop: kill -SIGSTOP pid

To continue: kill -SIGCONT pid

Solution 2:[2]

If you know before runtime exactly when you will need to pause, just take a look at the pdb module.

If you do not know, I suggest you insert some code which checks for the existance of a certain file at every iteration and, if it exists, calls pdb. Performance, of course, will suffer. You can then create that file when you want to pause.

(file existance is simply an arbitrary condition which is easy-to-implement. You can choose others.)

Solution 3:[3]

i think what your looking for is a command that will pause the code until you tell him to continue like hitting the Enter key

try putting

input()

where you want to "pause" the code

Solution 4:[4]

On my laptop running windows 7 < ctrl + s > pauses execution.

Solution 5:[5]

Win 10, python 3.5.1 shell: F10 toggles pause and run.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jkysam
Solution 2 Emilio M Bumachar
Solution 3 Roberto Vasquez
Solution 4 Octipi
Solution 5 redthumb