1

Is there a similar substituite to .exit() and sys.exit() that stops the program from running but without terminating python entirely?

Here's something similar to what I want to achieve:

import random

my_num = random.uniform(0, 1)

if my_num > 0.9:
   # stop the code here

# some other huge blocks of codes

Here's why I think I need to find such a command/function:

  1. I want the code to run automatically so definitely not "Ctrl+C"

  2. I don't want python to terminate because I want to check other previously defined variables

  3. I think else does not work well because there will be a huge amount of other codes after the condition check and there will be other .py to be running by os.system()

  4. Of course, force triggering an error message like would do but is that the only way?

9
  • time.sleep(...)? Commented Aug 20, 2021 at 3:12
  • You want to check variable values... are you looking for a breakpoint for debugging? docs.python.org/3/library/pdb.html Commented Aug 20, 2021 at 3:22
  • @Zev thanks for the info! No, Im not trying to debug. Say, there is a txt file involved in the program, and the program means to read the txt file. If it finds certain characters in the file, then the program will alert it, open the txt, ask me to delete them, and of course stop running. After I delete the characters, I can re-run the code. Commented Aug 20, 2021 at 3:27
  • so like input("press y to continue") ? Commented Aug 20, 2021 at 3:28
  • @Zev I don't think so? If I use input, then the code is actually still running and it will now allow me to enter any other command like to check my other variable, right? Commented Aug 20, 2021 at 3:31

2 Answers 2

1

When you run your script, use the -i option. Then call sys.exit() where you want to stop.

python3 -i myscript.py
if my_num > 0.9:
    sys.exit()

Python won't actually exit when the -i used. It will instead place you in the REPL prompt.


The next best method, if you can't use the -i option, is to enter an emulated REPL provided by the code module.

import sys
import code
import random
import readline


while True:
    my_num = random.uniform(0, 1)
    if my_num > 0.9:
        console = code.InteractiveConsole(globals())
        console.interact(banner="You are now in Python REPL. ^D exits.",
                         exitmsg="Bye!")
        break

That will start a REPL that is not the built-in one, but one written in Python itself.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Keith! That's very helpful!! Is it possible to achieve something similar after entering python without "-i"?
@GrumpyCivet I extended the answer with an alternative method to get a REPL using a Python implementation of it that you can run in your Python code.
1

If you don't want to terminate the code, you can tell python to "sleep":

import random
import time

my_num = random.uniform(0, 1)

if my_num > 0.9: 
    time.sleep(50) #==== 50 seconds. Use any number.

4 Comments

Thanks @Sujay! If I understand correctly, this does not stop but pause the program for a period time, right?
@GrumpyCivet Yep. It tells python to stay idle for the number of seconds provided. Then it will continue its job
thank you again! This is not particularly what I am looking for. In this case, if I'm using windows command prompt, I woulnd't be able to enter any other new command, right?
@GrumpyCivet Yes, you can use os.system(). You cannot manually enter the command unless you quit python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.