[英]How to debug python code running inside exec()
I'm trying to debug a plugin code that I'm running inside exec command我正在尝试调试我在 exec 命令中运行的插件代码
is there a way to debug it somehow?有没有办法以某种方式调试它? for example:
例如:
code='''
breakpoint()
foo=5
print(foo)
'''
exec(code)
I want to stop before foo is printed, and do list
(pdb) command and see the code我想在打印 foo 之前停止,然后执行
list
(pdb) 命令并查看代码
In [8]: code='import ipdb\nnfoo=5\nipdb.set_trace()\nprint(nfoo**2)'
In [9]: exec(code)
> <string>(4)<module>()
ipdb> nfoo
5
ipdb> nfoo = 6
ipdb> c
36
After ipdb.set_trace()
ipdb will start.在
ipdb.set_trace()
之后,ipdb 将启动。 You can go to the next break using c
or to next line with n
.您可以使用 go 到下一个中断使用
c
或使用n
到下一行。 check the following cheetsheet: cheetsheet检查以下小抄: 小抄
code='import ipdb\nnfoo=5\nipdb.set_trace()\nprint(nfoo**2)'
In [13]: exec(code)
None
> <string>(4)<module>()
ipdb> nfoo
5
ipdb> nfoo = 6
ipdb> n
36
Note: it's easier to place your code inside a """ """.注意:将代码放在“”“”“”中更容易。
found it in pudb I can add:在 pudb 中找到它,我可以添加:
_MODULE_SOURCE_CODE = code
or或者
linecache.lazycache("<path>/code.py",module_globals= None)
or或者
linecache.cache[self.path] = (len(code), None, code.splitlines(True), path)
or in VScode I can just add the file to somewhere vscode can find it and just set:或者在 VScode 中,我可以将文件添加到 vscode 可以找到它的地方并设置:
"justMyCode": false,
in launch.json file "justMyCode": false,
在 launch.json 文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.