ある処理をデバッグ時とそれ以外で分けたい(※)ときに、どちらなのかを判定する方法 in VSCodeです。
※ PythonとC++の混合モードデバッグをしたいので、デバッグ時はデバッグ ビルドのpyd、それ以外はリリースビルドのpydを使いたい場合とか
拡張機能インストール先\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher\__main__.p y
(2021.1.502429796は今回確認したバージョン)に
__main__.py
...略...
if __name__ == "__main__":
# debugpy can also be invoked directly rather than via -m. In this case, the first
...長いコメント...
if "debugpy" not in sys.modules:
# Do not use dirname() to walk up - this can be a relative path, e.g. ".".
sys.path[0] = sys.path[0] + "/../../"
__import__("debugpy")
del sys.path[0]
...略...
とあるので、実行したいスクリプトに以下のように書く:
import sys
if "debugpy" in sys.modules:
print('VSCodeからデバッグされてます')
else:
print('VSCodeからデバッグされてません')
('Д`)ふぅ...