python get module variable by name

getattr(themodule, "attribute_name", None)

The third argument is the default value if the attribute does not exist.

From https://docs.python.org/2/library/functions.html#getattr

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.


import mymodule

var = getattr(mymodule, variablename)