Can import a user-specified module?

Erik Max Francis max at alcyone.com
Wed Aug 14 03:32:46 CEST 2002


Sung Kim wrote:

> In Python, is something like the following pseudo-code possible? Thank
> you!
> 
> moduleName = GetNameFromUser ()
> 
> import moduleName
> 
> if moduleName.SomeFunction exists
>         x = moduleName.SomeFunction ()

Try the __import__ function:

	__import__(moduleName)
	if moduleName.__dict__.has_key(functionName):
	    moduleName[functionName]()

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list