Python call a package submodule with variable module name
Question: How can I call a common submodule (present in various modules), selecting the
right module from a value?

Example:

Let's say I have this folder structure:

    
    
    myprogram/
              myprogram.py          #main program
              colors/               #colors package
                     __init__.py    #contains __all__
                     blue/          #blue module
                          paint.py  #submodule
                     red/           #red module
                          paint.py  #submodule
    

The paint submodule has the same name in every module, but different code in
each.

In myprogram.py I want to do something like this:

    
    
    import colors   #import all the colors modules
    
    my_shape = circle()   
    my_color = "blue"     #define the module to call by his name
    if my_color in colors:
      colors.my_color.paint(my_shape)    #calls the submodule from the right mosule
    

I want the "colors" package to be scalable, so I can easy remove one color
from one deploy with the minimum effort..

(I can make a single paint.py module with a case of inside, but it's not
easily scalable)

The questions are:

  * Is it possible to do the `if my_module in package` thing?
  * If so, haw can I call a package's module using a variable `package.my_module_variable.function()`?

I'm trying to solve this [storing functions into
dicts](http://programmers.stackexchange.com/questions/182093/why-store-a-
function-inside-a-python-dictionary), am I in the right way?


Answer: You should be able to treat the color submodules as attributes. The submodules
will need `__init__.py` files in their directorys (ex. `blue/__init__.py`)
along with the `paint.py` module. Then you could do something like this:

    
    
    my_color = "blue"
    if hasattr(colors, my_color):
        getattr(colors, my_color).paint(my_shape)
    

Note however that in your example `paint` is a module so you wouldn't be able
to call the module. If there is a `paint` function in the `paint` module then
you would do:

    
    
    getattr(colors, my_color).paint.paint(my_shape)
    

**Edit** : I should mention, like others have, that this seems overkill for
the example you've shown. If your real code/situation is more complex than
this then go for it. Otherwise a nice alternative might be to have one paint
module with dictionaries for various operations. So a file like
`colors/paint.py` could contain:

    
    
    def paint_blue(shape):
        print("BLUE!")
    
    def paint_red(shape):
        print("RED!")
    
    paint_funcs = {
        "blue": paint_blue,
        "red": paint_red,
    }
    
    def paint(color, shape):
        return paint_funcs[color](shape)
    

**Edit 2** :

The `__init__.py` files inside the color directories are needed to treat them
as subpackages, otherwise you can't import them. My initial method of using
attributes like this won't work unless the main package knows about the
submodules. So for example:

    
    
    import colors
    # colors doesn't know it has submodules 'blue' and 'red'
    from colors import *
    # if the colors __init__ has an __all__ declaration now it does know about them
    hasattr(colors, "blue") # will work
    

Or you can import the colors in the colors `__init__.py` module

    
    
    # in colors/__init__.py
    import blue
    import red
    

Or you can do what others have said about dynamic imports.

