sys.exitfunc not working in python
I am trying to run following simple code
import sys
print("Starting Test Python Module");
def testmethod():
print("From test method")
sys.exitfunc = testmethod
print("Terminating Test Python Module");
and it prints
C:\Users\athakur\Softwares>python test.py
Starting Test Python Module
Terminating Test Python Module
I am not able to understand why it does not print "From Test method"
Using atexit works fine though
import atexit
print("Starting Test Python Module");
def testmethod():
print("From test method")
atexit.register(testmethod)
print("Terminating Test Python Module");
Outputs
C:\Users\athakur\Softwares>python test.py
Starting Test Python Module
Terminating Test Python Module
From test method
Asked by: Alfred135 | Posted: 06-12-2021
Answer 1
sys.exitfunc
is deprecated since python2.4 and was removed in python3.
Answer 2
sys.exitfunc
does not exist in Python 3.x.
Similar questions
python - How to skip sys.exitfunc when unhandled exceptions occur
As you can see, even after the program should have died it speaks from the grave. Is there a way to "deregister" the exitfunction in case of exceptions?
import atexit
def helloworld():
print("Hello World!")
atexit.register(helloworld)
raise Exception("Good bye cruel world!")
outputs
Traceback (most recent call last):
File "test.py", line 8, in <module>
ra...
python - matplotlib: Error in sys.exitfunc
I keep getting error in sys.exitfunc when working with matplotlib. For example, the following code throw it for matplotlib 1.3.0 / Python 2.7.3 / Ubuntu 12.04.3 LTS
from matplotlib.pyplot import figure, show
from numpy.random import random
fh = figure(figsize = (15, 10, ))
ax = fh.add_axes((.1, .1, .8, .8, ))
ax.scatter(random((100, )), random((100, )))
fh.show()
This yields
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python