ipdb icon indicating copy to clipboard operation
ipdb copied to clipboard

RuntimeWarning: 'ipdb.__main__' found in sys.modules after import of package 'ipdb', but prior to execution of 'ipdb.__main__'; this may result in unpredictable behaviour warn(RuntimeWarning(msg))

Open alayamanas opened this issue 3 years ago • 4 comments

My command is : python -m ipdb test.py and I got:

/root/.miniconda3/lib/python3.6/runpy.py:125: RuntimeWarning: 'ipdb.__main__' found in sys.modules after import of package 'ipdb', but prior to execution of 'ipdb.__main__'; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))

What does this mean and how do I fix it? Thank you! ps: ipdb version 0.13.5

alayamanas avatar Mar 06 '21 12:03 alayamanas

@alayamanas If this is possible on your system, try running ipdb directly:

ipdb3 test.py

Since several others (over 1000 reported views) have run into this as well, it might be worth fixing.

Some background

  • In Python 3, when you run python -m it first imports the module (runs __init__.py) and then __main__.py directly.
  • There is no easy way to tell in __init__.py whether ipdb was run with the -m flag.

Two possible solutions

  1. Do as many other libraries: move most of the main logic into a new file, e.g. ipdb.py and import from there, both in __init__.py and also in __main__.py.
  2. Move most of the logic and main() function into __init__.py and import main from __main__.py.

What do you think @gotcha?

mikez avatar Aug 13 '21 19:08 mikez

@mikez would you point me to one of those libraries ? It woule help me understand point 2 in your proposal.

A PR would be even better 😉

gotcha avatar Sep 09 '21 11:09 gotcha

@gotcha The most important thing to understand is what python -m does in Python3 (see my above post). After that, it boils down to moving most lines from __main__.py to another file. Either to a new file (e.g. ipdb.py) or to __init__.py.

Hope that helps. For the solution (1) you can see most CLIs out there, for example flask or pip. The solution (2) I haven't seen in the wild I think, but it should work just as well. The important thing here is to understand what python -m does.

mikez avatar Sep 09 '21 17:09 mikez

@mikez Thanks for the clarification.

gotcha avatar Sep 10 '21 10:09 gotcha