6

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

asked Aug 5, 2010 at 19:20
2
  • 3
    That script sounds very, very painful. Commented Aug 5, 2010 at 19:23
  • 15
    ... yo dawg ... knowyourmeme.com/memes/xzibit-yo-dawg Commented Aug 5, 2010 at 19:28

7 Answers 7

15

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.

Wayne Werner
52.3k35 gold badges213 silver badges305 bronze badges
answered Aug 5, 2010 at 19:49
Sign up to request clarification or add additional context in comments.

2 Comments

Yes; check the input before eval'ing. One suggestion: can you use less ALLCAPS and use e.g. bold formatting (or no formatting at all)? It's somewhat hard to read the 'spaghetti post' with allcaps thrown in.
sorry for the "spaghetti post" I have gotten much better in later messages with formatting
7
answered Aug 5, 2010 at 19:24

Comments

1

May want to look at the statement exec: http://docs.python.org/release/2.5.2/ref/exec.html

S.Lott
393k83 gold badges521 silver badges791 bronze badges
answered Aug 5, 2010 at 19:22

Comments

1

If you want to execute the script within the context of the main script, you might want to check eval [ http://docs.python.org/py3k/library/functions.html#eval ]

answered Aug 5, 2010 at 19:35

Comments

0

Not sure how wise this is but isn't the exec function what you use if you need to execute Python code?

answered Aug 5, 2010 at 19:24

Comments

0

There is precedence for what you are trying to do. The collections.namedtuple function builds a template string which is passed to exec in order to build a dynamically defined class.

answered Aug 5, 2010 at 19:53

Comments

0

As of Python version 3.11, execfile (which was the most convenient function) is not listed as built-in function anymore.

@George Lambert answer is the way to go. I'll add just some code for quick reference.

Run script within script

To run the script "run_me.py" within the script "main.py" do this:

  • run_me.py be like
print("hello_world")
  • main.py be like
with open("run_me.py", "r") as fl:
 exec(fl.read())

Run code contained in a string

To run the code contained in a string us eval:

  • main.py be like
string_w_py_code = "print('hello world')"
eval(string_w_py_code)
answered Nov 22, 2022 at 11:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.