24

Is it possible to have a script like the following in Python?

...
Pause
->
Wait for the user to execute some commands in the terminal (e.g.
  to print the value of a variable, to import a library, or whatever).
The script will keep waiting if the user does not input anything.
->
Continue execution of the remaining part of the script

Essentially the script gives the control to the Python command line interpreter temporarily, and resume after the user somehow finishes that part.


What I come up with (inspired by the answer) is something like the following:

x = 1

i_cmd = 1
while True:
  s = raw_input('Input [{0:d}] '.format(i_cmd))
  i_cmd += 1
  n = len(s)
  if n > 0 and s.lower() == 'break'[0:n]:
    break
  exec(s)

print 'x = ', x
print 'I am out of the loop.'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FJDU
  • 1,425
  • 2
  • 15
  • 21
  • This question is asking for a *REPL* ([Read-Eval-Print-Loop](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)). There are several suggestions on [a similar question](http://stackoverflow.com/questions/1395913/how-to-drop-into-repl-read-eval-print-loop-from-python-code), including `code.InteractiveInterpreter()` and `pdb`. – Possum Jan 31 '17 at 18:08

5 Answers5

40

if you are using Python 2.x: raw_input()

Python 3.x: input()

Example:

# Do some stuff in script
variable = raw_input('input something!: ')
# Do stuff with variable
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cameron Sparr
  • 3,925
  • 2
  • 22
  • 31
  • Thanks, but maybe I am not clear enough. What I want is, the script stop at some point (the position is specified when writing the script), then gives the control to the interpreter, so that the user can do anything he/she wants, for example, to print out something, to import some library, or even exit the whole script completely. This is common in some script languages for scientific computing (such as IDL), but I am not sure whether it is possible in python. I suppose the raw_input() function cannot do this? – FJDU Nov 21 '12 at 20:28
  • Well, maybe I can use exec() to execute the returned string of raw_input. – FJDU Nov 21 '12 at 20:36
  • hmmm I don't know of a way to give the user complete control of the interpreter in the middle of a script.... you might be able to get somewhat close to this by using the `eval()` function, and using this to execute whatever the user inputs, and then doing something tricky to keep it open. – Cameron Sparr Nov 21 '12 at 20:38
  • But `input` _does_ read and run python code!? Just stick it in a loop and check for 'quit'. http://docs.python.org/2/library/functions.html#input – John Mee Nov 21 '12 at 20:50
  • @JohnMee I checked that document and find that it used eval instead of exec, which means many commands cannot be executed. For example, I cannot "print x" or "print(x)". Anyway I think I need to wrap the input or raw_input in an infinite loop, and terminate the loop when the user inputs a string such as b[reak]. – FJDU Nov 21 '12 at 21:01
8

The best way I know to do this is to use the pdb debugger. So put

import pdb

at the top of your program, and then use

pdb.set_trace()

for your "pause".

At the (Pdb) prompt you can enter commands such as

(Pdb) print 'x = ', x

and you can also step through code, though that's not your goal here. When you are done simply type

(Pdb) c 

or any subset of the word 'continue', and the code will resume execution.

A nice easy introduction to the debugger as of Nov 2015 is at Debugging in Python, but there are of course many such sources if you google 'python debugger' or 'python pdb'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy Lorenz
  • 81
  • 1
  • 3
3

Waiting for user input to 'proceed':

The input function will indeed stop execution of the script until a user does something. Here's an example showing how execution may be manually continued after reviewing pre-determined variables of interest:

var1 = "Interesting value to see"
print("My variable of interest is {}".format(var1))
key_pressed = input('Press ENTER to continue: ')

Proceed after waiting pre-defined time:

Another case I find to be helpful is to put in a delay, so that I can read previous outputs and decide to Ctrl + C if I want the script to terminate at a nice point, but continue if I do nothing.

import time.sleep
var2 = "Some value I want to see"
print("My variable of interest is {}".format(var2))
print("Sleeping for 5 seconds")
time.sleep(5) # Delay for 5 seconds

Actual Debugger for executable Command Line:

Please see answers above on using pdb for stepping through code

Reference: Python’s time.sleep() – Pause, Stop, Wait or Sleep your Python Code

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kelton Temby
  • 825
  • 10
  • 20
  • 1
    This doesn't really answer the question. If you read the question carefully, OP would like to pause the script, allow for multiple REPL commands to be run, and then resume the script. This solution simply pauses the script. – Michael Kolber Jul 22 '19 at 22:02
  • @MichaelKolber after my initial post I updated the answer to reflect why I think my answer is also helpful (acknowledging that the PDB answer is the 'best' to the specific circumstance, but also that most people landing here, including myself, were actually looking for a nice quick way to pause/wait a python script, which the title of the question implies _IS_ the question!). This is supported by the most upvoted answer, which also does not 'answer' the OP circumstance, but also does not have detailed examples like mine provides. Cheers – Kelton Temby Jul 22 '19 at 22:11
  • Answer matches the title of OP's question (if not OP's use-case) and provided what I came looking for. – marianoju Nov 09 '21 at 10:48
2

I think you are looking for something like this:

import re

# Get user's name
name = raw_input("Please enter name: ")

# While name has incorrect characters
while re.search('[^a-zA-Z\n]',name):

    # Print out an error
    print("illegal name - Please use only letters")

    # Ask for the name again (if it's incorrect, while loop starts again)
    name = raw_input("Please enter name: ")
Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28
2

Simply use the input() function as follows:

# Code to be run before pause
input() # Waits for user to type any character and
        # press Enter or just press Enter twice

# Code to be run after pause
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131