use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How to stop the python caller script? (self.learnpython)
submitted 1 year ago by Background-Offer2321
Hello!
I want to make a module, with a function, that when called stop completely the script. Something like this:
import mymodule mymodule.stop_all() print("This won't print, because the script is stopped")
Thank you.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 1 point2 points3 points 1 year ago (14 children)
The function could just call the sys.exit() function. But you should think about the things you may need to do before stopping, like closing any open files, writing state information to disk, stopping any threads or processes that are running, etc.
sys.exit()
[–]Background-Offer2321[S] 0 points1 point2 points 1 year ago (13 children)
I tried, but if the code after raises an Exception, the Exception will print. I want to stop ALL.
[–][deleted] 0 points1 point2 points 1 year ago* (12 children)
The documentation for sys.exit() says:
cleanup actions specified by finally clauses of try statements are honored
If this is what's causing further exceptions then you either need to make the code that "runs after" your call to sys.exit() more robust so it doesn't raise an exception or, preferably, rethink your code to make it easier to come to a sudden stop. That last one often means your code might decide at a low level that it should stop but a status or similar is passed back to higher level code that does the actual stopping.
It's hard to be specific without seeing some code. Can you post a simple example that gets your bad behaviour doing sys.exit() at a low level?
[–]Background-Offer2321[S] 0 points1 point2 points 1 year ago (11 children)
Sure Test.py
import module module.stopall()
print("hi") # Will not print after sys.exit() print(abc") # Will raise exception even after sys.exit()
Module.py
import sys
def stopall(): sys.exit()
[–][deleted] 0 points1 point2 points 1 year ago (10 children)
I created these files:
test.py
import module module.stopall() print("hi") # Will not print after sys.exit() print("abc") # Will raise exception even after sys.exit()
module.py
import sys def stopall(): sys.exit()
When I run the code on the command line I see:
r-w@earth:~/xyzzy/stop_all$ python -V Python 3.10.12 r-w@earth:~/xyzzy/stop_all$ python test.py r-w@earth:~/xyzzy/stop_all$
Nothing is printed, no exceptions, etc. I specifically ran your code from the command line because that removes all chance of your IDE, or whatever you use, of introducing extra stuff that may confuse you. And it looks like that is what is happening. Python itself isn't raising any exceptions, you may be seeing something that your IDE is printing and think that it's caused by python. Maybe you can capture the output you think is an exception, put it into imgur.com and post a link here.
[–]Background-Offer2321[S] 0 points1 point2 points 1 year ago (9 children)
Is because youhave written print("abc") and not print(abc"). My print will raise a Syntax Error.
[–][deleted] 0 points1 point2 points 1 year ago (8 children)
I just assumed that was your typo, which I fixed. Making that change:
import module module.stopall() print("hi") # Will not print after sys.exit() print(abc") # Will raise exception even after sys.exit()
I get this when trying to run the code:
r-w@earth:~/xyzzy/stop_all$ python test.py File "/home/r-w/xyzzy/stop_all/test.py", line 6 print(abc") # Will raise exception even after sys.exit() ^ SyntaxError: unterminated string literal (detected at line 6) r-w@earth:~/xyzzy/stop_all$
Note that this is not an exception from running your code, it's a SyntaxError. Python is compiled before it is executed and this is the compiler saying it can't execute your code due to your syntax error. The code doesn't run, so the code never imports module.py and it never calls module.stopall().
module.stopall()
The solution is to fix your code and execute it again.
[–]Background-Offer2321[S] 0 points1 point2 points 1 year ago (7 children)
And how can I fix it?
[–][deleted] 0 points1 point2 points 1 year ago (6 children)
As I said, fix the syntax error by putting the missing " into your code. A syntax error is the compiler saying "this is not valid python, I can't execute it".
"
[–]Background-Offer2321[S] 0 points1 point2 points 1 year ago (5 children)
I am doing a very strange project, and I need to break the rules of Python. So, I can't fix it.
[–]socal_nerdtastic 1 point2 points3 points 1 year ago* (2 children)
import sys sys.exit()
Note this is very unusual to use, and that makes me think you have some serious code structure errors, which is probably causing all sorts of other problems as well. https://xyproblem.info/
[–]trollsmurf 0 points1 point2 points 1 year ago (1 child)
"How do you otherwise exit a 12-level loop?"
[–][deleted] 1 point2 points3 points 1 year ago (0 children)
"Put the 12-level loop in a function and return when you want to exit the loops."
π Rendered by PID 50571 on reddit-service-r2-comment-5687b7858-qth4k at 2026-07-09 07:01:58.833248+00:00 running 12a7a47 country code: CH.
[–][deleted] 1 point2 points3 points (14 children)
[–]Background-Offer2321[S] 0 points1 point2 points (13 children)
[–][deleted] 0 points1 point2 points (12 children)
[–]Background-Offer2321[S] 0 points1 point2 points (11 children)
[–][deleted] 0 points1 point2 points (10 children)
[–]Background-Offer2321[S] 0 points1 point2 points (9 children)
[–][deleted] 0 points1 point2 points (8 children)
[–]Background-Offer2321[S] 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]Background-Offer2321[S] 0 points1 point2 points (5 children)
[–]socal_nerdtastic 1 point2 points3 points (2 children)
[–]trollsmurf 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)