How to execute another python script from your script and be able to debug?

£可爱£侵袭症+ 提交于 2019-12-05 03:04:32

So far I found a solution that works only with Python 2.7+ (runpy.run_path() was introduced in Python 2.7).

If you can find one that works with 2.6 (or even 2.5) you are welcome to post it.

import runpy, sys
saved_argv = sys.argv
... # patch sys.argv[1:] and load new command line parameters
# run_path() does change only sys.argv[0] but restores it
runpy.run_path('another.py', run_name="__main__")
sys.argv = saved_argv # restore sys.argv

Do you have control over another.py? It would be a good idea to change it and add a main() method. Main() can then be invoked if __name__ == '__main__'. This will alleviate your problems a great deal. It is also unit testing friendly.

Based on the recommendation received from EOL, I made an extension to execfile() that does solve its limitations execfile2()

Below is the code, but newer versions will be published here. It is backwards compatible with execfile().

def execfile2(filename, _globals=dict(), _locals=dict(), cmd=None, quiet=False):
    _globals['__name__']='__main__'
    saved_argv = sys.argv # we save sys.argv
    if cmd:
    sys.argv=list([filename])
            if isinstance(cmd , list):
                sys.argv.append(cmd)
            else:
                sys.argv.extend(shlex.split(cmd))
    exit_code = 0
try:
        execfile(filename, _globals, _locals)
    except SystemExit as e:
        if isinstance(e.code , int):
            exit_code = e.code # this could be 0 if you do sys.exit(0)
        else:
            exit_code = 1
    except Exception:
        if not quiet:
            import traceback
            traceback.print_exc(file=sys.stderr)
        exit_code = 1
    finally:
        if cmd:
            sys.argv = saved_argv # we restore sys.argv
    return exit_code

You can make the main block call a function. This way you will be able to call the same function when importing as a module.

def main():
    print "All start up commands"

if __name__ == "__main__":
    main()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!