32
31

More than 5 years have passed since last update.

VSCodeでPythonをデバッグしたい

Last updated at Posted at 2017-12-03

はじめに

最近使用しているVSCodeでPythonの開発環境を構築していきます。
VSCodeでデバッグ、実行ができることまで。
Pythonはインストール済みとします。

環境

macOS Sierra 10.12.5
Visual Studio Code version1.18

extensionのinstall

  • python

基本的にこれで十分。ちなみに下記機能を揃えている
Linting
Debugging (multi-threaded, remote)
Intellisense
code formatting
refactoring
unit tests
snippets
Data Science (with Jupyter)
PySpark
...and more.

linterのインストール

事前にインストールしておいても良いが、
VSCodeで.pyファイル開くとlinterがない旨のメッセージ出るので
OK押すとpylintがインストールされるのでそれでOK

デバッグ

デバッグ(shift + cmd + D)を選択
スクリーンショット 2017-12-02 15.41.08.jpg

歯車みたいなボタン押す
スクリーンショット 2017-12-02 15.46.25.jpg

「Python」の環境を選択する
スクリーンショット 2017-12-02 15.47.48.jpg

launch.jsonファイルが作成され、自動的にvscode上で開かれるので、そのまま変更せずに閉じる
スクリーンショット 2017-12-02 15.48.52.jpg

デバッグしたい.pyファイルをアクティブにした状態でF5でデバッグ可能
スクリーンショット 2017-12-02 15.51.54.jpg
デバッグコンソールに出力される
スクリーンショット 2017-12-02 15.52.34.jpg

実行

F1(もしくは cmd+Shift+P)キーを押して Command Palette を開き、「tasks」を入力
出てきた一覧の中から「タスク:タスクの構成」を選択
スクリーンショット 2017-12-02 15.59.03.jpg

「テンプレートからtasks.jsonを生成」を選択
スクリーンショット 2017-12-02 16.00.11.jpg

更に一覧が出てくるので「Others 任意の外部コマンドを実行する例」を選択
スクリーンショット 2017-12-02 16.01.31.jpg

選択すると、tasks.jsonが作成される
スクリーンショット 2017-12-02 16.03.54.jpg

 
tasks.jsonを下記のように変えて保存

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "python",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command": "python",
            "args": [
                "${file}"
            ],
            "presentation": {
                "echo": false,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": []
        }
    ]
}

[Command]+[Shift]+[b]で実行できる
スクリーンショット 2017-12-02 16.04.40.jpg

おわりに

ん〜こんなに手軽にデバッグできるのか。
開発準備が整いましたので
色々と作って行こうと思います。(多分)

32
31
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
32
31