Three python modules, calling one another
Question: I am working on a project where I have three python modules (`a.py`, `b.py`
and `c.py`).

Module `a` is calling module `b`, module `b` is calling module `c`, and module
`c` is calling module `a`. But the behaviour is very bizzare when it runs.

Here are my three modules:

**_a.py_**

    
    
    print('module a')
    def a() : 
        print('inside a')
        return True
    
    import b
    b.b()
    

**_b.py_**

    
    
    print('module b')
    
    def b() : 
        print('inside b')
        return True
    
    import c
    
    c.c()
    

**_c.py_**

    
    
    print('module c')
    
    def c() : 
        print('inside c')
        return True
    
    import a
    
    a.a()
    

When I run `a.py`, the output observed is :

    
    
    module a
    module b
    module c
    module a
    inside b
    inside a
    inside c
    inside b
    

Whereas the expected behavior is:

    
    
    module a
    module b
    module c
    module a
    inside b
    

Why does this happen? Is there an alternative way for such an implementation?


Answer: I think the key misunderstanding is that you don't expect all the modules to
run after their imports, but they do. They get interrupted mid script to do
another import but they will return to finish out the commands.

So what ends up happening is: (I'm removing the function declarations, just
for clarity)

    
    
    print('module a')
    import b
    >>> importing b
        print('module b')
        import c
        >>> importing c
            print('module c')
            import a
            >>> importing a
                print('module a')
                import b
                >>> Doesn't reimport b
                b.b()
            a.a()
        c.c()
    b.b()
    

So to just show the order of commands without the imports and nesting:

    
    
    print('module a')
    print('module b')
    print('module c')
    print('module a')
    b.b()
    a.a()
    c.c()
    b.b()
    

And this does match your actual output.

