473,796 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ 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 4645
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
2197
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 the function object. Since 'exec' is able to execute any string or bytecode in the current scope, it would seem possible to execute code of any function in any namespace. But no matter how I tried, I could not do it. There must be something I am...
4
13815
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 WHERE CHAMP2="4" AND CHAMP3 = "C" FOR UPDATE;
1
11785
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 when ever I try to do something that does a remote write I get the follow error message Microsoft OLE DB Provider for SQL Server error '80040e14'
1
1544
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 recieves "telegramms"
3
10634
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 I try to use is with the stored procedure "xp_cmdshell". If I write in DOS prompt dtsrun /F c:\DTS1.dts the DTS1 will execute well
2
3216
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 database. Tried all possible combinations but it still fails. I have Windows 2003 server having SQL Server 2000 installed with SP2. The installation went Ok on a XP professional machine and I was able to create database and user logins etc on...
2
1337
by: peleme | last post by:
Hi, Here is a code example to visualize my problem. ---------------------------------------------------------------------------------- import thread import threading from time import sleep def a():
5
2438
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
4674
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 use EXECUTE IMMEDIATE with context.But it is not working. Following is my piece of code. Please help. Thanks in advance. int main() {
27
5156
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 contents ]; close $fileID
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.