4

Python doesn't exit if I have imported two libraries in a certain order. I'm using the python libraries scitools and fenicstools.

In the python shell, the following will work:

import fenicstools
import scitools
exit()

This won't exit but hang (reversed the imports):

import scitools      # ok
import fenicstools   # ok
exit()               # gets me stuck, I can still exit with Ctrl+C

I can reproduce this on two Ubuntu 14.04 machines and am now at a complete loss. How do I even start debugging such an issue?

Background: I'm using sumatra to keep track of my numerical simulations. It gathers and logs the versions of the dependencies of my project. I thus have no control over the order in which it tries to do so. Result: It gets stuck.

Edit: Following @ErlVolton's suggestion, I tried pdb. Put the two imports in their problematic order in a file called test.py.

$ pdb test.py
> /home/gallomania/test.py(1)<module>()
-> import scitools
(Pdb) n
> /home/gallomania/test.py(2)<module>()
-> import fenicstools
(Pdb) n
--Return--
> /home/gallomania/test.py(2)<module>()->None
-> import fenicstools
(Pdb) exit

... This makes pdb not exit.

gallomania
  • 41
  • 3
  • 1
    Just use the first example and move on? – takendarkk Oct 09 '14 at 16:22
  • 2
    @Takendarkk there's nothing wrong with wanting to understand why something is happening. – Sterling Archer Oct 09 '14 at 16:26
  • I'm using [sumatra](http://neuralensemble.org/sumatra/) to keep track of my numerical simulations. It gathers and logs the versions of the dependencies of my project. I thus have no control over the order in which it tries to do so. Result: It gets stuck. – gallomania Oct 09 '14 at 16:27
  • @gallomania I think you should add that to your question. – takendarkk Oct 09 '14 at 16:30
  • Instead try either `sys.exit()` or `raise SystemExit`. Builtins `exit` is added by the `site` module; I wouldn't rely on it for non-interactive use. – Eryk Sun Oct 09 '14 at 18:11
  • @eryksun the exit() is during interactive use. If I just put the imports in a script and execute it with python, it will hang as well. – gallomania Oct 09 '14 at 18:42

1 Answers1

0

Use pdb to step through each operation and see which line of code fenicstools is hanging on in its __init__.py

https://docs.python.org/2/library/pdb.html

Example:

$ pdb test.py
> /home/cleekley/test/test.py(1)<module>()
-> import sys
(Pdb) s
> /home/cleekley/test/test.py(2)<module>()
-> import time
(Pdb) s
> /home/cleekley/test/test.py(4)<module>()
-> while True:
(Pdb) s
> /home/cleekley/test/test.py(5)<module>()
-> time.sleep(1)
(Pdb) s
> /home/cleekley/test/test.py(4)<module>()
-> while True:
(Pdb) quit
ErlVolton
  • 6,714
  • 2
  • 15
  • 26
  • It imports fenicstools just fine. It's the exit on which it hangs. – gallomania Oct 09 '14 at 16:33
  • Are you sure? What does pdb say? There could be an atexit function in one of these libraries that's hanging. I will try in a virtualenv and see what I can come up with – ErlVolton Oct 09 '14 at 16:36
  • Absolutely sure, I'm testing this in an interactive shell. So, following your suggestions I have put the two imports in a file I called `test.py` and run pdb on it. I will append the results to my question. – gallomania Oct 09 '14 at 16:45
  • @gallomania please use the pdb command s instead of n . Also, typing exit at the pdb prompt is telling pdb to exit as is not the same as calling exit() – ErlVolton Oct 09 '14 at 16:53
  • I really appreciate your effort to shed light into this. Stepping through the program like you suggest sends me down the rabbit hole. I think scitools is too large to step through its import line by line. Have 's' as fast as I could type ;) – gallomania Oct 09 '14 at 17:04
  • You can step in some with s and then use n. You could also try to figure out which library it's hanging in and only step into that one. You could also potentially script something to just step until it hangs. Let me know what you find out. – ErlVolton Oct 09 '14 at 17:06