Changing behaviour of namespaces - solved, I think

Mikael Olofsson mikael at
Thu Sep 21 15:34:18 CEST 2006

Peter Otten wrote:
> To feed an arbitrary mapping object to execfile() you need to upgrade to
> Python 2.4.

Thanks! As clear as possible. I will do that.

FYI: I think I managed to achieve what I want in Py2.3 using the 
compiler module:

    def getNamesFromAstNode(node,varSet):
        if node.__class__ in (compiler.ast.Global,compiler.ast.Import):
        if node.__class__ in (compiler.ast.Name,):
        for subNode in node.getChildNodes():

    # Get all variable names that are referenced in the file:
    varSet = sets.Set()
    mainNode = compiler.parseFile(filePath)

    # Define a namespace and update it:
    ns = {}
    for varName in varSet:
        ns[varName] = dummyObject

    # Execute the file without NameErrors:

Batteries included!


More information about the Python-list mailing list