Mailing List Archive

execfile(), import and namespaces
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.
execfile(), import and namespaces [ In reply to ]
From: "Nikolas Kauer" <nkauer@students.wisc.edu>

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.
execfile(), import and namespaces [ In reply to ]
From: "Gordon McMillan" <gmcm@hypernet.com>

Nikolas Kauer wrote:

> 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. [...] 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.

But __import__ does. Check the library docs under built-in-functions.

- Gordon
execfile(), import and namespaces [ In reply to ]
Nikolas Kauer wrote:

> 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. [...] 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.

But __import__ does. Check the library docs under built-in-functions.

- Gordon