Search code examples
pythonpackageresource-cleanup

Is there some way to cleanup a Python Package?


Python Packages have explicit means to initialize when imported (the "mandatory" __init__.py script); is there some way to obtain the reverse (i.e.: define a script to be run whenever Package is unloaded, either explicitly or because interpreter terminates)?

My current use would be:

I have a simple server which receives commands through a named pipe, so first thing __init__.py does is to create such a pipe (along with other initializations). I would like to "automagically" delete the file when Package is teared down, for whatever reason (as far as possible).


Solution

  • you can use ateexit module

    https://docs.python.org/2/library/atexit.html

    register your cleanup function in the __init__ file