'Run the module after setting up the environment.
First check the syntax.  If OK, make sure the shell is active and
then transfer the arguments, set the run environment\'s working
directory to the directory of the module being executed and also
add that directory to its sys.path if not already included.'

def _run_module_event(self, event):

    filename = self.getfilename()
    if (not filename):
        return 'break'
    code = self.checksyntax(filename)
    if (not code):
        return 'break'
    if (not self.tabnanny(filename)):
        return 'break'
    interp = self.shell.interp
    if PyShell.use_subprocess:
        interp.restart_subprocess(with_cwd=False)
    dirname = os.path.dirname(filename)
    interp.runcommand('if    1:\n                                                __file__    =    {filename!r}\n                                                import    sys    as    _sys\n                                                from    os.path    import    basename    as    _basename\n                                                if    (not    _sys.argv    or\n                                                                _basename(_sys.argv[0])    !=    _basename(__file__)):\n                                                                _sys.argv    =    [__file__]\n                                                import    os    as    _os\n                                                _os.chdir({dirname!r})\n                                                del    _sys,    _basename,    _os\n                                                \n'.format(filename=filename, dirname=dirname))
    interp.prepend_syspath(filename)
    interp.runcode(code)
    return 'break'
