The original question was about a way to check if the script is run with the debugger in pycharm.
Pycharm 设定环境变量 PYDEVD_LOAD_VALUES_ASYNC 文字穿透。
To check if the debugger is active you can check if PYDEVD_LOAD_VALUES_ASYNC exists in the list of environment variables:
import os
if PYDEVD_LOAD_VALUES_ASYNC in os.environ:
print( debugger is running )
else:
print( normal run )
或作为职务
import os
def debugger_enabled():
return PYDEVD_LOAD_VALUES_ASYNC in os.environ
在PyCharm 2023.3.4(社区版)测试
(this is a repost of my original answer https://stackoverflow.com/a/78118417/1641449 because i found it so practical that i wanted to post it where i searched before but didn t find it.)