# How do I import python modules from external files?

As my Addon started growing, I decided to split the ui into different files. I have several subfolders with modules that I want to register und unregister in my main script. I tried to manage this with

from folder.subfolder import module1
from folder.subfolder import module2

...

def register():
bpy.utils.register_module(__name__)
bpy.utils.register_module(module1)
bpy.utils.register_module(module2)

def unregister():
bpy.utils.unregister_module(__name__)
bpy.utils.unregister_module(module1)
bpy.utils.unregister_module(module2)


Inside module1.py, it looks something like this:

import bpy

class MyPanel(bpy.types.Panel):
bl_label = "MyPanel"
bl_idname = "...."
bl_space_type = "...."
bl_region_type = "...."

def draw(self, context):
....


However, it always prints out this error message:

        Traceback (most recent call last):
tils.py", line 350, in enable
mod.register()
File "C:/path/to/main/script.py", line 2388, in register
bpy.utils.register_module(module1)
File "C:\Program Files\Blender Foundation\Blender\2.79\scripts\modules\bpy\uti
ls\__init__.py", line 685, in register_module
raise Exception("register_module(%r): defines no classes" % module)
Exception: register_module(<module 'module1' from 'C:/path/to/module1.py'>): defines no classes


What do I need to change in order to be able to register and unregister external modules properly?

• Animation nodes, has a register() in each submodule, the addons main register() calls the register for each submodule. Look here – sambler May 23 '18 at 5:07
• I see, but if you reload Animation Nodes by pressing F8, it prints the same error message (...defines no classes). I try to circumvent this behaviour – J. Doe May 23 '18 at 11:44
• It is common to use importlib.reload see this example. I expect that for a complex setup as above you should be able to call unregister() and then register() for each submodule when bpy is already in locals. – sambler May 24 '18 at 0:41