Runtime import with with multiple subdirectories in python
Question: what is the best way to perform this type of import in python

file to be imported which is available in location one/ne_one/one_two/"

fielname : two.py

    
    
    def foo():
        print "venkatttt!"
    

main file : main.py

    
    
    s = __import__("one.one_one.one_two.two", fromlist=[])
    function_class =  getattr(s,"one_one")
    function_class1 =  getattr(function_class,"one_two")
    function_class2 =  getattr(function_class1,"two")
    print s
    print function_class
    print function_class1
    print function_class2
    function_class2.foo()
    

output of this code:

    
    
    <module 'one' from '/opt/auto/src/ex/one/__init__.pyc'>
    <module 'one.one_one' from '/opt/auto/src/ex/one/one_one/__init__.pyc'>
    <module 'one.one_one.one_two' from '/opt/auto/src/ex/one/one_one/one_two/__init__.pyc'>
    <module 'one.one_one.one_two.two' from '/opt/auto/src/ex/one/one_one/one_two/two.py'>
    venkatttt!
    

i am looking out for the best way to perform this import


Answer: From your output, I can see you already have `__init__.py` files in each
subdirectory, therefore, you can simply `import` them:

    
    
    $> from one.one_one.one_two.two import foo
    $> foo()
    

If you want a handle for each module, you can import them separately:

    
    
    $> import one.one_one as function_class
    $> import one.one_one.one_two as function_class1
    $> import one.one_one.one_two.two as function_class2
    

Finally, you can also define `__all__` in `one/__init__.py` and let this auto-
imports happen automatically when `import one` is executed.

