user1801279
user1801279

Reputation: 1783

how to use module = __import__(filename) and invoke the main function

I am trying to develop this program , that invokes the main function of another program and takes the program to be inkoved's name as user input , let us call this program 1 :

Program 1 # takes the program 2's name as user input

try:

    print "Please input the file you want to test"
    filename = raw_input().split(".")[0]
    module = __import__(filename)

except:

program 2 is like

def main():
    first()

def first():
    5/0

if __name__ == "__main__":
    main()

so basically I want to know how to invoke program 2's main function from program 1 .

thank you

Upvotes: 0

Views: 156

Answers (1)

YXD
YXD

Reputation: 32511

Just do module.main() - there's nothing special about a function called main

Upvotes: 1

Related Questions