3

I am invoking the python interpreter like the following

python -m MyModule.main 

Is there an easy way to invoke the the pdb debugger via the command line in this use case ?

Hakan Baba
  • 1,897
  • 4
  • 21
  • 37
  • One option I found is to call `pdb.set_trace()` in the function but I do not want to modify the source every time. – Hakan Baba Feb 19 '17 at 02:51
  • I find the `python -m pdb my_script.py` way of invoking pdb very convenient. Is something similar possible in this use case ? – Hakan Baba Feb 19 '17 at 02:53
  • You simply run the `main.py` of the module. That's what `-m` does. – Klaus D. Feb 19 '17 at 04:23
  • In that case I am getting a relative import in non package error. `from .Config import Config ValueError: Attempted relative import in non-package Uncaught exception. Entering post mortem debugging` – Hakan Baba Feb 25 '17 at 07:19
  • 1
    Possible duplicate of [Launch Python debugger while simultaneously executing module as script](https://stackoverflow.com/questions/18166362/launch-python-debugger-while-simultaneously-executing-module-as-script) – Thomas Hickman Jun 18 '18 at 11:00

1 Answers1

0

You can execute pdb on a python file which has same effect as executing python -m MyModule.main using the runpy module (see https://www.python.org/dev/peps/pep-0338/#id16) e.g as a command on a UNIX-like OS:

python -m pdb <(echo "import runpy;runpy.run_module('MyModule.main', run_name='__main__', alter_sys=True)")

This could be generalised into a function that you could add to your bash_profile:

function pdb-m { python -m pdb <(echo "import runpy;runpy.run_module('$1', run_name='__main__', alter_sys=True)") "${@:2}" }

Thomas Hickman
  • 145
  • 1
  • 12