Exec and namespaces

Aquarius aquarius at kryogenix.org
Tue Jul 24 14:03:51 CEST 2001

I've got a CGI which reads a file and then execs its contents. The file
defines a function, and that function can't find itself to call
recursively; it gets a NameError.


[ aquarius at giles ] /home/httpd/kryogenix.org/current/web $ cat foo.cgi
#! /usr/bin/python

import sys
sys.stderr = sys.stdout

print "Content-type: text/html\n\n"

def main():
    fp = open("foo.txt")


[ aquarius at giles ] /home/httpd/kryogenix.org/current/web $ cat foo.txt

def bar(quux=None):
    if quux == "test":
        print "Hello, world!"

[ aquarius at giles ] /home/httpd/kryogenix.org/current/web $ python foo.cgi
Content-type: text/html

Traceback (innermost last):
  File "foo.cgi", line 13, in ?
  File "foo.cgi", line 11, in main
  File "<string>", line 8, in ?
  File "<string>", line 6, in bar
NameError: bar

My question here is: is there anything I can do about this? I assume
that you can't call bar() from inside bar() because it only knows about
two namespaces, global (which main() is in) and the "inside-bar()"
namespace, and bar() itself is in neither. I've tried this with both
1.5.2 and 2.0.1 and there doesn't appear to be any different (aside
from the wording of the NameError changing to "There is no variable
named 'bar'" under 2.0.1, which I assume is the same thing).

Aq -- I wish I understood namespaces

"The grand plan that is Aquarius proceeds apace"
            -- Frank Miller, "Ronin"

More information about the Python-list mailing list