Python dynamic class names
Question: > **Possible Duplicate:**  
>  [Dynamic loading of python
> modules](http://stackoverflow.com/questions/951124/dynamic-loading-of-
> python-modules)  
>  [python: How to add property to a class
> dynamically?](http://stackoverflow.com/questions/1325673/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.


Answer: You have to use the `__import__` function:

[http://docs.python.org/library/functions.html#**import**](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()
    

