Rahul
Rahul

Reputation: 11669

how to input python code in run time and execute it?

Well i want to input a python function as an input in run time and execute that part of code 'n' no of times. For example using tkinter i create a textbox where the user writes the function and submits it , also mentioning how many times it wants to be executed. My program should be able to run that function as many times as mentioned by the user.

Ps: i did think of an alternative method where the user can write the program in a file and then i can simply execute it as python filename as a system cmd inside my python program , but i dont want it that way.

Upvotes: 1

Views: 9414

Answers (3)

S.Lott
S.Lott

Reputation: 391818

That's what execfile() is for.

http://docs.python.org/library/functions.html#execfile

  1. Create a temporary file.

  2. Write the content of the textbox into the file.

  3. Close.

  4. Execfile.

  5. Delete when done.

Upvotes: 2

Piotr Duda
Piotr Duda

Reputation: 1815

Python provides number of ways to do this using function calls: - eval() - exec()

For your needs you should read about exec.

Upvotes: 2

underdark
underdark

Reputation: 1166

Would IPython do?

Doc: http://ipython.scipy.org/moin/Documentation

Upvotes: 0

Related Questions