How to pass parameter when importing a module?

B

Bo Peng

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:

From what I have read, from .... does not take any parameter. Maybe I
should use environmental variables?

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
------------
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.keys():
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.
 
S

Swaroop C H

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 *

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

=?ISO-8859-1?Q?Andr=E9_Roberge?=

Bo said:
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:
How about this:
#===== constants.py
lib = whatever #assigned somewhere else

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

etc.

André
 
S

Serge Orlov

Bo said:
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:

[snip]

Take a look at wxPython versioning:
http://wiki.wxpython.org/index.cgi/MultiVersionInstalls

The most simple usage looks like

import wxversion
wxversion.select("2.4")
import wx
Serge.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top