'Execute a single code block, including imports and appending
global vars.
Args:
code (str): Code to run.
extra_environ (dict): Environment variables to run with code.
debug (bool, optional): Set the DEBUG variable in the execution
namespace.
Returns:
err (str or None): An error code or None (ok).'

def code_exec(self, code, extra_environ=None, debug=False):

    environdict = {'settings_module': settings, 'DEBUG': debug}
    for (key, value) in extra_environ.items():
        environdict[key] = value
    code = ('#    batchcode    evennia    initialization:    \ntry:    settings_module.configure()\nexcept    RuntimeError:    pass\nfinally:    del    settings_module\n\n%s' % code)
    try:
        exec code in environdict
    except Exception:
        (etype, value, tb) = sys.exc_info()
        fname = tb_filename(tb)
        for tb in tb_iter(tb):
            if (fname != tb_filename(tb)):
                break
        lineno = (tb.tb_lineno - 1)
        err = ''
        for (iline, line) in enumerate(code.split('\n')):
            if (iline == lineno):
                err += ('\n|w%02i|n:    %s' % ((iline + 1), line))
            elif ((lineno - 5) < iline < (lineno + 5)):
                err += ('\n%02i:    %s' % ((iline + 1), line))
        err += '\n'.join(traceback.format_exception(etype, value, tb))
        return err
    return None
