473,785 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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.

Jul 18 '05 #1
4 8761
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.selec t("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.selec t("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
18404
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 sub_2.noway # prints 'no extern' :-( # EOF
46
3525
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 by pointer in C++, is there such way in Python? Thansk in advance. J.R.
12
2406
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 some cases there is a further complication: module importing through an indirect mechanism, like: exec "from " + xxx + " import *". A part the fact that I have not understood the "real" difference between import and from ... import (or also from......
4
2668
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 piece which holds the programs reference: globals, functions, classes, modules etc. The objects of this file (functions and classes) are directly accessible; 'import suchModule' s objects are attainable through the *qualified name*...
5
11071
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) modHandler.rptOpen (Me.Report.Name), (Me.Tag) End Sub
4
11990
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 - containing file paths, Excel worksheet names, etc - I'm not sure how to pass these around, or if I'd be better off saving things to a table and using a recordset. My guess is a recordset would be slower. For example, should I do something like...
9
5015
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 something like this, but it did not work, gave core dump. class myclass { public: myclass(){}; ~myclass(){};
2
1037
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 parameters from optparse to the workers in two ways: (1) creating a Globals.py module, set parameters once in the cli code and read it when needed in the worker class methods. Something like this: import Globals
12
11111
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.