0

Somewhere in my library code I have a matplotlib.pyplot import. When I import one of my library modules I see that pyplot ends up getting imported (as evidenced by the fact that, in ipython notebook, my notebook immediately crashes because I did not run %matplotlib inline).

How can I get a stacktrace showing the sequence of imports (starting with my library module import) that resulted in pyplot getting imported? So far I have tried using sed to replace all my instances of

from matplotlib import pyplot as plt

to

import pdb; pdb.set_trace(); from matplotlib import pyplot as plt

but that hasn't turned it up yet.

unsorted
  • 3,114
  • 3
  • 29
  • 39
  • Why can't you do a recursive word search for pyplot through that library? –  Oct 02 '17 at 01:21
  • Can you elaborate? – unsorted Oct 02 '17 at 13:01
  • You state "Somewhere in my library code I have a matplotlib.pyplot import". Why can't you search for where that happens? –  Oct 02 '17 at 21:30
  • Sorry, that was confusing. I have a bunch of pyplot imports in my library, and one of them is getting hit from importing one of my modules (which does not directly import pyplot but which imports other modules which import other modules which... eventually imports pyplot). I want to know how to find the path that led to the pyplot import so I can reorganize my library structure. – unsorted Oct 03 '17 at 22:19
  • Sorry, it's still confusing: I would think things start with a script, or perhaps a one-liner in the console (Jupyter or default). That scripts imports what? The library, or a module? If it imports a module, does that module (indirectly) import the library? Is it the *library* where the (bad) pyplot import happens (yes, as I read it now, but just want to double check)? –  Oct 04 '17 at 01:25
  • I'm also surprised that just a pyplot import crashes your notebook (I can do `from matplotlib import pyplot` in a notebook just fine, without any extras). I assume then that there's no traceback (not on the command line running the notebook either), but a segfault or similar? –  Oct 04 '17 at 01:27
  • I wonder if you can install matplotlib locally (eg, with `--user` or `--prefix`), set that first in your PYTHONPATH, and raise an exception at the top of the `pyplot.py` module (or if you have access and don't mind meddling in the default installation, you could do it directly there). The exception should provide you with a traceback of the import sequence (just don't use `raise SystemExit()`: that exception won't show a traceback). –  Oct 04 '17 at 01:41
  • Not sure what you mean with comment #4. In a script (or in ipython notebook, which is basically a script) I import one of my modules, which imports some of my other modules, and so on. A few of my lowlevel modules import pyplot, and I just want to know which one of them it was (and what path of imports resulted in that module getting imported). – unsorted Oct 04 '17 at 23:40
  • I think we're on the same page though because I agree that modifying `pyplot.py` seems like it would work. Is there no way to inject a breakpoint into an external library before importing it? – unsorted Oct 04 '17 at 23:42
  • Possible duplicate of [Trace Python imports](https://stackoverflow.com/questions/7332299/trace-python-imports) –  Oct 05 '17 at 06:13

0 Answers0