Recently, I tried to simulate the way Python runs programs, but I was worried that there was a gotcha. It turns out there is one: when imp.load_module loads a Python source file, it compiles it to bytecode, and saves it as a file.
Python uses a very simplistic way of naming the compiled file: it adds a 'c' to the end of the source file name. This works well when the file is "foo.py" and the compiled file is "foo.pyc". But if you try to run a script in this way, the file may not have a .py extension. So for example, if the python file is named "zzz", then load_module will create a compiled file named "zzzc". This is bad.
There was no reasonable way to prevent this compiled file creation, so I went back to the execfile technique, instead setting up more of the proper sys structures to get the proper execution context. If you're interested in the details, the code is in execfile.py, with tests in test_execfile.py.
BTW: This was basically Fredrik's advice from the first post, and he was right. Marius suggested I look at runpy in the standard library, and it's similar to what I'm trying to do here, but it takes a module name rather than a file, so it isn't quite right.