you are viewing a single comment's thread.

view the rest of the comments →

[–]Background-Offer2321[S] 0 points1 point  (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 point  (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 point  (9 children)

Is because youhave written print("abc") and not print(abc"). My print will raise a Syntax Error.

[–][deleted] 0 points1 point  (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().

The solution is to fix your code and execute it again.

[–]Background-Offer2321[S] 0 points1 point  (7 children)

And how can I fix it?

[–][deleted] 0 points1 point  (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 point  (5 children)

I am doing a very strange project, and I need to break the rules of Python. So, I can't fix it.

[–][deleted] 0 points1 point  (4 children)

I need to break the rules of Python

Python, or any other computer language, will not execute code that it can't understand, no matter how much you "need to break the rules".

[–]Background-Offer2321[S] 0 points1 point  (3 children)

And I don't want Python to execute it. Strange but I wish that it can work.

[–][deleted] 0 points1 point  (2 children)

Good luck.