Hello,

I have an application written in python and Tk. My application is to be scriptable with python, i.e., the user gives a small piece of code written in python, and that is to be executed by my application in a different interpreter, with different set of local variables.

I know this is possible as I've seen the application Blender do something like this.

I think what I need for this is embedding python into python applications. But all I could find googling is embedding Python in C/C++.

Any help appreciated.
Thanks

please guys... any insight on this would be so good!

i am getting really frustrated on this.. I just want to run strings of python code using python.. is that sooo difficult...

sorry for my language, this problem is eating all my time :'(

Well if you're using Python 2.x there's the exec expression. I don't know if it still exists in Python 3.0, and I wouldn't normally use it, but it's all I can think of in this case. All you have to do is provide a string that's valid Python and it'll get executed. Like this:

code = "print 'Hello World!'"
exec code

You just need to be careful of quotes and properly escaping them inside the string that will be executed.
Hope that helps!

Thank you sooo much shadwickman!

I am using 2.6, not in the mood of typing print('hello') already :P

one more thing, can I pass a dictionary to exec containing the locals in which the code is to be executed?

thank you once again :)

I'm not entirely sure... Just have a look through the Python documentation! :P

Thank you for your help... I think I found it

it is exec code in globals, locals

Oh thanks! Now I learned something new too :D

EDIT:

exec code in globals(), locals()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.