How to get two modules loaded into the same namespace?

r.vanwees at chello.nl r.vanwees at chello.nl
Thu Nov 8 17:06:40 CET 2001


I am trying to build a rule engine. I have two Python modules: 
RuleEngine.py and utilities.py.

I want to keep the engine and the utility functions separated for 
reasons of maintainiability and cleannes.

I am running into a few problems:

1) In RuleEngine.py I have defined a global, DEBUG_PRINT_LEVEL, which 
allows me to steer the amount of debug info I get at runtime 
(curently somewhere between 'nothing' and 'an awful lot').

When I try the following in RuleEngine.py:

    global DEBUG_PRINT_LEVEL
    DEBUG_PRINT_LEVEL = 0
    from utilities import *

    class RuleEngine:
        someUtilityFuncThatReferencesDEBUG_PRINT_LEVEL()

I get a NameError on DEBUG_PRINT_LEVEL (see stack trace below):

    File "D:\Dev\PyCharGen\utilities.py", line 120, in calcLevel
    if DEBUG_PRINT_LEVEL >= 4:
    NameError: global name 'DEBUG_PRINT_LEVEL' is not defined
    Process terminated with exit code 0

As far as I understand the 
docs, the 'from x import *' syntax should import stuff into the 
current namespace, but apparently this does not hold for globals.

2) Several functions in utilities.py refer back to functions in  
RuleEngine.py. They also return a NameError (see stacktrace below 
from an actual debug log)

    File "D:\Dev\PyCharGen\utilities.py", line 157, in calcSpells
    statLevel = find(stat, 'base') + find(stat, 'bonus')
    NameError: global name 'find' is not defined
    Process terminated with exit code 0

Again, it seems that the two modules are not in the same namespace.

Both python 2.1.1 and 2.2beta show this behavior.

Does anyone know a solution (other than putting the utility functions 
in RuleEngine.py) to this problem? I must admit that I got lost at 
the Python namespace rules before...

Kind regards,

Rob van Wees







More information about the Python-list mailing list