Python using import in a function, import content of variable and not its name?
Question: I have a function that checks if a module is installed, if not it will install
it. I am passing the extension through a function. However how can I stop it
attempting to import the variable name and use its contents?

Example:

    
    
    def importExtension(extension):
         try:
            import extension
        except:
            Do stuff
    importExtension("blah")
    


Answer: Use
[importlib](http://docs.python.org/2/library/importlib.html#importlib.import_module)
([backport](https://pypi.python.org/pypi/importlib/)).

    
    
    import importlib
    
    def importExtension(extension):
         try:
            importlib.import_module(name)
        except:
            Do stuff
    importExtension("blah")
    

* * *

Also, to quote the docs about `__import__(..)`:

> This is an advanced function that is not needed in everyday Python
> programming, unlike importlib.import_module().

