ERROR: search for unknown operator

Not really a question more like a little solution to a curious gotcha.
In a script that defines keymap shortcuts I was getting
“search for unknown operator”
errors that occurred when loading my script.
After much mucking around I noticed something that once you know it, is totally obvious.
The issue was simply when you define a shortcut it must be done after the relevant class that it is calling has been defined so change this


addon_keymaps = []

def register():
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name='Window', space_type='EMPTY' ,region_type='WINDOW')
    kmi = km.keymap_items.new("ui.show_shortcuts", 'F1', 'PRESS', oskey=True)
    addon_keymaps.append(km)
    bpy.utils.register_module(__name__)


to this


addon_keymaps = []

def register():
    bpy.utils.register_module(__name__)
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name='Window', space_type='EMPTY' ,region_type='WINDOW')
    kmi = km.keymap_items.new("ui.show_shortcuts", 'F1', 'PRESS', oskey=True)
    addon_keymaps.append(km)


and the problem is fixed.
Simple and obvious now.
Just a tip I thought I would share to save someone else the time

bpy classes such as operators need to be registered before than can be accessed in panel layouts, keymaps etc. - that’s right.

Thanks for posting this, will help new scripts for sure!

One thing though, is that when the addon is removed the message will show.
This is not a problem it’s (checked this with ideasman42)
sought of like Blender is just saying You have uninstalled the op and I can’t find it.
So if you get that it doesn’t matter, it’s just Blender being friendly :eyebrowlift:

hm… what if you put
bpy.utils.unregister_module(name)

last in unregister()?

Thats the funny thing it doesn’t seem to matter. I thought that at first and have tried it both ways.
But it only happens when you uninstall the addon.
It doesn’t happen when you close Blender
The register one happens all the time when it’s the wrong way around.
But it doesn’t affect the functioning of the code
Blender is obviously checking it twice (like Santa :eyebrowlift2:)
I didn’t try a refresh but I guess that would throw the error also

Anyway this thread should be easy enough to find and save someone else trying to work this out