Python isn't really set up for what you're doing. The exec'd code can too easily mess around with the calling code, if it's malicious. You're best off just running the student's program in a separate process and capturing the output.