* Alan Gauld <[EMAIL PROTECTED]> [050419 15:08]: > > Is it possible to dynamically compose a module name > > for import? Hi Alan: > Yes, there is a module somewhere that allows you to import a module > programatically by passing the name as a string. Look for an import > or module function or module in the docs... Yes. Since I posted this, I tried the following: >>> X = __import__("xml") >>> help(X) ## seems to work kind of like "import foo as f" > > Pointers to documentation or other discussions would > > be sufficient at this time. > > But this begs the question why? Apart from possible environment > differences, like importing a different module depending on OS > (which can be easier done via an if/else construct anyway) why > would you want to?
I'm just in the design phase of a large implementation where many modules - each having the same function and data member names - would be generated outmatically and a calling application would import such a module based on the name of the main application. This may not prove to be the pythonesque way of doing it, but am investigating python's particular features around this subject. Here's a little more detail; Let's say we have a series of executable scripts called form1.py, form2.py, form3.py and each import a function called 'setup()' from a common module called (!!) Common.py. Inside of the setup() function, a call to the cgi object determines the name of the calling application (form1,py ... etc.) and then imports another module (automatically generated from another process), always having the same members, composing that auto module name from the name of the calling application: form1.py => form1_extra.py...... And if I have to change the algorithm that determines the name of the module imported or the names of the members of the modules, I could just do it in two places: the setup() function and the application generating the auto modules (which isn't necessarily written in python). but like I said, I'm just thinking about this now, and am open to alternatives. Thanks Alan tim -- Tim Johnson <[EMAIL PROTECTED]> http://www.alaska-internet-solutions.com _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor