Launch Python debugger while simultaneously executing module as script

前端 未结 5 1429
你的背包
你的背包 2020-12-14 09:21

When developing a Python package, it\'s very convenient to use the -m option to run modules inside the package as scripts for quick testing. For example, for

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 09:33

    Building on @jed's answer, I built this module:

    import pdb
    import runpy
    import sys
    
    
    def main():
        module = sys.argv[1]
        sys.argv[1:] = sys.argv[2:]
        pdb.runcall(runpy.run_module, module, run_name='__main__')
    
    
    __name__ == '__main__' and main()
    

    Put that module as mpdb.py anywhere in your Python Path (current directory works), then you may invoke:

    python -m mpdb somepackage.somemodule even with args
    

提交回复
热议问题