LoginSignup
1
1

More than 1 year has passed since last update.

Python 3のデバッグ環境構築(Visual Studio Code + Windows 10 with Poetry)

Last updated at Posted at 2022-07-15

概要

  • 以下のデバッグ環境を構築する。
    • Windows 10
    • Python
      • Poetry

前提条件

  • Python 3 インストール済
  • Visual Studio Code インストール済
  • Visual Studio Code の操作について習熟していること。

環境

環境構築

Poetryインストール

  • コマンドラインで以下を実行。
pip install poetry

プロジェクト作成及び移動

  • コマンドラインで以下を実行。
poetry new foo-project --src
cd foo-project

Visual Studio Code用設定

portry設定修正

  • デフォルトの設定のままだとVisual Studio Codeが仮想環境を認識しないため以下の設定を行う。
  • 仮想環境をプロジェクト配下へ生成するよう設定。
poetry config virtualenvs.create true --local

拡張機能追加

  • 必要であれば、Pythonをインストールする
  • Python Poetryをインストールする

pytest失敗対応

  • poetryで作成されたプロジェクトのpytestバージョンが5.4.3のためpytestに失敗する。
  • コマンドラインで以下を実行。
poetry add -D pytest@latest
  • 以下のように表示されれば成功。
================================================= test session starts =================================================
platform win32 -- Python 3.10.5, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\vcs\gitlab\foo-project
collected 1 item

tests\test_foo_project.py .                                                                                      [100%]

================================================== 1 passed in 0.02s ==================================================

仮想環境へライブラリインストール

  • コマンドラインで以下を実行。
poetry install

コマンドライン動作確認

  • コマンドラインで以下を実行。
poetry run pytest

Visual Studio Code動作確認

  • Visual Studio Codeを起動

  • 以下のようなサンプルプログラムを作成してブレイクポイントを設定する。

mainpy - foo-project - Visual Studio Code_2022-7-15_13-13-37_No-00.png
mainpy - foo-project - Visual Studio Code_2022-7-15_13-13-50_No-00.png

  • 実行デバッグの開始を行いブレイクポイントで止まることを確認。
    mainpy - foo-project - Visual Studio Code_2022-7-15_13-14-19_No-00.png

お疲れ様でした。

感想

  • 一昔前はVisual Studio Codelaunch.jsonを書かないと動かなかったが、今はプラグインと設定のみになったので楽になりましたねー。
  • poetry使い始めたら、pyenv使わなくなりました。プロジェクトごとにバージョン管理できるのでいいですね。
1
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
1
1