Skip to content

Could not find platform independent libraries <prefix> when debugging tests in VS Code #966

Open

Description

I'm trying to write a plugin for QGIS and some tests:

import os
import sys
import unittest

from qgis.core import QgsVectorLayer

class Whatever(unittest.TestCase):
    def test_1(self):
        print(f"PATH: { os.environ["PATH"] }")
        print(f"PYTHONPATH: { os.environ["PYTHONPATH"] }")
        print(f"PYTHONHOME: { os.environ["PYTHONHOME"] }")
        print(f"Executable: { sys.executable }")

        layer = QgsVectorLayer("mylayer")

I also have a launch.json like this one:

{
  "version": "0.2.0",
  "configurations": [
    {
        "name": "Python Debugger: Debug Tests",
        "type": "debugpy",
        "request": "launch",
        "console": "integratedTerminal",
        "purpose": ["debug-test"],
        "cwd": "${workspaceFolder}",
        "envFile": "${workspaceFolder}/.env"
    }
  ]
}

and a settings.json:

{
    "python.defaultInterpreterPath": "C:/OSGeo4W/bin/python.exe",
    "python.analysis.extraPaths": [
        "C:/OSGeo4W/apps/qgis-ltr/python"
    ],
    "python.analysis.autoImportCompletions": true,
    "python.autoComplete.extraPaths": ["C:/OSGeo4W/apps/qgis-ltr/python"],
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "tests/",
        "-p",
        "*test.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true
}

and finally my .env-file:

PYTHONHOME=C:/OSGeo4W/apps/Python312
PYTHONPATH=C:/OSGeo4W/apps/qgis-ltr/python
PATH=C:/OSGeo4W/apps/qgis-ltr/bin;C:/OSGeo4W/apps/qt5/bin;C:/OSGeo4W/apps/Python312/Scripts;C:/OSGeo4W/bin;C:/Windows/system32;C:/Windows;C:/Windows/system32/WBem
GDAL_DATA=C:/OSGEO4W/share/gdal
GDAL_DRIVER_PATH=C:/OSGEO4W/bin/gdalplugins
GDAL_FILENAME_IS_UTF8=YES
GS_LIB=C:/OSGeo4W/apps/gs/lib
GEOTIFF_CSV=C:/OSGEO4W/share/epsg_csv
O4W_QT_BINARIES=C:/OSGEO4W/apps/Qt5/bin
O4W_QT_DOC=C:/OSGEO4W/apps/Qt5/doc
O4W_QT_HEADERS=C:/OSGEO4W/apps/Qt5/include
O4W_QT_LIBRARIES=C:/OSGEO4W/apps/Qt5/lib
O4W_QT_PLUGINS=C:/OSGEO4W/apps/Qt5/plugins
O4W_QT_PREFIX=C:/OSGEO4W/apps/Qt5
O4W_QT_TRANSLATIONS=C:/OSGEO4W/apps/Qt5/translations
OSGEO4W_ROOT=C:/OSGeo4W
QT_PLUGIN_PATH=C:/OSGEO4W/apps/qgis-ltr/qtplugins
QT_QPA_PLATFORM=windows:altgr
QGIS_PREFIX_PATH=C:/OSGEO4W/apps/qgis
QGIS_PLUGINPATH=D:/git/QgisPlugIns

When I click on the gutter within the test-explorer to debug the above test, I get the forementioned error. However when I just run the test, everything works fine. I already duplictaed the variables from the env-file to the launch.json-file without any success.

I also tried to put these variables into the settings.json-file into the "terminal.integrated.env.windows-section and started a python-interpreter in the terminal. I wrote this script:

from qgis.core import QgsVectorLayer

and it worked. So afterall I can confirm the above paths are 100% correct. However I cannot make it to have them work in both Test-Explorer and in the Terminal. Having them in env-file only makes terminal break ("ImportError: DLL load failed while importing _core: The specified module could not be found". This error relates to the QgsVectorLayer-import-statement), having them in the settings.json only makes test-explorer break. Having them in both leads to "Invalid message: Found duplicate in "env": PATH".

This is my VS Code version:

Version: 1.106.3
Commit: bf9252a2fb45be6893dd8870c0bf37e2e1766d61
Date: 2025-11-25T22:28:18.024Z
Electron: 37.7.0
ElectronBuildId: 12781156
Chromium: 138.0.7204.251
Node.js: 22.20.0
V8: 13.8.258.32-electron.0
OS: Windows_NT x64 10.0.26100

