Load post handler is run twice

I have a load_post handler to run some code at startup:

@persistent
def update_prefix(scene):
# some function

def register():


It works fine with the blender startup file, but when loading a file it runs twice and gives a PyContext error in the console:

How do I get it to only run once or am I missing something?

EDIT:

Even if I just add this to \startup

import bpy
from bpy.app.handlers import persistent

@persistent
def update_prefix(scene):
print("Hello World")

def register():


I still get:

Hello World
Hello World
• Try if update_prefix in bpy.app.handlers.load_post: return in your register() function. – dr. Sybren Jun 10 '18 at 9:31
• In addition, To make sure you are also clearing the previously appended handler when testing,.removing the functions via there names . [f for f in bpy.app.handlers.load_pre if f.__name__.startswith("update_prefix")] from the handlers ensures no "hangers on". Does "some function" involve context members? (It looks like you are calling an operator from a handler.. do you have two scenes?) When registering: context is restricted and some members are not available. Or in the case of running in headless mode from cli, UI members like screen and area will be None. – batFINGER Jun 10 '18 at 9:55