I have multiple python files, each with different classes and methods in it. I want to execute all those files with a main function I have separately outside all of them.
As previous answers suggest, if you simply need to re-use the functionality do an import.
However, if you do not know the file names in advance, you would need to use a slightly different import construct. For a file called one.py
located in the same directory, use:
Contents of the one.py:
print "test"
def print_a():
print "aa"
Your main file:
if __name__ == "__main__":
imp = __import__("one")
print dir(imp)
Prints out test and also gives information about the methods contained in the imported file.