Type3157のPythonスクリプトをVS Codeでデバッグする

Type3157はPythonで処理をコーディングできる便利なコンポーネントです。とはいえ、統合された開発環境が用意されているわけではありません。エディタを使ってコーディングすることになりますが、ちょっと複雑なスクリプトを作ろうと思うと、やはりちゃんとした開発ツールが使いたくなります。

Type3157はTRNSYSのコンポーネントですが、仕組としてはPythonを埋め込んだ単なるプログラムです。これならVisual Studio Codeで実行中のTRNSYSにアタッチすればデバッグできるのでは?

そう思って試したらうまくデバッグできてしまったので、以下ご紹介です。

実行条件

TRNSYSのDckファイル、Pythonスクリプトなどすべてのファイルが同じフォルダに納められているのを前提とします。

デバッガー用にパッケージdebugpyを使用します。予めコマンドラインで次のようにしてインストールしておいてください。

pip install debugpy

以下、Type3157のExample、01-SimplePolynomialを使って話を進めます。

デバッグの準備

Visual Studio Codeを起動して以下の設定を行ってください

プロジェクトファイルを開く

まずはじめに、01-SimplePolynomialフォルダを開きます。

メニューから[File]-[Open Folder]を選んで、 で”C:\TRNSYS18\TRNLib\CallingPython-Cffi\Examples\01-SimplePolynomial”フォルダを開いてください。

Pythonスクリプトの編集

デバッグ対象のPythonスクリプト(Polynomial.py)を開いてTRNSYSの実行時にVisual Studio Codeからのアタッチを待機すための処理を追加します。

スクリプトの最初に次のコードを追加します。

import sys
import debugpy  # a debugger for Python
python_path = os.path.join(sys.prefix,'python.exe')
debugpy.configure(python=str(python_path))
debugpy.listen(('localhost', 5678))
debugpy.wait_for_client()  # blocks execution until client is attached

例)Polynomial.pyへデバッグ処理を追加した例

# Python module for the TRNSYS Type calling Python using CFFI
# Data exchange with TRNSYS uses a dictionary, called TRNData in this file (it is the argument of all functions).
# Data for this module will be in a nested dictionary under the module name,
# i.e. if this file is called "MyScript.py", the inputs will be in TRNData["MyScript"]["inputs"]
# for convenience the module name is saved in thisModule
#
# MKu, 2022-02-15

import numpy
import os

# --- Added the following lines ---
import sys
import debugpy  # a debugger for Python
python_path = os.path.join(sys.prefix,'python.exe')
debugpy.configure(python=str(python_path))
debugpy.listen(('localhost', 5678))
debugpy.wait_for_client()  # blocks execution until client is attached
# ---------------------------------

thisModule = os.path.splitext(os.path.basename(__file__))[0]

launch.jsonへデバッグの設定を定義する

デバッグ実行用の設定ファイル(launch.json)を開いて、設定を2つ追加します。

  • “TRNSYS:launch” TRNSYSの起動用の設定
  • “Python-debug”  Type3157のPythonスクリプトへアタッチする設定

launch.jsonへ以下の内容をそのままコピペすればOKです。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "TRNSYS:launch", // Configuration to run TRNSYS
            "type": "cppvsdbg",
            "request": "launch",
            "program": "C:\\TRNSYS18\\Exe\\TrnEXE64.exe", // path to TRNSYS
            "cwd":"${workspaceFolder}",
            "console": "integratedTerminal",
            "args": ["Polynomial.dck"], // Dck file to run
        },
        {
            "name": "Python-debug", // To attach to the Python script
            "type": "python",
            "request": "attach",
            "port":5678,
            "host":"localhost"
        }
    ]
}

launch.jsonがなければ、次の方法で作成してください。

  1. デバッグボタンをクリック
  2. 「create a launch.json file」 をクリック
  3. 「Python File」を選択

ブレークポイントの設定

Polynomial.pyを開いて、ブレークポイントを設定します。この例では出力値の計算を行っている行(62行目)をクリックして、ブレークポイントを設定しています。

デバッグを実行

※デバッグの実行にはVisual Studio CodeのC/C++、及びPythonの機能拡張が必要です。まだインストールされていなければ、後述の補足情報を参考にインストールしてください。

TRNSYS,Pythonスクリプトの順で実行します。

  1. 「TRNSYS:Launch」を選択する
  2. デバッグ実行(Start-debugging)ボタンをクリックしてデバッグを開始

TRNSYSが起動します。この例ではOnline Plotterが画面に表示されますが、すぐに計算が停止してPythonスクリプトのデバッグ待ち状態になります。

つづいて、Pythonスクリプトを実行する。

  1. 「Python-debug」を選択する
  2. デバッグ実行(Start-debugging)ボタンをクリックしてデバッグを開始する。(必ずPolynomial.pyを開いた状態で実行してください)

デバッグが開始されると、さきほど設定したブレークポイントで処理が停止します。

このあとはVisual Studio Code の機能を使ってデバッグすることができます。

注意

Visual Studio Codeを使わず、Simulation Studioでスクリプトを実行する場合は必ずデバッグサーバーの処理をコメントアウトしてください。コメントアウトしていないとデバッガーの実行待ちで、この行から計算が進まなくなります。

import numpy
import os

# # --- Added the following lines ---
# import sys
# import debugpy  # a debugger for Python
# python_path = os.path.join(sys.prefix,'python.exe')
# debugpy.configure(python=str(python_path))
# debugpy.listen(('localhost', 5678))
# debugpy.wait_for_client()  # blocks execution until client is attached
# # ---------------------------------

thisModule = os.path.splitext(os.path.basename(__file__))[0]

# Initialization: function called at TRNSYS initialization

補足情報(Visual Studio Code機能拡張)

Visual Studio CodeにはC/C++、Pythonの機能拡張(Extension)を予めインストールしておいてください。

  1. “Extension”をクリック
  2. “C/C++” をキーボードから入力して機能拡張を検索
  3. “Install”をクリックしてインストールする

同様にPythonの機能拡張もインストールしてください。

  1. “Extension”をクリック
  2. “Python” をキーボードから入力して機能拡張を検索
  3. “Install”をクリックしてインストールする

解説動画

操作方法を動画にまとめました。

動作環境

以下の環境で動作を確認しています。

  • Windows11 Pro(64bit, 22H2)
  • TRNSYS18.05.001(64bit)
  • Visual Studio Code Version: 1.77.3
  • Python 3.10.7
  • debugpy 1.6.7
Pocket

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です