Search code examples
pythonsysshutdown-hookatexit

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

Solution

  • sys.exitfunc does not exist in Python 3.x.