Newbie question.. how to load modules with a dynamic name?

Sean 'Shaleh' Perry shaleh at valinux.com
Fri Feb 23 16:13:26 EST 2001


On 23-Feb-2001 Ron Scinta wrote:
> I have a need to run various python script files from a master control
>  script, however, I do not know the names of the scripts until runtime.
> 
>  What is the prefered method of doing this type of operation?
> 

>>> foo = 'sys'
>>> exec "import %s" % foo
>>> print sys.argv
['']
>>> dir(sys)      
['__doc__', '__name__', '__stderr__', '__stdin__', '__stdout__', 'argv',
'builtin_module_names', 'copyright', 'exc_info', 'exc_type', 'exec_prefix',
'executable', 'exit', 'getrefcount', 'hexversion', 'maxint', 'modules', 'path',
'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setprofile',
'settrace', 'stderr', 'stdin', 'stdout', 'version']





More information about the Python-list mailing list