execfile(), import and namespaces

Nikolas Kauer nkauer at students.wisc.edu
Fri Jul 2 11:53:43 EDT 1999


We use a modification of "Listing One" of Andrew Kuchling's "A CGI Framework
in Python"
article in Web Techniques (February 1998) for our dynamic Web site.  We just
made our
Web site public and wanted to switch from files with Python source (under
/htdocs) to compiled files with Python object code (i.e. python -c 'import
file.py' -> file.pyc) as suggested in CGI FAQ 1.8, mainly to make it harder
to (download and) re-use our code.  The object files would be loaded as
modules with 'import'.

Problem: In the following code snipet environment variables are passed to
the
executed Python file via the corresponding global namespace argument of the
execfile() function; whereas the 'import' statement does not allow for such
arguments.

namespace = {
             'headers': headers,
             'webvars': webvars,
             'environ': os.environ
            }

[ more code ]

try:
    execfile(filename, namespace)
except:
    # The page's code raised some sort of exception.

Does anyone know of an elegant way to pass namespace objects to modules in a
similar
way execfile() does?

Thanks,
Nikolas

PS I'm not on the mailing list, please reply directly.






More information about the Python-list mailing list