0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

QGIS Pugin デバッグ と リロード

Last updated at Posted at 2025-04-14

QGIS の Plugin をデバッグ方法と、ソースコードの変更を反映させる方法

QGIS の Plguin を開発していて、ソースコードを変更しても反映されない事があったので、ソースコードの反映方法をメモします。

ソースコードの変更を反映方法だけを知りたい人はここだけ読んでください

例、ElevationTile4JP という Plugin をデバッグする場合

開発中の ElevationTile4JPフォルダを、zip圧縮して、ElevationTile4JP.zip を作成します。

QGIS => MENU => プラグイン => プラグインの管理とインストール…
image.png

ElevationTile4JP.zip をインストールします。
image.png

C:\Users\username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\ElevationTile4JP
ここに、ElevationTile4JP Plugin がインストールされます。

VSCode で、
C:\Users\username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\ElevationTile4JP
をフォルダで開きます。

VSCode の 右下に表示されている インタープリターの選択、または、Python のバージョン番号をクリックします。
image.png

image.png

インタープリター パスを入力…
image.png

ファイルシステムを参照して Python インタープリターを検索します。
image.png

インストールしている、Python.exe を選択します。
ここで、使っている QGIS Python.exe を選択すると、なぜか下で説明するプロセスにアタッチができなくなります、、、
image.png

.vscode\launch.json
を下記のように書きます。

[
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach QGIS",
            "type": "debugpy",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "${env:APPDATA}/QGIS/QGIS3/profiles/default/python/plugins/ElevationTile4JP"
                }
            ],
            "justMyCode": true
        }
    ]
}
]

プラグインのソースコードの変更を反映する方法

Plugin Reloader という QGIS の Plugin をインストールしてください。
image.png

プラグインの

__init__.py

に、下記コードの、<=== START END ===> の範囲を追加します。

重要 : このコードを追加することで、Plugin をリロードした時に、ソースコードの変更を反映できるようになります。

import sys
import os
import importlib

sys.path.append(os.path.dirname(__file__))

# <=== START
base_dir = os.path.dirname(__file__)
for root, dirs, files in os.walk(base_dir):
    for file in files:
        if file.endswith(".py"):
            # 相対パスを取得
            rel_path = os.path.relpath(os.path.join(root, file), base_dir)
            # .py を外す
            mod_name = os.path.splitext(rel_path)[0]
            # ディレクトリの区切りを '.' に置き換えてモジュールパスに変換
            mod_name = mod_name.replace(os.path.sep, ".")

            # すでにインポートされているモジュールなら reload を試みる
            if mod_name in sys.modules and mod_name != "__init__":
                print(f"Reloading: {mod_name}")
                importlib.reload(sys.modules[mod_name])
# END ===>

def classFactory(iface):  # pylint: disable=invalid-name
    from elevation_tile_for_jp import ElevationTileForJP
    return ElevationTileForJP(iface)

VSCode で、実行とデバッグ ボタンを押して、

image.png
Attach QGIS... をクリックします。

プロセスに接続 qgis-ltr-bin.exe を選択します。
image.png

QGIS で、目的の Plugin この場合、ElevationTile4JPを リロードします。
image.png

ブレークポイントで止まるようになりました。
image.png

オリジナルソースコード、
image.png

ソースコードを下記のように変更しても、
image.png

QGIS で、もう一度 ElevationTile4JPを リロードすれば、
変更したソースコードのブレークポイントで停止するようになりました。
image.png

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?