def execmodule(module_name, globals=None, locals=None, set_argv0 = False):
    """Locate the requested module and run it using execfile

    Any containing packages will be imported before the module is executed.
    Globals and locals arguments are as documented for execfile
    set_argv0 means that sys.argv[0] will be set to the module's filename prior
    to execution (some scripts use argv[0] to determine their location).
    """
    if globals is None:
        globals = sys._getframe(1).f_globals # Mimic execfile behaviour
    if locals is None:
        locals = globals
    pkg_name = None
    path = None
    split_module = module_name.rsplit('.', 1)
    if len(split_module) == 2:
        module_name = split_module[1]
        pkg_name = split_module[0]
    try:
        # Import the containing package
        if pkg_name:
            pkg = __import__(pkg_name)
            for sub_pkg in pkg_name.split('.')[1:]:
                pkg = getattr(pkg, sub_pkg)
            path = pkg.__path__
        # Locate the module
        module_info = find_module(module_name, path)
    except ImportError, e:
        raise _ExecError(str(e))
    # Check that all is good
    module = module_info[0]
    filename = module_info[1]
    filetype = module_info[2][2]
    if module: module.close() # We don't actually want the file handle
    if filetype not in (PY_SOURCE, PY_COMPILED):
        raise _ExecError("%s is not usable as a script\n  (File: %s)" %
                          (module_name, filename))
    # Let's do it
    if set_argv0:
        sys.argv[0] = filename
    execfile(filename, globals, locals)
Locate the requested module and run it using execfile

    Any containing packages will be imported before the module is executed.
    Globals and locals arguments are as documented for execfile
    set_argv0 means that sys.argv[0] will be set to the module's filename prior
    to execution (some scripts use argv[0] to determine their location).
    
recipes/Python/307772_Executing_modules_inside_packages/recipe-307772.py