LoginSignup
11
15

More than 5 years have passed since last update.

Visual Studio CodeによるPythonのデバッグでinput()を扱う方法

Last updated at Posted at 2018-02-28

はじめに

Visual Studio CodeにおいてPython Extensionsインストール後の標準の状態ではデバッグ時にinput()による標準入力を受け取ることができません。
対応方法は公式ガイドに記載があるのですが、簡潔に書かれ過ぎていて分かりづらかったのでこちらにまとめました。

標準入力を受け付けるには、Pythonのデバッグコンソールをexternalに変更する必要があります。
プルダウンで簡単に切り替えられる方法があったのでその方法を追記しました。

環境

macOS: High Sierra Version 10.13.3
VS Code: Version 1.20.1
Python Extension: Version 2018.1.0

方法1: プルダウンで変更

  1. 左ペインのデバッグアイコンを押す。
  2. 一番上のプルダウンから「Python Terminal(external)」を選択する。

Screen Shot 2018-03-02 at 11.34.32.png

これでデバッグ実行時に標準のTerminalが自動的に別窓で起動するので、そこで入力操作を行うことができます。

方法2: デフォルト値を変更する

  1. 左ペインのDebugボタンを押して歯車アイコン(Open launch.json)を押して、設定ファイルを表示させる。
  2. "name": "Python"セクションに「"console": "externalTerminal",」を追記する。
launch.json
        {
            "name": "Python",
            "console": "externalTerminal",

3 . この時、下の方にある「"Python: Terminal (integrated)"」や「"name": "Python: Terminal (external)"」セクションは編集不要なのでそのままにする。
4. launch.jsonを保存すれば即座に反映される。VS Codeの再起動は不要。

手順は以上です。

参考

公式ガイド
https://code.visualstudio.com/docs/python/debugging#_unable-to-capture-user-input-while-debugging

リリースノート
https://blogs.msdn.microsoft.com/ayatokura/2016/09/10/vsc1-5-1_releasenote/
昔の方法の"externalConsole": true が不要となったことが記載されています。

11
15
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
11
15