0

I'm trying to debug a plugin code that I'm running inside exec command

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

4

2 回答 2

0
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. You can go to the next break using c or to next line with 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 """ """.

于 2022-01-09T17:00:20.417 回答
0

在 pudb 中找到它,我可以添加:

_MODULE_SOURCE_CODE = code

或者

linecache.lazycache("<path>/code.py",module_globals= None)

或者

linecache.cache[self.path] = (len(code), None, code.splitlines(True), path)

或者在 VScode 中,我可以将文件添加到 vscode 可以找到它并设置的地方:

"justMyCode": false, 在 launch.json 文件中

于 2022-01-10T09:35:17.233 回答