newbie question - object from a string?

Kragen Sitaker kragen at
Tue Nov 27 23:14:12 CET 2001

Gordon Scott <gordon.scott at> writes:
> I have a file named in which I have the class MyHandler.
> when I use
> __import__('MyHandlers', globals(), locals(), ['MyHandler'])  or
> .
> .
> .
> eval('MyHandler()', globals(), locals())  or eval('MyHandlers.MyHandler()',
> globals(), locals())
> I get a NameError: name 'MyHandler' is not defined.

__import__ doesn't import anything into your namespace; it just loads
and returns the module.  You can say:

MyHandler = __import__('MyHandlers').MyHandler

and then you can call MyHandler.


More information about the Python-list mailing list