473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:
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
4 8729
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: M-a-S | last post by:
I'd like to parametrize a module. That is, to set and pass some values into the module, while it is being imported. I tried this: # sub_1.py -- main program extern = "OK" import sub_2 print...
46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
4
by: jean-marc | last post by:
As an application programmer, I'm not well versed in the material aspects of computing (memory, cpu, bus and all). My understanding of imports in Python is such: the __main__ program is the center...
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
4
by: deko | last post by:
This is a basic program flow question. I'm trying to refractor an AC2000 app and split sections of code into separate modules. But there are a number of collections I create in one big module -...
9
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried...
2
by: Tim Arnold | last post by:
Hi, I'm writing a command-line interface using optparse. The cli takes several options with a single action and several parameters to be used in the resulting worker classes. I've been passing...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.