help with exec statement

Jason Morgan jwm-python at
Fri Jan 3 04:55:43 CET 2003

I am trying to pull dynamic code from database and execute it. I have
done this in the past with files; however, the same method is not working
and I keep getting syntax errors back.

I pass a string (coming out of a MySQL db) to this function:

def dyncode(code, name):
	import imp
	module = imp.new_module(name)
	exec code in module.__dict__
	return module

This is the contents of the string:

def generate():
        return "TEST"

And this is the code that executes generate():

dynModule = dyncode('string','test')
dynCode = dynModule.generate()

And, finally, this is my error:

File "<string>", line 2

    return "TEST"


SyntaxError: invalid syntax

Now, I've the same this with open files objects before, and that works
fine, but no matter what I try, I can't seem to get this to work.

Any help would be greatly appreciated.


More information about the Python-list mailing list