Running python code just before blender exits

Hi,
I was wondering if there is any way I can make Blender executes some python code just before it exits ?
In my case I want to disable an addon, such that blender does not try to enable it the next time it runs.
I tried using atexit.register() of course, but I get an error that makes me think Blender has already deallocated everything, so I have no more access to bpy.ops.preferences.addon_disable().
I have been thinking of using a handler (https://docs.blender.org/api/current/bpy.app.handlers.html) but none seems to fit my need.

Thanks for your answers.

1 Like

There isn’t a convenient way to do this besides atexit.


You can enable an add-on without enabling it in the preferences.

e.g. this keeps the add-on for the current session w/o modifying the preferences.

addon_utils.enable("some_addon", default_set=False, persistent=True)

Nice at least it solves my problem :slight_smile: (next time I’ll read the doc), thanks !

1 Like