2
0

【備忘録】VSCodeでPythonをデバッグ起動するときのメモ

Last updated at Posted at 2024-03-07

はじめに

VSCodeのデバッガを利用して、poetryの処理を追いたいです。
デバッガの設定で少し手間取ったので、自分用のメモとして残しておきます。

環境

  • VSCode 1.87.0
Version: 1.87.0 (user setup)
Commit: 019f4d1419fbc8219a181fab7892ebccf7ee29a2
Date: 2024-02-27T23:41:44.469Z
Electron: 27.3.2
ElectronBuildId: 26836302
Chromium: 118.0.5993.159
Node.js: 18.17.1
V8: 11.8.172.18-electron.0
OS: Windows_NT x64 10.0.22631

poetry 1.9の処理をデバッグ実行で確認する

事前準備

$ git clone https://github.com/python-poetry/poetry.git

$ poetry install

$ poetry env info

Virtualenv
Python:         3.12.1
Implementation: CPython
Path:           /home/yuji/.cache/pypoetry/virtualenvs/poetry-T2B_RS4e-py3.12
Executable:     /home/yuji/.cache/pypoetry/virtualenvs/poetry-T2B_RS4e-py3.12/bin/python
Valid:          True

Base
Platform:   linux
OS:         posix
Python:     3.12.1
Path:       /home/yuji/.pyenv/versions/3.12.1
Executable: /home/yuji/.pyenv/versions/3.12.1/bin/python3.12

手順

  1. VSCodeでPythonのInterpreterで、poetryが生成したpoetry-T2B_RS4e-py3.12/bin/pythonを選択する
  2. 以下のようなlaunch.jsonを使ってデバッガを起動する。以下のlaunch.jsonpoetry run poetry show --allを実行する。
.vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "test1",
            "type": "debugpy",
            "request": "launch",
            "module": "src.poetry.__main__",
            "args": [
                "show",
                "--all"
            ],

        }
    ]
}

ハマったこと

  • launch.jsonで、moduleではなく"program": "src/property/__main__.py"を指定してしまった。実行時にエラーが発生した。
  • Pythonのinterpreterに、poetryが生成していない仮想環境を選択してしまった。実行はできるが、ブレークポイントで停止しなかった。

pandas 2.2.1の処理をデバッグ実行で確認

実行するコード

sample.py
import pandas
df1=pandas.DataFrame({"A":[1,2,3]},index=["x","y","z"])
df2=pandas.DataFrame({"B":[11,12,13]},index=["x","y","z"])
df3 = df1.join(df2)

手順

  1. launch.json"justMyCode": falseを追加して、デバッガを起動する。justMyCodeのデフォルト値はtrue
.vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            // 追加
            "justMyCode": false
        }
    ]
}

When omitted or set to true (the default), restricts debugging to user-written code only. Set to false to also enable debugging of standard library functions.

ハマったこと

  • justMyCodeプロパティの存在を知らず、pandasの内のコードにブレークポイントを置いても、処理が止まらなかった
2
0
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
0