namespace & imported modules

Jason gaudette at ele.uri.edu
Wed Nov 24 00:58:12 EST 2004


Hi,

I've been having trouble understanding the difference between global
namespace within and between modules.  For example, I can program a
subthread to see a global name (a threaded event to be specific);
however, somehow when I break up my code into different modules, the
subthread can no longer see the global name in the main program.

Here's an example of what I'm trying to accomplish:

Working script pseudo-code:
======================================================
# main.py
---------------------------
import std modules

def thread1
def thread2

launch thread1: wait for event
launch thread2: signal thread1's event
======================================================
#END: Broken script pseudo-code 

and here's it's seemingly equivalent part...

#START: Broken script pseudo-code 
======================================================
# subfile.py:
---------------------------
import std modules

def thread1
def thread2

---------------------------
# main.py
import std modules
from subfile import thread1, thread2        # this should be the only
difference

launch thread1: wait for event
launch thread2: signal thread1's event
======================================================
#END: Broken script pseudo-code 

The broken code would yield the following exception when thread2 tries
to signal thread1's event:
"NameError: global name 'myName' is not defined"

I hope I didn't simplify the problem too much, but I didn't want to
paste 200 lines of code for no one to read.

Obviously I'm missing something regarding namespace and global scopes.
 Would anyone be able to point out what I've overlooked?  Any help is
appreciated.

-Jason



More information about the Python-list mailing list