importlib allows you to import a module with the module name being variable.
#!/usr/bin/env python import os, sys import importlib def main(): fName = '/online_dir/fioAdds.py' dirName = os.path.dirname( fName) baseName = os.path.basename( fName) if baseName.find( '.py') > 0: prefixName = baseName.rpartition('.')[0] if dirName not in sys.path: sys.path.insert( 0, dirName) mod = importlib.import_module( prefixName) res = mod.main() print( res) if __name__ == "__main__": main()