By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,353 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,353 IT Pros & Developers. It's quick & easy.

Re: how can I use exec with main module globals?

P: n/a
En Sat, 12 Jul 2008 16:15:36 -0300, Akathorn Greyhat <ak******@gmail.com>
escribi�:
Hello, this is my first message in the group.
Welcome!
I'm spanish so my english sometimes is really bad, sorry =(

I have a problem and I hope someone has the answer.

I'm trying to make an in-game python idle, it works great but the exec
statement can't access the main module globals but only the ones that are
inside the module in wich I defined the function.

def execute(self, string):
exec(string, globals(), globals())

I realized that if I move the code to the main module, It works, but I
want
it to be inside another script.
You have to pass in the namespace of the desired module - instead of
globals. I'd use an explicit argument:

def execute(self, string, namespace):
# exec string in namespace # 2.5
exec(string, namespace) # 3.0

Use it this way:

execute("a=1", target_module.__dict__)

then target_module.a will be 1

--
Gabriel Genellina

Jul 15 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.