Question about using from/import from with a class

John Lenton jlenton at gmail.com
Tue Jul 27 19:42:19 CEST 2004


On 27 Jul 2004 10:32:41 -0700, John Taylor <john_taylor_1973 at yahoo.com> wrote:
> I have a class that has this code in it:
> 
> def __init__(self,Site):
>     self.Site = Site
>     import_cmd = "from regexpr_%d import *" % ( int(self.Site.id) )
>     exec( import_cmd )
>     print AGENT # fails, NameError: global name 'AGENT' is not defined
> 
> The regexpr_1.py file, for example, has the line:
> AGENT="Mozilla"
> 
> After the exec() command is ran, how to I access varaibles defined in
> the regexpr_%d file?

don't do it this way. Try this instead:

   regexpr = __import__('regexpr_%d' % int(self.Site.id))
   print regexpr.AGENT

-- 
John Lenton (jlenton at gmail.com) -- Random fortune:
bash: fortune: command not found



More information about the Python-list mailing list