首页 > 解决方案 > PDB - Set or change Variable in interactive mode

问题描述

Currently, when I modify variables inside the interactive interpreter in pdb, it doesn't carry over outside the interactive session. Is there a way to do this?(I'm already aware of exec, !). However, I want to perform some multi-line operations.

(Pdb) c
(Pdb) pp locals()['a']
*** KeyError: 'a'
(Pdb) !a=2
(Pdb) pp locals()['a']
2
(Pdb) !del a             
(Pdb) pp locals()['a']   
*** KeyError: 'a'        
(Pdb) interact                       
*interactive*                        
>>> a=2                              
>>>                                  
now exiting InteractiveConsole...    
(Pdb) pp locals()['a']               
*** KeyError: 'a' 

标签: pythonpdb

解决方案


尝试下面的代码在调试时执行多行代码pdb/ipdb

!import code; code.interact(local=vars())

这将进入interactive console

仅用于检查您的代码,并且那里的pdb任何更改将仅适用于该执行并且不会更改您的代码


推荐阅读