import module from package - confusing syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David MacQuigg

    import module from package - confusing syntax

    I'm setting up a large hierarchy of module packages and using a
    variable to select which of many alternative packages to import. For
    example, the variable 'config.modsel' points to a particular "model
    selector" package. 'config' is a module containing many such
    variables.

    My first attempt resulted in a confusing error message:
    [color=blue][color=green][color=darkred]
    >>> from config.modsel.m odelselector import ModelSelector[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#2 5>", line 1, in -toplevel-
    from config.modsel.m odelselector import ModelSelector
    ImportError: No module named modsel.modelsel ector

    It works if I enter the literal path rather than a variable:
    [color=blue][color=green][color=darkred]
    >>> config.modsel[/color][/color][/color]
    <module 'libs.modsel01' ... >[color=blue][color=green][color=darkred]
    >>> from libs.modsel01.m odelselector import ModelSelector
    >>> ModelSelector[/color][/color][/color]
    <class libs.modsel01.m odelselector.Mo delSelector at 0x009F68D0>

    I need to use a variable, however. The best I can come up with is:
    [color=blue][color=green][color=darkred]
    >>> exec('from '+ config.modsel._ _name__ +'.modelselecto r import ModelSelector')
    >>>[/color][/color][/color]

    Is this the best we can do with current Python syntax?

    Should we think about suggesting a better syntax? I think the
    fundamental problem is the confusion that arises from using '.' as
    both a path separator and an attribute qualifier.

    -- Dave

  • Mark McEahern

    #2
    Re: import module from package - confusing syntax

    On Sat, 2004-07-03 at 08:21, David MacQuigg wrote:[color=blue]
    > I'm setting up a large hierarchy of module packages and using a
    > variable to select which of many alternative packages to import.[/color]

    This means you need __import__.

    // m


    Comment

    • David MacQuigg

      #3
      Re: import module from package - confusing syntax

      On Sat, 03 Jul 2004 08:54:50 -0500, Mark McEahern
      <marklists@mcea hern.com> wrote:
      [color=blue]
      >On Sat, 2004-07-03 at 08:21, David MacQuigg wrote:[color=green]
      >> I'm setting up a large hierarchy of module packages and using a
      >> variable to select which of many alternative packages to import.[/color]
      >
      >This means you need __import__.[/color]

      __import__ looks even messier than exec() We have to interpolate the
      string 'config.modsel' twice.

      import libs, config
      config.modsel = 'modsel01'

      ## # What we want:
      ## from (config.modsel) .modelselector import ModelSelector

      ## # Works, but messy:
      ## __import__('lib s.%s.modelselec tor' % config.modsel)
      ## modsel = getattr(libs, config.modsel)
      ## ModelSelector = modsel.modelsel ector.ModelSele ctor

      # Still seems like the lesser evil:
      exec('from libs.%s.modelse lector import ModelSelector' %
      config.modsel)

      The __import__ function looks like a real kludge. There is a
      suggestion in section 2.1 of the Library Reference to use the 'imp'
      module, and write my own import function. I guess I'll look at that
      next.

      -- Dave

      Comment

      Working...