1

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")
1
  • If I correctly understand your problem you can add del extension after import Commented Nov 13, 2013 at 14:16

1 Answer 1

3

Use importlib (backport).

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().

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.