function namespaces

M.E.Farmer mefjr75 at
Wed Mar 9 01:25:46 CET 2005

Hello Darren,
I am not sure why you are using execfile().
Py> help(execfile)
Help on built-in function execfile:

    execfile(filename[, globals[, locals]])

    Read and execute a Python script from a file.
    The globals and locals are dictionaries, defaulting to the current
    globals and locals.  If only globals is given, locals defaults to
Do you realize that execfile actually runs your script?

A simple import would probably be the best way to go.
#contents of
testvar = [1,2,3,4]

import myfile
print myfile.testvar

But to answer the question :
#contents of
testvar = [1,2,3,4]

# the problem is you are executing a script
# then searching in the wrong namespace.
def myfunction(filename):
        execfile(filename, globals())
        print testvar 

More information about the Python-list mailing list