By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

How to pass parameter when importing a module?

P: n/a
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:
lib = 'standard'
from myModule import * # actually import myModule_std
From what I have read, from .... does not take any parameter. Maybe I
should use environmental variables?
os.putenv('lib', 'standard')
from myModule import *
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
------------ import param
param.setParam('lib','standard')
from myModule import *


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.

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sun, 20 Mar 2005 10:18:14 -0600, Bo Peng <bp***@rice.edu> wrote:
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.
--
Swaroop C H
Blog: http://www.swaroopch.info
Book: http://www.byteofpython.info
Jul 18 '05 #2

P: n/a
Bo Peng wrote:
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é

Jul 18 '05 #3

P: n/a
Bo Peng wrote:
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:
>>> lib = 'standard'
>>> from myModule import * # actually import myModule_std


[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.
Jul 18 '05 #4

P: n/a

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.


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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.