Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unexpected (by me) exec behavior

Thread Tools

Unexpected (by me) exec behavior

Mark Wright
Posts: n/a
I have a script that I use to control our build process. It is a
general purpose script that exec's other scripts that contain project
specific python code. In one of those other, project-specific,
scripts I exec a third script. That third script is failing because
it can't seem to 'import' successfully. It seems that if one 'exec's
a string that in turn 'exec's another string, the 'import's don't work
in the second string. I'm assuming that I'm misunderstanding
something about Python namespaces, but here's an example that
illustrates the problem:

# filename =
s1 = """
s2 = \"\"\"
import socket
def xyz():
print socket.gethostbyname('somehost')
if __name__ == '__main__':

def abc():
exec s2
if __name__ == '__main__':

exec s1

Traceback (most recent call last):
File "", line 24, in ?
exec s1
File "<string>", line 13, in ?
File "<string>", line 11, in abc
File "<string>", line 6, in ?
File "<string>", line 4, in xyz
NameError: global name 'socket' is not defined

Can anyone explain this to me?

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Net::SSH.exec Using the "exec" method interactively Guillermo Riojas Ruby 0 11-26-2010 05:17 PM
Runtime.exec(String[]) Doesn't Always Work, bBut Runtime.exec(String) Does Hal Vaughan Java 11 05-22-2006 04:49 PM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM
Backup Exec 9.1: The Backup Exec job engine system service is not responding Christian Falch Computer Support 1 06-23-2004 02:22 AM