gpt4 book ai didi

python - 在 PyCharm 中以 Debug模式运行代码 "partially"

转载 作者:行者123 更新时间:2023-11-28 17:04:46 24 4
gpt4 key购买 nike

我想在 PyCharm 中以调试器模式运行一些 Python 代码。我的代码包含一个 API 函数调用,由于某种原因,该单个函数调用在调试器模式下会一直运行。

我真的不关心调试那个特定的函数,让调试器跳过那个函数(只在常规模式下运行它)就可以了。但是,我希望能够在 Debug模式下运行我的其余代码。

这在 PyCharm 中是否可行,或者是否有任何 Python 解决方法?

# some code to be run in debugger mode, e.g.
func_a(obj_a) #this function modifies obj_a

# some API function call, super slow in debugger mode. can I just run this part in run mode? e.g.
obj_b = api_func(obj_a)

# rest of the code to be run in debugger mode e.g.
func_c(obj_b)

最佳答案

可能您可以使用 sys.gettracesys.settrace在您的 API 调用运行时删除调试器,但不推荐这样做,如果您这样做,PyCharm 会向您提示:

PYDEV DEBUGGER WARNING:
sys.settrace() should not be used when the debugger is being used.
This may cause the debugger to stop working correctly.
If this is needed, please check:
http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html
to see how to restore the debug tracing back correctly.

在你的情况下,你会做这样的事情:

import sys

# some code to be run in debugger mode, e.g.
func_a(obj_a) #this function modifies obj_a

# Remove the trace function (but keep a reference to it).
_trace_func = sys.gettrace()
sys.settrace(None)

# some API function call, super slow in debugger mode. can I just run this part in run mode? e.g.
obj_b = api_func(obj_a)

# Put the trace function back.
sys.settrace(_trace_func)

# rest of the code to be run in debugger mode e.g.
func_c(obj_b)

强烈建议您在禁用调试器时运行的代码尽可能短。

关于python - 在 PyCharm 中以 Debug模式运行代码 "partially",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51752911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com