以为ipdb能够和gdb一样用tui方式运行,结果还是有点遗憾。
补救办法是可以将默认的3行改大。
1 2 3 |
alvin@chen:~/python$ python -c 'import ipdb; print(ipdb)' <module 'ipdb' from '/usr/local/lib/python3.6/dist-packages/ipdb/__init__.py'> alvin@chen:~/python$ |
此时可以找到相应的__main__.py为/usr/local/lib/python3.6/dist-packages/ipdb/__main__.py。
修改__main__.py中的context为合适的数值,如下所示:
1 2 3 4 |
alvin@chen:~$ cat /usr/local/lib/python3.6/dist-packages/ipdb/__main__.py | grep -n -P "context=\d" 45:def _init_pdb(context=20, commands=[]): 62:def set_trace(frame=None, context=20): alvin@chen:~$ |
修改后效果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
alvin@chen:~/python$ ipdb3 1119.py > /home/alvin/python/1119.py(2)<module>() 1 #!/usr/bin/python3 ----> 2 class Solution: 3 def removeVowels(self, S: str) -> str: 4 temp = "" 5 for ch in S: 6 if ch not in [ 'a','e','i','o','u']: 7 temp = temp+ch 8 return temp 9 10 input = "abcdefghijklmnopqrstuvwxyz"; 11 x = Solution(); 12 print("The result of " + input + " is " + x.removeVowels(input)); ipdb> |
ps:以上测试为macbook,在ubuntu上安装以后一直报错:
1 2 3 4 5 6 |
alvin@chen.asia:~/lc$ ipdb3 0001.py Traceback (most recent call last): File "/usr/local/bin/ipdb3", line 5, in <module> from ipdb.__main__ import main ImportError: No module named ipdb.__main__ alvin@chen.asia:~/lc$ |
找了各种资料都没解决,偶然发现这里有问题:
1 2 3 4 5 6 7 8 9 10 |
alvin@chen.asia:~/lc$ cat /usr/local/bin/ipdb3 #!/usr/bin/python # -*- coding: utf-8 -*- import re import sys from ipdb.__main__ import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(main()) alvin@chen.asia:~/lc$ |
将/usr/bin/python修改为/usr/bin/python3就一切正常了。