0

Possible Duplicate:
Dynamic loading of python modules
python: How to add property to a class dynamically?

I have a dictionary with the filename and class names how can I import this class names and how can I create this classes?

Example:

classNames = { 'MCTest':MCTestClass}

I want to import the MCTest and create the MCTestClass.

Community
  • 1
  • 1
Mokus
  • 10,174
  • 18
  • 80
  • 122
  • 1
    I don't understand at all what you're saying. Can you rephrase your question, maybe show some example of what you have in your dictionary, and what you want to have afterwards? What is this "filename"? – Tim Pietzcker Sep 02 '11 at 08:56
  • I assumed he has module and class names as strings and wants to instantiate the classes – tauran Sep 02 '11 at 09:00

2 Answers2

6

You have to use the __import__ function:

http://docs.python.org/library/functions.html#import

Example from doc page:

>>> import sys
>>> name = 'foo.bar.baz'
>>> __import__(name)
<module 'foo' from ...>
>>> baz = sys.modules[name]
>>> baz
<module 'foo.bar.baz' from ...>

To instantiate a class from baz you should be able to do:

>>> SomeClass = getattr(baz, 'SomeClass')
>>> obj = SomeClass()
tauran
  • 7,986
  • 6
  • 41
  • 48
1

From turbogears.util:

def load_class(dottedpath):
    """Load a class from a module in dotted-path notation.

    E.g.: load_class("package.module.class").

    Based on recipe 16.3 from Python Cookbook, 2ed., by Alex Martelli,
    Anna Martelli Ravenscroft, and David Ascher (O'Reilly Media, 2005)

    """
    assert dottedpath is not None, "dottedpath must not be None"
    splitted_path = dottedpath.split('.')
    modulename = '.'.join(splitted_path[:-1])
    classname = splitted_path[-1]
    try:
        try:
            module = __import__(modulename, globals(), locals(), [classname])
        except ValueError: # Py < 2.5
            if not modulename:
                module = __import__(__name__.split('.')[0],
                    globals(), locals(), [classname])
    except ImportError:
        # properly log the exception information and return None
        # to tell caller we did not succeed
        logging.exception('tg.utils: Could not import %s'
            ' because an exception occurred', dottedpath)
        return None
    try:
        return getattr(module, classname)
    except AttributeError:
        logging.exception('tg.utils: Could not import %s'
            ' because the class was not found', dottedpath)
        return None

use it like this:

cls = load_class('package.module.class')
obj = cls(...)
Marco Mariani
  • 13,556
  • 6
  • 39
  • 55