importing / loading a module / class dynamically
gandalf at designaproduct.biz
Fri Jan 5 17:54:50 CET 2007
> hg wrote:
>> I have the following problem.
>> I find in a directory hierarchy some files following a certain sets of
>> each file will in turn have a class with the same name as the filename
>> (minus .py)
>> I fetch those names in a list of string and want to import the files /
>> instantiate the classes.
>> I block at the beginning and tried this (test.py is a real file)
>>>>> s = 'test.py'
>>>>> eval ('import ' + s)
>> and get
>> Traceback (most recent call last):
>> File "<pyshell#1>", line 1, in -toplevel-
>> eval ('import ' + s)
>> File "<string>", line 1
>> import test.py
>> Any clue ?
> OK, from http://mail.python.org/pipermail/python-list/2004-July/272081.html,
> I need to use exec and not eval
Well, you can also use the 'imp' module. You should read this:
More information about the Python-list