Activity

  1. rchiodo commented on Feb 25, 2026

    @rchiodo
    Contributor

    I guess I'd turn on logging in the launch.json and then look at the output logs. Output logs should end up where the debugger extension is installed. For me that's here:

    C:\Users\rchiodo\.vscode\extensions\ms-python.debugpy-2025.19.2026022401-win32-x64
    
  2. Kohlroulade commented on Feb 26, 2026

    @Kohlroulade
    Author

    I found this in the debugpy-adapter.log:

                         "env": {
                             "PYTHONHOME": "C:/OSGeo4W/apps/Python312",
                             "PYTHONPATH": "C:/OSGeo4W/apps/qgis-ltr/python;C:/OSGeo4W/apps/qgis-ltr/python",
                             "PATH": "C:/OSGeo4W/apps/qgis-ltr/bin;C:/OSGeo4W/apps/qt5/bin;C:/OSGeo4W/apps/Python312/Scripts;C:/OSGeo4W/bin;C:/Windows/system32;C:/Windows;C:/Windows/system32/WBem",
                             "GDAL_DATA": "C:/OSGEO4W/share/gdal",
                             "GDAL_DRIVER_PATH": "C:/OSGEO4W/bin/gdalplugins",
                             "GDAL_FILENAME_IS_UTF8": "YES",
                             "GS_LIB": "C:/OSGeo4W/apps/gs/lib",
                             "GEOTIFF_CSV": "C:/OSGEO4W/share/epsg_csv",
                             "O4W_QT_BINARIES": "C:/OSGEO4W/apps/Qt5/bin",
                             "O4W_QT_DOC": "C:/OSGEO4W/apps/Qt5/doc",
                             "O4W_QT_HEADERS": "C:/OSGEO4W/apps/Qt5/include",
                             "O4W_QT_LIBRARIES": "C:/OSGEO4W/apps/Qt5/lib",
                             "O4W_QT_PLUGINS": "C:/OSGEO4W/apps/Qt5/plugins",
                             "O4W_QT_PREFIX": "C:/OSGEO4W/apps/Qt5",
                             "O4W_QT_TRANSLATIONS": "C:/OSGEO4W/apps/Qt5/translations",
                             "OSGEO4W_ROOT": "C:/OSGeo4W",
                             "QT_PLUGIN_PATH": "C:/OSGEO4W/apps/qgis-ltr/qtplugins",
                             "QT_QPA_PLATFORM": "windows:altgr",
                             "QGIS_PREFIX_PATH": "C:/OSGEO4W/apps/qgis",
                             "QGIS_PLUGINPATH": "D:/git/QgisPlugIns",
                             "PYTHONIOENCODING": "UTF-8",
                             "PYTHONUNBUFFERED": "1",
                             "TEST_RUN_PIPE": "\\\\.\\pipe\\python-test-results-9a10978f17deae786816",
                             "RUN_TEST_IDS_PIPE": "C:\\Users\\me\\AppData\\Local\\Temp\\test-ids-40a21764154ac953ea9e.txt",
                             "Path": "C:/OSGeo4W/apps/qgis-ltr/bin;C:/OSGeo4W/apps/qt5/bin;C:/OSGeo4W/apps/Python312/Scripts;C:/OSGeo4W/bin;C:/Windows/system32;C:/Windows;C:/Windows/system32/WBem;C:\\Program Files\\Eclipse Adoptium\\jdk-21.0.10.7-hotspot\\bin;C:\\Python312\\Scripts\\;C:\\Python312\\;D:\\oracle\\client\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\Puppet Labs\\Puppet\\bin;C:\\Program Files\\nodejs\\;C:\\ProgramData\\chocolatey\\bin;C:\\Program Files\\PuTTY\\;C:\\ProgramData\\chocolatey\\lib\\maven\\apache-maven-3.9.9\\bin;C:\\Program Files\\dotnet\\;C:\\Program Files\\PowerShell\\7\\;C:\\Users\\me\\AppData\\Local\\Microsoft\\WindowsApps;C:\\OSGeo4W\\apps\\Python39\\Scripts;C:\\Users\\me\\.dotnet\\tools;C:\\Users\\me\\AppData\\Local\\Programs\\Git\\cmd;;C:\\Program Files\\Eclipse Adoptium\\jdk-21.0.10.7-hotspot\\bin;C:\\Python312\\Scripts\\;C:\\Python312\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\Puppet Labs\\Puppet\\bin;C:\\Program Files\\nodejs\\;C:\\ProgramData\\chocolatey\\bin;C:\\Program Files\\PuTTY\\;C:\\ProgramData\\chocolatey\\lib\\maven\\apache-maven-3.9.9\\bin;C:\\Program Files\\dotnet\\;C:\\Program Files\\PowerShell\\7\\;C:\\Users\\me\\AppData\\Local\\Microsoft\\WindowsApps;C:\\OSGeo4W\\apps\\Python39\\Scripts;C:\\Users\\me\\.dotnet\\tools;C:\\Users\\me\\AppData\\Local\\Programs\\Git\\cmd;"
                         },
    

    As you can see, the value in PYTHONPATH is doubled, while the env-var PATH is doubled and contaisn many duplicates also. So I tried to eleiminate the envFile-setting within launch.json, as I assued the file is read twice. However that changed absolutely nothing, the variables are still doubled. I'm not sure if this is the actual problem here, but the rest of the variables seem okay to me.

    There's also this suspicious log-entry:

    D+00001.047: /handling #2 request "launch" from Client[1]/
                 Client[1] <-- {
                     "seq": 7,
                     "type": "request",
                     "command": "runInTerminal",
                     "arguments": {
                         "kind": "integrated",
                         "title": "Python Debug Console",
                         "args": [
                             "C:\\OSGeo4W\\bin\\python.exe",
                             "c:\\Users\\me\\.vscode\\extensions\\ms-python.debugpy-2025.16.0-win32-x64\\bundled\\libs\\debugpy\\launcher",
                             "56970",
                             "--",
                             "c:\\Users\\me\\.vscode\\extensions\\ms-python.python-2025.16.0-win32-x64\\python_files\\unittestadapter\\execution.py",
                             "c:\\Users\\me\\.vscode\\extensions\\ms-python.python-2025.16.0-win32-x64\\python_files\\unittestadapter\\execution.py",
                             "--udiscovery",
                             "-v",
                             "-s",
                             "tests/",
                             "-p",
                             "*test.py"
                         ],
                         "env": {
                             "DEBUGPY_LOG_DIR": "c:\\Users\\me\\.vscode\\extensions\\ms-python.debugpy-2025.16.0-win32-x64"
                         },
                         "cwd": "D:\\git\\test"
                     }
                 }
    

    What seems strange to me here, is the doubled argument " "c:\\Users\\me\\.vscode\\extensions\\ms-python.python-2025.16.0-win32-x64\\python_files\\unittestadapter\\execution.py".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage-neededNeeds assignment to the proper sub-team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions