Question about using from/import from with a class

Discussion in 'Python' started by John Taylor, Jul 27, 2004.

  1. John Taylor

    John Taylor Guest

    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?

    Thanks,
    John
    John Taylor, Jul 27, 2004
    #1
    1. Advertising

  2. John Taylor

    John Lenton Guest

    On 27 Jul 2004 10:32:41 -0700, John Taylor <> 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 () -- Random fortune:
    bash: fortune: command not found
    John Lenton, Jul 27, 2004
    #2
    1. Advertising

  3. John Taylor

    John Lenton Guest

    On 27 Jul 2004 10:32:41 -0700, John Taylor <> 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 () -- Random fortune:
    bash: fortune: command not found
    John Lenton, Jul 27, 2004
    #3
  4. John Taylor

    John Lenton Guest

    On 27 Jul 2004 10:32:41 -0700, John Taylor <> 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 () -- Random fortune:
    bash: fortune: command not found
    John Lenton, Jul 27, 2004
    #4
  5. John Taylor

    Peter Otten Guest

    John Taylor 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"


    It may be bad style but it _does_ work here. I would verify that

    [faked:]

    >>> import regexpr_1
    >>> regexpr_1.AGENT

    'Mozilla'

    in the interpreter doesn't also raise the NameError. If so, check
    int(self.Site.id).

    Peter
    Peter Otten, Jul 27, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    900
    Paul Opal
    Oct 11, 2004
  2. E11
    Replies:
    1
    Views:
    4,686
    Thomas Weidenfeller
    Oct 12, 2005
  3. Vitali Gontsharuk
    Replies:
    2
    Views:
    528
    Vitali Gontsharuk
    Aug 25, 2005
  4. jd
    Replies:
    3
    Views:
    334
  5. Artur M. Piwko

    import/from import question

    Artur M. Piwko, Jun 29, 2003, in forum: Python
    Replies:
    1
    Views:
    811
    Peter Hansen
    Jul 2, 2003
Loading...

Share This Page