TIL 20210608 vscode pytest使用時のモジュールNotFoundError


現在pytestをテストモジュールとして、IDEはvsccodeを使用しています.
ユニットテストのためにrepositoryを再取得する場合、常にmoduleNotFoundErrorが発生する.
発生するたびに検索が面倒になり、簡単に整理するための記録です.

run and debugタブで、現在のファイルを実行するオプションを指定し、次のようにギア別に設定ファイル(/.vcode/launch.json)を開きます.

最初に何も設定されていない場合、cwdおよびenvは存在せず、これらの部分を以下からコピーして追加することができる.
{
    // 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": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {"PYTHONPATH": "${workspaceRoot}"},
            "console": "integratedTerminal"
        }
    ]
}
筆者はこのようにpytestを問題のない問題に変換できると考えているが,PATH関連の問題であるため,100%解決できるかどうかは不明である.