In python how can I call a function using an imported module
<p>I have this module that calls main() function:</p>

<pre><code>## This is mymodules ##
    def restart():
        r = input('Do you want to build another configuration file?\n1. Yes\n2. No\n')
        if r == '1':
            main()
        elif r == '2':
            os.system('pause')
</code></pre>

<p>The main() is in another script that loads this module. However when it calls it says main() is not defined.  essentially this is what I have in my test:</p>

<pre><code>import mymodules as my
def main():
    print('good')

my.restart()
</code></pre>

<p>When this runs I want the my.restart() to be able to call the main() defined.</p>

<p>For a code as simple as this one you could simply pass the <code>main</code> function as an argument to the restart function.</p>

<p>E.g.</p>

<pre><code>def restart(function):
    r = input('Do you want to build another configuration file?\n1. Yes\n2. No\n')
    if r == '1':
        function()
    elif r == '2':
        os.system('pause')
</code></pre>

<p>And:</p>

<pre><code>import mymodules as my
def main():
    print('good')

my.restart(main)
</code></pre>

<p>This is a popular design pattern known as a <a href="http://stackoverflow.com/a/4690014/3424550">callback</a></p>

<p>However, this only works in simple examples like this. If you are writing something more complex you probably want to use objects and pass the objects instead. That way you will be able to call all multiple methods/functions from a single object.</p>

python function import module