1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python実行時にVisual Studio Codeでデバッグ中かどうかを判定する

Last updated at Posted at 2021-02-10

ある処理をデバッグ時とそれ以外で分けたい(※)ときに、どちらなのかを判定する方法 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からデバッグされてません')

('Д`)ふぅ...

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?