ipdb icon indicating copy to clipboard operation
ipdb copied to clipboard

Support -m flag

Open jacobsvante opened this issue 10 years ago • 8 comments

So one can run the equivalent of e.g. python -m alembic.config revision --autogenerate

jacobsvante avatar Sep 10 '14 12:09 jacobsvante

I do not understand what you mean. Can you gibe an example with ipdb ?

gotcha avatar Dec 22 '14 16:12 gotcha

I just mean that it would be nice if ipdb could support the -m flag just like Python. Nothing more nothing less.

$ python --help
[...]
-m mod : run library module as a script (terminates option list)
[...]

The part I mentioned in the previous message is just an example where I call a module named alembic.config (which itself takes the argument revision and the flag --autogenerate). You can run the command yourself if you just install alembic with pip. Doing this won't work though:

ipdb -m alembic.config revision --autogenerate

It gives me Error: -m does not exist.

jacobsvante avatar Dec 22 '14 17:12 jacobsvante

It is supposed to work without -m as explained when running ipdb -h

usage: ipdb.py scriptfile [arg] ...

Have you tried ?

gotcha avatar Dec 22 '14 17:12 gotcha

Yeah and that won't work with packages that use relative imports (from . import db). The error thrown is ValueError: Attempted relative import in non-package.

Also it's really nice to not have to know the entire path if it's a 3rd party script buried somewhere on my file system in site-packages. I.e. instead of doing:

ipdb /my/path/to/venv/lib/python2.7/site-packages/mypackage/mymodule.py

It would be nice to be able to do:

ipdb -m mypackage.mymodule

jacobsvante avatar Dec 22 '14 20:12 jacobsvante

+1 to this.

If I am already running a module by python -m mypackage.mymodule then being able to immediately replace python with ipdb on the command line to rerun with the debugger enabled would be very useful indeed.

wodow avatar Jan 08 '15 23:01 wodow

The issue is that the __package__ variable is not getting properly set to None per PEP 366. It appears like this is specifically a Python 3 bug because the problem also appears in pdb but only in Python 3. I've opened an issue here.

As a work around, you can either use Python 2 or simply inject __package__ = None at the top of your script while debugging.

kprussing avatar Nov 20 '15 16:11 kprussing

@kprussing If I understand well what you say, this is not an ipdb bug but a Python 3 bug. Correct ?

gotcha avatar Nov 22 '15 11:11 gotcha

Partially. After reviewing the discussion, I realize that my comment is a bit off topic. I ran into a similar issue trying to debug a script that uses relative imports. According to PEP 366, the loader is supposed to set the __package__ variable to None when a script is directly executed. Thus, when run as python script.py, __package__ is supposed to be None; whereas, it is properly set when run as python -m script. The Python 3 version of pdb does not do this. Rather, it sets __package__ to the empty string. I haven't fully reviewed your source, but I suspect this is affecting the relative import in your case too. The minimum working example I attached to the linked issue above shows this behavior in both pdb and ipdb.

I like the idea of supporting the -m flag, but if the erroneous loader behavior is present in Python, you would have to manually work around it. I opened the issue on the Python project because it seems to me it should be handled at the underlying source.

kprussing avatar Nov 22 '15 18:11 kprussing