simple composition use of a a blender module/menu class

I am taking a stab at python development in blender, but the execution of a simple concept is defeating me. I want to simply use composition and packages in my .py development. So instead of a single py file with a bunch of classes within it (messy) I want to place my classes in a separate py file (module) within a sub package; However I can’t seem to get this simple concept to work.

my package structure I have my Initial file I also have a directory named menu, and two files within that: and



In my top level I have the following code:

if "bpy" in locals():
    import imp
    import menu.__my_menu__

import bpy

def menu(self, context):, icon="PLUGIN")
def register():

def unregister():


Within the menu directory, file I have the following:

import bpy

class MyMenu(bpy.types.Menu):

    bl_idname = "OBJECT_MT_my_menu"
    bl_label = "My Menu"
    def draw(self, context):
        layout = self.layout
        layout.operator_context = 'INVOKE_REGION_WIN'

I get the following error

Import Error: No module named menu.__my_menu__


shouldn’t it be more like this?


menu will be the module

i don’t think there is a need for a top-level

you may have a look at the structure of my export addon:

inside the menu subfolder your will have to have a import __my_menu

files are not automagically imported

thus why you get that error.

please bare in mind that using double underscore in front of any name is used by python to note “privacy” that the user should not access that feature, but other than that double score is both ugly and meaningless and not recommended as general python practice. As matter of fact that “privacy” is really fake.

Thank you kilon, I am quite new to python and assumed double underscore was a naming convention for python modules, glad to hear it is not, and you are right, it is quite ugly I will fix directly. I will give placing an import my_menu_ within my a try.

Also I figured out I have to place a a import from add_on_directory_name import fileName for blender to to see my addon. Is this correct?

Thank again.

in case of it is, that is the module it actually import when you do an import menu the same way init() of your class is executed every time you create a new instance of your class.

if you talk about your top level it depends

do you really need to import , of course not, after all that module is what you already execute
the only thing that would make sense is

import menu

remember takes always the name of the folder it belongs too

now if you don’t like doing something like


and instead you prefer doing

mymenu = MyMenu()

then your import should be

from menu import *

or if you like to import only MyMenu

from menu import MyMenu

if you don’t like the name of MyMenu then you could

from menu import MyMenu as menu

so you can do

menu = menu()

python offers you loads and loads of flexibility and I just barely scratched the surface.

all the above implies that your menu contains a from my_menu import * or something similar to what i described above