paste.util.import_string – Import objects from strings

‘imports’ a string – converts a string to a Python object, importing any necessary modules and evaluating the expression. Everything before the : in an import expression is the module path; everything after is an expression to be evaluated in the namespace of that module.

Alternately, if no : is present, then import the modules and get the attributes as necessary. Arbitrary expressions are not allowed in that case.

Module Contents

paste.util.import_string.eval_import(s)

Import a module, or import an object from a module.

A module name like foo.bar:baz() can be used, where foo.bar is the module, and baz() is an expression evaluated in the context of that module. Note this is not safe on arbitrary strings because of the eval.

paste.util.import_string.simple_import(s)

Import a module, or import an object from a module.

A name like foo.bar.baz can be a module foo.bar.baz or a module foo.bar with an object baz in it, or a module foo with an object bar with an attribute baz.

paste.util.import_string.import_module(s)

Import a module.

paste.util.import_string.try_import_module(module_name)

Imports a module, but catches import errors. Only catches errors when that module doesn’t exist; if that module itself has an import error it will still get raised. Returns None if the module doesn’t exist.