package/namespace confusion

Bruce Edge bedge at troikanetworks.com
Mon Mar 5 14:49:38 EST 2001


Hi,
I'm sure this is a real rookie issue here, but I can't seem to define a
global then access it in a package.
I'm using __init__.py to define glob_var, then load util.py.

I would have expected util/ufunc() to have access to glob_var.


test/__init__.py:

glob_var=0        <- is this where I should stick package globals?
__all__=["util"]


test/util.py:

def ufunc():
	global glob_var
	print "glob_var =", glob_var
	glob_var=2
	print "glob_var =", glob_var



>>> from test import *
>>> glob_var 
0
>>> util.ufunc()
glob_var =
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "test/util.py", line 3, in ufunc
    print "glob_var =", glob_var
NameError: There is no variable named 'glob_var'
>>> dir()
['__builtins__', '__doc__', '__name__', 'atexit', 'glob_var',
'histfile', 'os', 'readline', 'rlcompleter', 'savehist', 'sys', 'util']



More information about the Python-list mailing list