Importing from __future__ doesn't work under PDB

martin846 at gmail.com martin846 at gmail.com
Wed May 11 05:52:13 EDT 2016


Hi folks, 

I've been trying to use pdb to debug a Python 2.7 program and this has really stumped me.....

When debugging a program that uses 

from __future__ import division 

in Python 2.7, it would be useful to be able to have the same behaviour at the pdb prompt. However, it looks like pdb doesn't honour either the import statement in the original program, or one run explicitly at the pdb prompt. 

Given the program test.py:

from __future__ import division
print(2/3)

I get the following interactive pdb session:

martin at martin-H97-D3H:~$ python -m pdb test.py
> /home/martin/test.py(1)<module>()
-> from __future__ import division
(Pdb) n
> /home/martin/test.py(2)<module>()
-> print(2/3)
(Pdb) n
0.666666666667
--Return--
> /home/martin/test.py(2)<module>()->None
-> print(2/3)
(Pdb) 2/3
0
(Pdb) from __future__ import division
(Pdb) 2/3
0
(Pdb) 

In other words, I get different results for the same expression (2/3) in the program and on the pdb prompt, which makes debugging tricky. I cannot figure out how to persuade pdb to actually load the division module. Is there an approved way? Normal modules load as expected in pdb:

(Pdb) import os
(Pdb) os.getcwd()
'/home/martin'

so this is obviously some special magic that applies only to the __future__ module. Any ideas?





More information about the Python-list mailing list