2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoder Python 勢向け:VSCodeで標準入力付きのデバッグ環境を作る方法

Last updated at Posted at 2025-05-12

VSCode で AtCoder などの標準入力のデバッグを紹介します。

この記事では、VSCodeの launch.json を活用して、sample.in を標準入力として渡しながらデバッグ実行する方法を紹介します。

🎯 対象読者

  • AtCoder を Python で解いている人
  • sample.in などの入力ファイルを使いたい人
  • VSCode のデバッガーで標準入力を再現したい人

📁 ディレクトリ構成の例

─ ABC001
  ├── a
  │   ├── main.py
  ├── b
  │   ├── main.py
  ├── c
  │   ├── main.py
  └── d
      └── main.py
─ sample.in
  • sample.in にテスト用の入力を記載する

⚙️ .vscode/launch.json の設定

  1. .vscode フォルダを作成(なければ)
  2. launch.json を作成して、以下を記述:
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python デバッガー: 現在のファイル",
      "type": "debugpy",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "args": [
        "<",
        "sample.in"
      ]
    }
  ]
}

💡 args に "<", "sample.in" を指定することで、実行時に sample.in の内容が input() へ流れるようになります。

▶️ デバッグの実行手順

  1. VSCodeで main.py を開く
  2. デバッグパネル(左の虫アイコン)を開く
  3. 「Python デバッガー: 現在のファイル」を選択し、F5キーを押す

標準入力として sample.in の内容が渡され、AtCoderと同じ動作になります!

🧠 デバッグを使うことで得られるメリット

デバッグ機能には、以下のような機能があります。

  • ブレークポイント:任意の行で処理を一時停止し、ロジックの流れを1行ずつ確認できます
  • 変数ウォッチ:実行中の変数の値をリアルタイムに監視できます
  • ステップ実行:1行ずつ処理を追うことで、思わぬバグの原因を特定しやすくなります
  • スタックトレースの可視化:再帰や例外の流れを視覚的に把握できます

競技プログラミングでは「とりあえず print デバッグ」という方も多いですが、デバッガーを使いこなせば原因特定が数倍速くなります!

ぜひこの機会に、VSCode デバッガーでの標準入力デバッグをマスターして、実力をひとつ上のステージへ引き上げてみてください 💪

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?