How to pass parameter when importing a module?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bo Peng

    #1

    How to pass parameter when importing a module?

    Dear list,

    What I would like to do is something like:

    In myModule.py ( a wrapper module for different versions of the module),

    if lib == 'standard':
    from myModule_std import *
    elsif lib == 'optimized'
    from myModule_op import *

    but I do not know how to pass variable lib to myModule.py to achieve the
    following effect:
    [color=blue][color=green][color=darkred]
    >>> lib = 'standard'
    >>> from myModule import * # actually import myModule_std[/color][/color][/color]

    From what I have read, from .... does not take any parameter. Maybe I
    should use environmental variables?
    [color=blue][color=green][color=darkred]
    >>> os.putenv('lib' , 'standard')
    >>> from myModule import *[/color][/color][/color]

    myModule.py
    -------------
    import os
    lib = os.getenv('lib' )
    ...


    or use a separate module?

    param.py
    ---------

    para = {}
    def setParam(key, val):
    para[key] = val

    main session
    ------------[color=blue][color=green][color=darkred]
    >>> import param
    >>> param.setParam( 'lib','standard ')
    >>> from myModule import *[/color][/color][/color]

    in myModule.py
    --------------
    from param import para
    try:
    lib = para['lib']
    except:
    lib = 'standard'
    ...

    Is there an established approach for this problem?

    Many thanks in davance.
    Bo

    =============== =============== =============== =============== =============== ====
    FULL STORY:

    I have several modules all (SWIG) wrapped from the same C++ source code
    but with different compiling flags. For example,

    myModule_std (standard module)
    myModule_op (optimized, without error checking)
    ...

    These modules are put directly under /.../site-packages . To load a
    module, I use, for example

    from myModule_op import *

    This works fine until I need to write some helper functions for
    myModule_?? in another module myHelper.py since I do not know which
    myModule is being used

    from myModule?? import A,B

    I find one solution

    # find out which module is being used
    import sys
    if 'myModule_op' in sys.modules.key s():
    from myModule_op import A,B
    else:
    from myModule_std import A,B

    but not completely satisfied. Therefore, I am writing a 'wrapper' module
    myModule that can load one of the myModule_?? modules according to user
    supplied info.

  • Swaroop C H

    #2
    Re: How to pass parameter when importing a module?

    On Sun, 20 Mar 2005 10:18:14 -0600, Bo Peng <bpeng@rice.edu > wrote:[color=blue]
    > What I would like to do is something like:
    > In myModule.py ( a wrapper module for different versions of the module),
    > if lib == 'standard':
    > from myModule_std import *
    > elsif lib == 'optimized'
    > from myModule_op import *[/color]

    Suggestion: Maybe you use builtin `__import__` to load a module ? Note
    that in this way, you'll have to use the module name prefix.


    --
    Swaroop C H
    Blog: http://www.swaroopch.info
    Book: http://www.byteofpython.info

    Comment

    • André Roberge

      #3
      Re: How to pass parameter when importing a module?

      Bo Peng wrote:[color=blue]
      > Dear list,
      >
      > What I would like to do is something like:
      >
      > In myModule.py ( a wrapper module for different versions of the module),
      >
      > if lib == 'standard':
      > from myModule_std import *
      > elsif lib == 'optimized'
      > from myModule_op import *
      >
      > but I do not know how to pass variable lib to myModule.py to achieve the
      > following effect:
      >[/color]
      How about this:
      #===== constants.py
      lib = whatever #assigned somewhere else

      #===== myModule.py
      from constants import lib

      etc.

      André

      Comment

      • Serge Orlov

        #4
        Re: How to pass parameter when importing a module?

        Bo Peng wrote:[color=blue]
        > Dear list,
        >
        > What I would like to do is something like:
        >
        > In myModule.py ( a wrapper module for different versions of the
        > module),
        > if lib == 'standard':
        > from myModule_std import *
        > elsif lib == 'optimized'
        > from myModule_op import *
        >
        > but I do not know how to pass variable lib to myModule.py to achieve
        > the following effect:
        >[color=green][color=darkred]
        > >>> lib = 'standard'
        > >>> from myModule import * # actually import myModule_std[/color][/color][/color]

        [snip]

        Take a look at wxPython versioning:


        The most simple usage looks like

        import wxversion
        wxversion.selec t("2.4")
        import wx
        Serge.


        Comment

        • Bo Peng

          #5
          Re: How to pass parameter when importing a module?

          [color=blue]
          >
          > Take a look at wxPython versioning:
          > http://wiki.wxpython.org/index.cgi/MultiVersionInstalls
          >
          > The most simple usage looks like
          >
          > import wxversion
          > wxversion.selec t("2.4")
          > import wx
          > Serge.
          >[/color]

          This is essentially my second method: using another module to set
          parameter for myModule. Since wxPython uses this method, I suppose this
          is the standard approach for this problem.

          Thanks.
          Bo

          Comment

          Working...