Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute python script inside a python script

Tags:

python

I have a scenario where i want to dynamically generate a python script - inside my main python script - store it as a string and then when need be, execute this dynamically generated script from my main script.

Is this possible, if so how?

thanks

like image 633
LoudNPossiblyWrong Avatar asked Aug 05 '10 19:08

LoudNPossiblyWrong


1 Answers

For a script in a file use exec

For a script in a string use eval

!!! But !!!

before you use strings passed in from an external source, sanity check them! Otherwise you expose the ability to execute arbitrary code from within you program, so range check your variables!

You do not ever want to be asking the question:

"excuse me mam, did you really name your son Robert'); DROP TABLE students; -- "?

If you dont understand the reference - see this quick cartoon...

http://xkcd.com/327/

but when you EVAL - you are taking full responsibility for the instructions that you are eval'ing.

like image 114
George Lambert Avatar answered Oct 13 '22 12:10

George Lambert