473,473 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4605
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Filip Dreger | last post by:
Each function has a func_code property that is suposed to contain the pure bytecode of the function. All the context (including reference to relevant namespaces) is stored in different fields of...
4
by: Oracle 9Ri2 AS 2.1 IA 64 | last post by:
Hello, I am working with Oracle 9Ri2 version on Linux AS 2.1 for IA64. I have a problem when i try to lanch the following program : EXEC SQL PREPARE MyStmt FROM SELECT CHAMP1 FROM TAB1...
1
by: Steve Thorpe | last post by:
Hi. I have two sql servers and have ran exec sp_addlinkedserver 'ACSPSM', N'SQL Server' to link one to the other and also vise versa. Each server has two users permissioned. My problem is...
1
by: Matik | last post by:
Hello to all, Maybe first small introduction: - SQLServer 2000 SP3, - XP Pro EN, - ActiveX, - SP in database It should working like this. There is a instanse of an object working, which...
3
by: Cesco | last post by:
Hallo to everybody. I have a DTS in SQL Server 2000 and I need to execute it from stored procedure. I know that there are various method fot does this but they doesn't work. The first method that...
2
by: Vaap | last post by:
I did lot of googling to see if I can solve the SQL server not found problem while trying to run ASP.Net community starter kit from an XP machine to Windows 2003 server hosting SQL server 2000...
2
by: peleme | last post by:
Hi, Here is a code example to visualize my problem. ---------------------------------------------------------------------------------- import thread import threading from time import sleep ...
5
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
2
by: chets | last post by:
Hi All, I am facing problem in executing one dynamic query in PRO *C program on linux. I want to update table mytable by data MADURAI for a column mycolumn1 where primary key is myPK.I want to...
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.