importing / loading a module / class dynamically
laurent rahuel
toto at titi.de
Fri Jan 5 16:23:34 EST 2007
Hi,
Using exec or eval ISN'T what should be done ever. When you have
troubles importing you should :
- Add some repository to your sys.path
and/or
- Use the buildin import method
and/or
- Use Mr Eby's Importing module (http://python.org/pypi/Importing)
Regards,
Laurent
hg a écrit :
> Hi,
>
> I have the following problem.
>
> I find in a directory hierarchy some files following a certain sets of
> rules:
>
> .../.../../plugin/name1/name1.py
> ....
> .../.../../plugin/namen/namen.py
>
> 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 ?
>
> Thanks
>
> hg
>
More information about the Python-list
mailing list