import module from string variable

Posted on

Solving problem is about exposing yourself to as many situations as possible like import module from string variable and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about import module from string variable, which can be followed any time. Take easy to follow this discuss.

import module from string variable

I’m working on a documentation (personal) for nested matplotlib (MPL) library, which differs from MPL own provided, by interested submodule packages. I’m writing Python script which I hope will automate document generation from future MPL releases.
I selected interested submodules/packages and want to list their main classes from which I’ll generate list and process it with pydoc

Problem is that I can’t find a way to instruct Python to load submodule from string. Here is example of what I tried:

import matplotlib.text as text
x = dir(text)


i = __import__('matplotlib.text')
y = dir(i)


j = __import__('matplotlib')
z = dir(j)

And here is 3 way comparison of above lists through pprint:

enter image description here

I don’t understand what’s loaded in y object – it’s base matplotlib plus something else, but it lack information that I wanted and that is main classes from matplotlib.text package. It’s top blue coloured part on screenshot (x list)

Please don’t suggest Sphinx as different approach.

Asked By: theta


Answer #1:

The __import__ function can be a bit hard to understand.

If you change

i = __import__('matplotlib.text')


i = __import__('matplotlib.text', fromlist=[''])

then i will refer to matplotlib.text.

In Python 2.7 and Python 3.1 or later, you can use importlib:

import importlib
i = importlib.import_module("matplotlib.text")

Some notes

  • If you’re trying to import something from a sub-folder e.g. ./feature/, the code will look like importlib.import_module("")

  • You can’t import anything if there is no in the folder with file you are trying to import

Answered By: mzjn

Answer #2:

importlib.import_module is what you are looking for. It returns the imported module. (Only available for Python >= 2.7 or 3.x):

import importlib
mymodule = importlib.import_module('matplotlib.text')

You can thereafter access anything in the module as mymodule.myclass, etc.

Answered By: gecco

Answer #3:

spent some time trying to import modules from a list, and this is the thread that got me most of the way there – but I didnt grasp the use of ___import____ –

so here’s how to import a module from a string, and get the same behavior as just import. And try/except the error case, too. 🙂

  pipmodules = ['pycurl', 'ansible', 'bad_module_no_beer']
  for module in pipmodules:
          # because we want to import using a variable, do it this way
          module_obj = __import__(module)
          # create a global object containging our module
          globals()[module] = module_obj
      except ImportError:
          sys.stderr.write("ERROR: missing python module: " + module + "n")

and yes, for python 2.7> you have other options – but for 2.6<, this works.

Answered By: keen

Answer #4:

I developed these 3 useful functions:

def loadModule(moduleName):
    module = None
        import sys
        del sys.modules[moduleName]
    except BaseException as err:
        import importlib
        module = importlib.import_module(moduleName)
    except BaseException as err:
        serr = str(err)
        print("Error to load the module '" + moduleName + "': " + serr)
    return module
def reloadModule(moduleName):
    module = loadModule(moduleName)
    moduleName, modulePath = str(module).replace("' from '", "||").replace("<module '", '').replace("'>", '').split("||")
    if (modulePath.endswith(".pyc")):
        import os
        module = loadModule(moduleName)
    return module
def getInstance(moduleName, param1, param2, param3):
    module = reloadModule(moduleName)
    instance = eval("module." + moduleName + "(param1, param2, param3)")
    return instance

And everytime I want to reload a new instance I just have to call getInstance() like this:

myInstance = getInstance("MyModule", myParam1, myParam2, myParam3)

Finally I can call all the functions inside the new Instance:


The only specificity here is to customize the params list (param1, param2, param3) of your instance.

Answered By: prossblad

Answer #5:

Apart from using the importlib one can also use exec method to import a module from a string variable.

Here I am showing an example of importing the combinations method from itertools package using the exec method:

    import_str = "from {0} import {1}".format(ITEM[0],', '.join(str(i) for i in ITEM[1:]))
ar = list(combinations([1, 2, 3, 4], 2))
for elements in ar:


(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
Answered By: arsho
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published. Required fields are marked *