470,593 Members | 2,494 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,593 developers. It's quick & easy.

Problem with exec

I'm trying to parallise with python. Specifically I'm sending code to
the processes and let them exec this code (in ascii form). However I
ran into a problem with Namespaces (I think) which I do not understand.

Here's what I do first:
---------------------------------------
bash-3.2$ cat execBug.py
#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
exec header
print "f() =",f()

bash-3.2$ ./execBug.py
f() = 0.633306324515
it est: it works.
However when I do this:

bash-3.2$ cat execBug2.py
#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
def g():
exec header
return f()
print "g() =",g()
bash-3.2$ ./execBug2.py
g() =
Traceback (most recent call last):
File "./execBug2.py", line 10, in <module>
print "g() =",g()
File "./execBug2.py", line 9, in g
return f()
File "<string>", line 4, in f
NameError: global name 'randn' is not defined
bash-3.2$ ???

I get this global name error. I can fix it with adding some line like
"global randn" but I don't want to do this. I want to do exactly what
I wrote: Import the function scipy.randn in the local namespace of the
function "g" so that "return f()" makes sense. Can anybody help me out?
Yours Justus
Mar 13 '08 #1
2 4360
On Mar 14, 9:47 am, Justus Schwabedal <justus.schwabe...@gmx.de>
wrote:
[snipped]
However when I do this:

bash-3.2$ cat execBug2.py
#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
def g():
exec header
return f()
print "g() =",g()
bash-3.2$ ./execBug2.py
g() =
Traceback (most recent call last):
File "./execBug2.py", line 10, in <module>
print "g() =",g()
File "./execBug2.py", line 9, in g
return f()
File "<string>", line 4, in f
NameError: global name 'randn' is not defined
bash-3.2$ ???

I get this global name error. I can fix it with adding some line like
"global randn" but I don't want to do this. I want to do exactly what I wrote: Import the function scipy.randn in the local namespace of the

function "g" so that "return f()" makes sense. Can anybody help me out?
Yours Justus
Maybe using an explicit namespace is good enough for your needs:

#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
def g():
n = {}
exec header in n
return n['f']()
print "g() =",g()
(i don't understand the issues, but I can cut-and-paste with the best
of them)
Mar 14 '08 #2

On Mar 14, 2008, at 4:41 AM, al******@gmail.com wrote:
On Mar 14, 9:47 am, Justus Schwabedal <justus.schwabe...@gmx.de>
wrote:
[snipped]
>However when I do this:

bash-3.2$ cat execBug2.py
#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
def g():
exec header
return f()
print "g() =",g()
bash-3.2$ ./execBug2.py
g() =
Traceback (most recent call last):
File "./execBug2.py", line 10, in <module>
print "g() =",g()
File "./execBug2.py", line 9, in g
return f()
File "<string>", line 4, in f
NameError: global name 'randn' is not defined
bash-3.2$ ???

I get this global name error. I can fix it with adding some line like
"global randn" but I don't want to do this. I want to do exactly
what I wrote: Import the function scipy.randn in the local
namespace of the

function "g" so that "return f()" makes sense. Can anybody help me
out?
Yours Justus

Maybe using an explicit namespace is good enough for your needs:

#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
def g():
n = {}
exec header in n
return n['f']()
print "g() =",g()
(i don't understand the issues, but I can cut-and-paste with the best
of them)
--
http://mail.python.org/mailman/listinfo/python-list
That's what I was looking for: It's probably even a faster solution
than declaring all the imports global, isn't it?

Mar 14 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Filip Dreger | last post: by
4 posts views Thread by Oracle 9Ri2 AS 2.1 IA 64 | last post: by
1 post views Thread by Steve Thorpe | last post: by
2 posts views Thread by peleme | last post: by
5 posts views Thread by TPJ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.