Python exec statement
December 9, 2009
The exec statement in Python can be used to run code sourced from somewhere at runtime. However, it's important that the line endings are \n, not \r\n.
I was loading code from a database at runtime, and I had a problem where running the script resulted in a Syntax Error. When the code is run manually, it's fine.
I found out that this was a problem by using repr(lMapping)
which shows what the line endings are.
I solved the problem using by changing the line endings after loading from the database:
lMapping = lMapping.replace('\r\n','\n') exec lMapping