if you want to run a script in a script, then the import is wrong; you can use exec if you only care about exceptions:
namespace = {} f = open("script.py", "r") code = f.read() try: exec code in namespace except Exception: print "bad code"
you can also compile code with
compile(code,'<string>','exec')
if you plan to execute the script more than once and execute the result in the namespace
or use a subprocess as described above if you need to capture the output generated by your script.
user237419
source share