import os, impdef valid_python_suffix(file): for suffix in [i[0] for i in imp.get_suffixes()]: if file.endswith(suffix): return True return Falsedef module_name(file): for suffix in [i[0] for i in imp.get_suffixes()]: if file.endswith(suffix): return file[:-len(suffix)].split(os.path.sep)[-1] return ''def execute_file(file): if not file or not valid_python_suffix(file): raise Exception('Not a python file argument given') path = os.path.abspath(file) dirname = os.path.dirname(path) modulename = module_name(path) info = imp.find_module(modulename, [dirname, ]) module = imp.load_module(modulename , *info)def main(): execute_file('print.py')if __name__ == '__main__': main()